From 8cfd263d118cbcef2dea9b386f08233b01587c82 Mon Sep 17 00:00:00 2001 From: ANKUR DWIVEDI Date: Tue, 23 Jul 2024 02:17:21 +0530 Subject: [PATCH 1/2] added checks paramter --- libs/interfaces/UploadOptions.ts | 4 ++++ libs/upload/index.ts | 2 ++ package.json | 2 +- tests/upload.js | 24 ++++++++++++++++++++++++ 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/libs/interfaces/UploadOptions.ts b/libs/interfaces/UploadOptions.ts index 95a477d..ed36a20 100644 --- a/libs/interfaces/UploadOptions.ts +++ b/libs/interfaces/UploadOptions.ts @@ -108,4 +108,8 @@ export interface UploadOptions { [key: string]: string | number | boolean | Array; }, transformation?: Transformation + /** + * Optional `checks` parameters can be used to run server-side checks before files are uploaded to the Media Library. + */ + checks?: string } diff --git a/libs/upload/index.ts b/libs/upload/index.ts index c8ff345..5ac7420 100644 --- a/libs/upload/index.ts +++ b/libs/upload/index.ts @@ -92,6 +92,8 @@ export default function ( } else if (key === "transformation" && typeof uploadOptions.transformation === "object" && uploadOptions.transformation !== null) { form.append(key, JSON.stringify(uploadOptions.transformation)); + } else if (key === "checks" && uploadOptions.checks) { + form.append(key, uploadOptions.checks); } else { form.append(key, String(uploadOptions[key])); } diff --git a/package.json b/package.json index 3754b8b..5727607 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "imagekit", - "version": "5.0.1", + "version": "5.0.2", "description": "Offical NodeJS SDK for ImageKit.io integration", "main": "./dist/index.js", "types": "./dist/index.d.ts", diff --git a/tests/upload.js b/tests/upload.js index fa4ea5f..b245009 100644 --- a/tests/upload.js +++ b/tests/upload.js @@ -548,4 +548,28 @@ describe("File upload", function () { expect(callback.calledOnce).to.be.true; sinon.assert.calledWith(callback, errRes, null); }); + + it("With checks option", function (done) { + const fileOptions = { + fileName: "test_file_name", + file: "test_file_content", + checks: "'request.folder' : '/'", + }; + + 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: "checks", fieldValue: fileOptions.checks }); + done(); + }); + + imagekit.upload(fileOptions, callback); + }); }); From 5475c8cbb0cd3df02e7c2bb95377025fd8646a91 Mon Sep 17 00:00:00 2001 From: ANKUR DWIVEDI Date: Thu, 25 Jul 2024 10:13:49 +0530 Subject: [PATCH 2/2] updated readme and version --- README.md | 6 ++++-- package.json | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c12f1cf..bf3ede4 100644 --- a/README.md +++ b/README.md @@ -350,7 +350,8 @@ imagekit.upload({ value: 'w-100' } ] - } + }, + checks={`"file.size" < "1mb"`} // To run server side checks before uploading files. Notice the quotes around file.size and 1mb. }, function(error, result) { if(error) console.log(error); else console.log(result); @@ -376,7 +377,8 @@ imagekit.upload({ value: 'w-100' } ] - } + }, + checks={`"file.size" < "1mb"`} }).then(response => { console.log(response); }).catch(error => { diff --git a/package.json b/package.json index 5727607..2958dbc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "imagekit", - "version": "5.0.2", + "version": "5.1.0", "description": "Offical NodeJS SDK for ImageKit.io integration", "main": "./dist/index.js", "types": "./dist/index.d.ts",