Skip to content

Commit

Permalink
Merge pull request #98 from imagekit-developer/IK-1499
Browse files Browse the repository at this point in the history
added checks paramter
  • Loading branch information
imagekitio authored Aug 7, 2024
2 parents 8166889 + 5475c8c commit 9fe6eb0
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 3 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -376,7 +377,8 @@ imagekit.upload({
value: 'w-100'
}
]
}
},
checks={`"file.size" < "1mb"`}
}).then(response => {
console.log(response);
}).catch(error => {
Expand Down
4 changes: 4 additions & 0 deletions libs/interfaces/UploadOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,8 @@ export interface UploadOptions {
[key: string]: string | number | boolean | Array<string | number | boolean>;
},
transformation?: Transformation
/**
* Optional `checks` parameters can be used to run server-side checks before files are uploaded to the Media Library.
*/
checks?: string
}
2 changes: 2 additions & 0 deletions libs/upload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]));
}
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.0.1",
"version": "5.1.0",
"description": "Offical NodeJS SDK for ImageKit.io integration",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
24 changes: 24 additions & 0 deletions tests/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit 9fe6eb0

Please sign in to comment.