diff --git a/README.md b/README.md index bf3ede43..4289312d 100644 --- a/README.md +++ b/README.md @@ -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); @@ -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 @@ -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 => { diff --git a/libs/interfaces/FileDetails.ts b/libs/interfaces/FileDetails.ts index 41566296..683ef478 100644 --- a/libs/interfaces/FileDetails.ts +++ b/libs/interfaces/FileDetails.ts @@ -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; + }; } /** diff --git a/libs/interfaces/UploadOptions.ts b/libs/interfaces/UploadOptions.ts index ed36a206..47389b21 100644 --- a/libs/interfaces/UploadOptions.ts +++ b/libs/interfaces/UploadOptions.ts @@ -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 } diff --git a/libs/interfaces/UploadResponse.ts b/libs/interfaces/UploadResponse.ts index ce35b4f8..9ca44376 100644 --- a/libs/interfaces/UploadResponse.ts +++ b/libs/interfaces/UploadResponse.ts @@ -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 } diff --git a/libs/manage/file.ts b/libs/manage/file.ts index 7735726b..74cd0dc3 100644 --- a/libs/manage/file.ts +++ b/libs/manage/file.ts @@ -202,14 +202,22 @@ const updateDetails = function ( respond(true, errorMessages.UPDATE_DATA_MISSING, callback); return; } - var data = { + + var data = {}; + data = { tags: updateData.tags, customCoordinates: updateData.customCoordinates, extensions: updateData.extensions, webhookUrl: updateData.webhookUrl, - customMetadata: updateData.customMetadata + customMetadata: updateData.customMetadata, }; + if (updateData.publish) + data = { + ...data, + publish: updateData.publish, + }; + var requestOptions = { url: "https://api.imagekit.io/v1/files/" + fileId + "/details", method: "PATCH", diff --git a/package.json b/package.json index 2958dbca..38d0f722 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/tests/mediaLibrary.js b/tests/mediaLibrary.js index f583e0d0..6f9defa7 100644 --- a/tests/mediaLibrary.js +++ b/tests/mediaLibrary.js @@ -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"; diff --git a/tests/upload.js b/tests/upload.js index b245009e..935c6f18 100644 --- a/tests/upload.js +++ b/tests/upload.js @@ -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); + }); });