Skip to content

Commit 326e6db

Browse files
authored
Merge pull request #45 from imagekit-developer/Added-extensions-in-Javascript-SDK
Added extensions parameter in upload API of javascript sdk
2 parents 8b65733 + a27c0b6 commit 326e6db

File tree

8 files changed

+112
-7
lines changed

8 files changed

+112
-7
lines changed

README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,14 @@ Sample usage
287287
imagekit.upload({
288288
file: file.files[0],
289289
fileName: "abc1.jpg",
290-
tags: ["tag1"]
290+
tags: ["tag1"],
291+
extensions: [
292+
{
293+
name: "aws-auto-tagging",
294+
minConfidence: 80,
295+
maxTags: 10
296+
}
297+
]
291298
}, function(err, result) {
292299
console.log(arguments);
293300
console.log(imagekit.url({

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

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

samples/sample-app/views/index.pug

+8-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,14 @@ html
3737
imagekit.upload({
3838
file : file.files[0],
3939
fileName : file.files[0].name || "test_image.jpg",
40-
tags : ["test_tag_1"]
40+
tags : ["test_tag_1"],
41+
//- extensions: [
42+
//- {
43+
//- name: "aws-auto-tagging",
44+
//- minConfidence: 80,
45+
//- maxTags: 10
46+
//- }
47+
//- ],
4148
}, function(err, result) {
4249
if (err) {
4350
statusEl.innerHTML = "Error uploading image. "+ err.message;

src/interfaces/UploadOptions.ts

+8
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,12 @@ export interface UploadOptions {
6767
* For example, set the value of this field to tags,customCoordinates,isPrivateFile,metadata to get value of tags, customCoordinates, isPrivateFile , and metadata in the response.
6868
*/
6969
responseFields?: string;
70+
/*
71+
* Object with array of extensions to be processed on the image.
72+
*/
73+
extensions?: object[];
74+
/*
75+
* Final status of pending extensions will be sent to this URL.
76+
*/
77+
webhookUrl?: string
7078
}

src/interfaces/UploadResponse.ts

+8
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,12 @@ export interface UploadResponse {
151151
* The metadata of the upload file. Use responseFields property in request to get the metadata returned in response of upload API.
152152
*/
153153
metadata?: Metadata;
154+
/*
155+
* AITags field is populated only because the google-auto-tagging extension was executed synchronously and it received a successresponse.
156+
*/
157+
AITags?: object[];
158+
/*
159+
* Field object which will contain the status of each extension at the time of completion of the update/upload request.
160+
*/
161+
extensionStatus?: { [key: string]: string }
154162
}

src/upload/index.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,23 @@ export const upload = (
3333
return;
3434
}
3535

36+
if(uploadOptions.tags && Array.isArray(uploadOptions.tags))
37+
{
38+
uploadOptions.tags = String(uploadOptions.tags);
39+
}
40+
3641
var formData = new FormData();
3742
let i: keyof typeof uploadOptions;
3843
for (i in uploadOptions) {
3944
const param = uploadOptions[i];
4045
if (typeof param !== "undefined") {
41-
if (typeof param === "string" || typeof param === "boolean") {
46+
if (typeof param === "string" || typeof param === "boolean") {
4247
formData.append(i, String(param));
43-
} else {
48+
}
49+
else if(Array.isArray(param)) {
50+
formData.append(i, JSON.stringify(param));
51+
}
52+
else {
4453
formData.append(i, param);
4554
}
4655
}

test/upload.js

+67-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ const uploadSuccessResponseObj = {
2020
"tags": ["t-shirt", "round-neck", "sale2019"],
2121
"isPrivateFile": false,
2222
"customCoordinates": null,
23-
"fileType": "image"
23+
"fileType": "image",
24+
"AITags":[{"name":"Face","confidence":99.95,"source":"aws-auto-tagging"}],
25+
"extensionStatus":{"aws-auto-tagging":"success"}
2426
};
2527

2628
function successSignature() {
@@ -144,6 +146,22 @@ describe("File upload", function () {
144146
sinon.assert.calledWith(callback, { message: "Missing authentication endpoint for upload", help: "" }, null);
145147
});
146148

149+
it('Missing public key', function(){
150+
const fileOptions = {
151+
fileName: "test_file_name",
152+
file: "test_file"
153+
};
154+
155+
var callback = sinon.spy();
156+
157+
imagekit.upload(fileOptions, callback, {
158+
publicKey : ""
159+
});
160+
161+
expect(server.requests.length).to.be.equal(0);
162+
sinon.assert.calledWith(callback, { message: "Missing public key for upload", help: "" }, null);
163+
});
164+
147165
it('Auth endpoint network error handling', function () {
148166
const fileOptions = {
149167
fileName: "test_file_name",
@@ -336,6 +354,54 @@ describe("File upload", function () {
336354
sinon.assert.calledWith(callback, null, uploadSuccessResponseObj);
337355
});
338356

357+
it('With extensions parameter', function(){
358+
const fileOptions = {
359+
fileName: "test_file_name",
360+
file: "test_file",
361+
tags: "test_tag1,test_tag2",
362+
customCoordinates: "10, 10, 100, 100",
363+
responseFields: "tags, customCoordinates, isPrivateFile, metadata",
364+
useUniqueFileName: false,
365+
isPrivateFile: true,
366+
extensions: [
367+
{
368+
name: "aws-auto-tagging",
369+
minConfidence: 80,
370+
maxTags: 10
371+
}
372+
],
373+
webhookUrl: "https://your-domain/?appId=some-id"
374+
};
375+
var jsonStringifiedExtensions = JSON.stringify(fileOptions.extensions);
376+
var callback = sinon.spy();
377+
378+
imagekit.upload(fileOptions, callback);
379+
380+
expect(server.requests.length).to.be.equal(1);
381+
successSignature();
382+
expect(server.requests.length).to.be.equal(2);
383+
successUploadResponse();
384+
385+
var arg = server.requests[1].requestBody;
386+
387+
expect(arg.get('file')).to.be.equal("test_file");
388+
expect(arg.get('fileName')).to.be.equal("test_file_name");
389+
expect(arg.get('token')).to.be.equal("test_token");
390+
expect(arg.get('expire')).to.be.equal("123");
391+
expect(arg.get('signature')).to.be.equal("test_signature");
392+
expect(arg.get('tags')).to.be.equal("test_tag1,test_tag2");
393+
expect(arg.get('customCoordinates')).to.be.equal("10, 10, 100, 100");
394+
expect(arg.get('responseFields')).to.be.equal("tags, customCoordinates, isPrivateFile, metadata");
395+
expect(arg.get('useUniqueFileName')).to.be.equal('false');
396+
expect(arg.get('isPrivateFile')).to.be.equal('true');
397+
expect(arg.get('publicKey')).to.be.equal('test_public_key');
398+
expect(arg.get('extensions')).to.be.equal(jsonStringifiedExtensions);
399+
expect(arg.get('webhookUrl')).to.be.equal('https://your-domain/?appId=some-id')
400+
401+
expect(callback.calledOnce).to.be.true;
402+
sinon.assert.calledWith(callback, null, uploadSuccessResponseObj);
403+
});
404+
339405
it('Bare minimum request', function () {
340406
const fileOptions = {
341407
fileName: "test_file_name",

0 commit comments

Comments
 (0)