Skip to content

Commit 2f9c49a

Browse files
ANKUR DWIVEDIANKUR DWIVEDI
authored andcommitted
added checks paramter
1 parent 3917883 commit 2f9c49a

File tree

5 files changed

+64
-2
lines changed

5 files changed

+64
-2
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "imagekit-javascript",
3-
"version": "3.0.1",
3+
"version": "3.0.2",
44
"description": "Javascript SDK for using ImageKit.io in the browser",
55
"main": "dist/imagekit.cjs.js",
66
"module": "dist/imagekit.esm.js",

src/interfaces/UploadOptions.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,9 @@ export interface UploadOptions {
141141
* Optional XMLHttpRequest object that you can send for upload API request. You can listen to `progress` and other events on this object for any custom logic.
142142
*/
143143
xhr?: XMLHttpRequest
144+
145+
/**
146+
* Server-side checks to run on the asset.
147+
*/
148+
checks?: string
144149
}

src/upload/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export const upload = (
99
options: ImageKitOptions,
1010
callback?: (err: Error | null, response: UploadResponse | null) => void,
1111
) => {
12+
console.log({uploadOptions})
1213
if (!uploadOptions.file) {
1314
respond(true, errorMessages.MISSING_UPLOAD_FILE_PARAMETER, callback);
1415
return;
@@ -90,6 +91,8 @@ export const upload = (
9091
} else if(key === "transformation" && typeof uploadOptions.transformation === "object" &&
9192
uploadOptions.transformation !== null) {
9293
formData.append(key, JSON.stringify(uploadOptions.transformation));
94+
} else if (key === 'checks' && uploadOptions.checks) {
95+
formData.append("checks", uploadOptions.checks);
9396
} else if(uploadOptions[key] !== undefined) {
9497
formData.append(key, String(uploadOptions[key]));
9598
}

test/upload.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,4 +1309,58 @@ describe("File upload", function () {
13091309
expect(callback.calledOnce).to.be.true;
13101310
sinon.assert.calledWith(callback, errRes, null);
13111311
});
1312+
1313+
it("With checks option", async function () {
1314+
const fileOptions = {
1315+
...securityParameters,
1316+
fileName: "test_file_name",
1317+
file: "test_file",
1318+
responseFields: "tags, customCoordinates, isPrivateFile, metadata",
1319+
useUniqueFileName: false,
1320+
checks: "'request.folder' : '/'",
1321+
};
1322+
var callback = sinon.spy();
1323+
1324+
imagekit.upload(fileOptions, callback);
1325+
1326+
expect(server.requests.length).to.be.equal(1);
1327+
await sleep();
1328+
successUploadResponse();
1329+
await sleep();
1330+
1331+
var arg = server.requests[0].requestBody;
1332+
expect(arg.get("file")).to.be.equal("test_file");
1333+
expect(arg.get("fileName")).to.be.equal("test_file_name");
1334+
expect(arg.get("responseFields")).to.be.equal("tags, customCoordinates, isPrivateFile, metadata");
1335+
expect(arg.get("useUniqueFileName")).to.be.equal("false");
1336+
expect(arg.get("publicKey")).to.be.equal("test_public_key");
1337+
1338+
expect(callback.calledOnce).to.be.true;
1339+
sinon.assert.calledWith(callback, null, uploadSuccessResponseObj);
1340+
});
1341+
1342+
it("Should return error for unsuccessfull check", async function () {
1343+
const fileOptions = {
1344+
...securityParameters,
1345+
fileName: "test_file_name",
1346+
file: "test_file",
1347+
responseFields: "tags, customCoordinates, isPrivateFile, metadata",
1348+
useUniqueFileName: false,
1349+
checks: "'request.folder' : 'marketing/'",
1350+
};
1351+
var callback = sinon.spy();
1352+
1353+
imagekit.upload(fileOptions, callback);
1354+
1355+
expect(server.requests.length).to.be.equal(1);
1356+
await sleep();
1357+
var errRes = {
1358+
help: "For support kindly contact us at [email protected] .",
1359+
message: "Your request failed 'checks' validation.",
1360+
};
1361+
errorUploadResponse(400, errRes);
1362+
await sleep();
1363+
expect(callback.calledOnce).to.be.true;
1364+
sinon.assert.calledWith(callback, errRes, null);
1365+
});
13121366
});

0 commit comments

Comments
 (0)