Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added isPublished and publish object during upload and updateFileDetails respectively #101

Merged
merged 7 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,8 @@ imagekit.upload({
}
]
},
checks={`"file.size" < "1mb"`} // To run server side checks before uploading files. Notice the quotes around file.size and 1mb.
checks: {`"file.size" < "1mb"`}, // To run server side checks before uploading files. Notice the quotes around file.size and 1mb.
isPublished: true
}, function(error, result) {
if(error) console.log(error);
else console.log(result);
Expand Down Expand Up @@ -502,6 +503,8 @@ imagekit.getFileVersionDetails({

Update parameters associated with the file as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/update-file-details). The first argument to the `updateFileDetails` method is the file ID, and the second argument is an object with the parameters to be updated.

Note: If `publish` is included in the update options, no other parameters are allowed. If any are present, an error will be returned: `Your request cannot contain any other parameters when publish is present`.

```js
// Using Callback Function

Expand All @@ -524,15 +527,10 @@ imagekit.updateFileDetails("file_id", {
// Using Promises

imagekit.updateFileDetails("file_id", {
tags : ['image_tag'],
customCoordinates : "10,10,100,100",
extensions: [
{
name: "google-auto-tagging",
maxTags: 5,
minConfidence: 95
}
]
publish: {
isPublished: true,
includeFileVersions: true
}
}).then(response => {
console.log(response);
}).catch(error => {
Expand Down
7 changes: 7 additions & 0 deletions libs/interfaces/FileDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ export interface FileDetailsOptions {
* A key-value data to be associated with the asset. To unset a key, send null value for that key. Before setting any custom metadata on an asset you have to create the field using custom metadata fields API.
*/
customMetadata?: CMValues;
/**
* Configure the publication status of a file and its versions.
*/
publish?: {
isPublished: boolean;
includeFileVersions?: boolean;
};
}

/**
Expand Down
6 changes: 6 additions & 0 deletions libs/interfaces/UploadOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,10 @@ export interface UploadOptions {
* Optional `checks` parameters can be used to run server-side checks before files are uploaded to the Media Library.
*/
checks?: string
/**
* Optional. Determines whether the file should be uploaded as published.
* If set to false, the file will be marked as unpublished, restricting access to the file through the media library only.
* Files in draft or unpublished states can only be publicly accessed after they are published.
*/
isPublished?: boolean
}
4 changes: 4 additions & 0 deletions libs/interfaces/UploadResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,8 @@ export interface UploadResponse {
* A key-value data associated with the asset. Before setting any custom metadata on an asset, you have to create the field using custom metadata fields API.
*/
customMetadata?: CMValues;
/**
* Is the file published or in draft state. It can be either true or false.
*/
isPublished?: boolean
}
21 changes: 14 additions & 7 deletions libs/manage/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,20 @@ const updateDetails = function (
respond(true, errorMessages.UPDATE_DATA_MISSING, callback);
return;
}
var data = {
tags: updateData.tags,
customCoordinates: updateData.customCoordinates,
extensions: updateData.extensions,
webhookUrl: updateData.webhookUrl,
customMetadata: updateData.customMetadata
};

var data;
if (updateData.publish)
data = {
publish: updateData.publish,
};
else
data = {
tags: updateData.tags,
customCoordinates: updateData.customCoordinates,
extensions: updateData.extensions,
webhookUrl: updateData.webhookUrl,
customMetadata: updateData.customMetadata,
};
ahnv marked this conversation as resolved.
Show resolved Hide resolved

var requestOptions = {
url: "https://api.imagekit.io/v1/files/" + fileId + "/details",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "imagekit",
"version": "5.1.0",
"version": "5.2.0",
"description": "Offical NodeJS SDK for ImageKit.io integration",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
22 changes: 22 additions & 0 deletions tests/mediaLibrary.js
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,28 @@ describe("Media library APIs", function () {
imagekit.updateFileDetails(fileId, updateData);
});


it('Update publish status', function (done) {
var fileId = "23902390239203923";

var updateData = {
publish: {
isPublished: false,
},
};

const scope = nock('https://api.imagekit.io')
.patch(`/v1/files/${fileId}/details`)
.basicAuth({ user: initializationParams.privateKey, pass: '' })
.reply(200, function (uri, requestBody) {
expect(this.req.path).equal(`/v1/files/${fileId}/details`);
expect(requestBody).to.deep.equal(updateData);
done()
})

imagekit.updateFileDetails(fileId, updateData);
});

it('Update file details invalid updateData', function (done) {
var fileId = "23902390239203923";

Expand Down
24 changes: 24 additions & 0 deletions tests/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -572,4 +572,28 @@ describe("File upload", function () {

imagekit.upload(fileOptions, callback);
});

it("With isPublished option", function (done) {
const fileOptions = {
fileName: "test_file_name",
file: "test_file_content",
isPublished: false
};

var callback = sinon.spy();

const scope = nock("https://upload.imagekit.io/api")
.post("/v1/files/upload")
.basicAuth({ user: initializationParams.privateKey, pass: "" })
.reply(200, function (uri, requestBody) {
expect(this.req.headers["content-type"]).include("multipart/form-data; boundary=---------------------");
var boundary = this.req.headers["content-type"].replace("multipart/form-data; boundary=", "");
checkFormData({ requestBody, boundary, fieldName: "fileName", fieldValue: fileOptions.fileName });
checkFormData({ requestBody, boundary, fieldName: "file", fieldValue: fileOptions.file });
checkFormData({ requestBody, boundary, fieldName: "isPublished", fieldValue: fileOptions.isPublished });
done();
});

imagekit.upload(fileOptions, callback);
});
});
Loading