Skip to content

Commit 1c767ed

Browse files
committed
updated upload options and test case
1 parent 33bf005 commit 1c767ed

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

libs/interfaces/UploadOptions.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,12 @@ export interface UploadOptions {
7676
/*
7777
* Final status of pending extensions will be sent to this URL.
7878
*/
79-
webhookUrl?: string
79+
webhookUrl?: string;
80+
overwriteFile?: boolean;
81+
overwriteAITags?: boolean;
82+
overwriteTags?: boolean;
83+
overwriteCustomMetadata?: boolean;
84+
customMetadata?: {
85+
[key: string]: string | number | boolean | Array<string | number | boolean>;
86+
}
8087
}

libs/upload/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type FormDataOptions = Modify<
1414
isPrivateFile: string;
1515
extensions?: string;
1616
webhookUrl?: string;
17+
customMetadata?: string;
1718
}
1819
>;
1920

@@ -55,6 +56,9 @@ export default function (
5556
}
5657
else if(key == "extensions" && Array.isArray(uploadOptions.extensions)){
5758
formData.extensions = JSON.stringify(uploadOptions.extensions);
59+
} else if (key === "customMetadata" && typeof uploadOptions.customMetadata === "object" &&
60+
!Array.isArray(uploadOptions.customMetadata) && uploadOptions.customMetadata !== null) {
61+
formData.customMetadata = JSON.stringify(uploadOptions.customMetadata)
5862
}
5963
else {
6064
formData[key] = String(uploadOptions[key]);

tests/upload.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,20 @@ describe("File upload", function () {
107107
maxTags: 10
108108
}
109109
],
110-
webhookUrl: "https://your-domain/?appId=some-id"
110+
webhookUrl: "https://your-domain/?appId=some-id",
111+
overwriteFile: true,
112+
overwriteAITags: false,
113+
overwriteTags: true,
114+
overwriteCustomMetadata: false,
115+
customMetadata: {
116+
brand: "Nike",
117+
color: "red"
118+
},
111119
};
112120

113121
var callback = sinon.spy();
114122
var jsonStringifiedExtensions = JSON.stringify(fileOptions.extensions);
123+
const customMetadata = JSON.stringify(fileOptions.customMetadata);
115124

116125
const scope = nock('https://upload.imagekit.io/api')
117126
.post('/v1/files/upload')
@@ -127,6 +136,11 @@ describe("File upload", function () {
127136
checkFormData({requestBody,boundary,fieldName:"responseFields",fieldValue:"tags,metadata"});
128137
checkFormData({requestBody,boundary,fieldName:"extensions",fieldValue:jsonStringifiedExtensions});
129138
checkFormData({requestBody,boundary,fieldName:"webhookUrl",fieldValue:"https://your-domain/?appId=some-id"});
139+
checkFormData({requestBody,boundary,fieldName:"overwriteFile",fieldValue:"true"});
140+
checkFormData({requestBody,boundary,fieldName:"overwriteAITags",fieldValue:"false"});
141+
checkFormData({requestBody,boundary,fieldName:"overwriteTags",fieldValue:"true"});
142+
checkFormData({requestBody,boundary,fieldName:"overwriteCustomMetadata",fieldValue:"false"});
143+
checkFormData({requestBody,boundary,fieldName:"customMetadata",fieldValue:customMetadata});
130144
done()
131145
})
132146

0 commit comments

Comments
 (0)