Skip to content

Commit 20a9602

Browse files
committed
code review changes
1 parent f9c18d6 commit 20a9602

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

src/upload/index.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,23 @@ export const upload = (
4040
}
4141

4242
var formData = new FormData();
43-
let i: keyof typeof uploadOptions;
44-
for (i in uploadOptions) {
45-
const param = uploadOptions[i];
46-
if (typeof param !== "undefined") {
47-
if (typeof param === "string" || typeof param === "boolean") {
48-
formData.append(i, String(param));
49-
}
50-
else if(Array.isArray(param)) {
51-
formData.append(i, JSON.stringify(param));
52-
}
53-
else if(typeof param === "object" && !(param instanceof File || param instanceof Blob)) {
54-
formData.append(i, JSON.stringify(param));
43+
let key: keyof typeof uploadOptions;
44+
for (key in uploadOptions) {
45+
if (key) {
46+
if (key == "file" && typeof uploadOptions.file != "string") {
47+
formData.append('file', uploadOptions.file, String(uploadOptions.fileName));
48+
} else if (key == "tags" && Array.isArray(uploadOptions.tags)) {
49+
formData.append('tags', uploadOptions.tags.join(","));
50+
} else if (key == "responseFields" && Array.isArray(uploadOptions.responseFields)) {
51+
formData.append('responseFields', uploadOptions.responseFields.join(","));
52+
} else if (key == "extensions" && Array.isArray(uploadOptions.extensions)) {
53+
formData.append('extensions', JSON.stringify(uploadOptions.extensions));
54+
} else if (key === "customMetadata" && typeof uploadOptions.customMetadata === "object" &&
55+
!Array.isArray(uploadOptions.customMetadata) && uploadOptions.customMetadata !== null) {
56+
formData.append('customMetadata', JSON.stringify(uploadOptions.customMetadata));
5557
}
5658
else {
57-
formData.append(i, param);
59+
formData.append(key, String(uploadOptions[key]));
5860
}
5961
}
6062
}

src/utils/request.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ const addResponseHeadersAndBody = (body: any, xhr: XMLHttpRequest):IKResponse<Up
3939
}
4040

4141
export const request = (uploadFileXHR: XMLHttpRequest,formData: FormData, options: ImageKitOptions & { authenticationEndpoint: string }, callback?: (err: Error | null, response: UploadResponse | null) => void) => {
42-
var signatureXHR = new XMLHttpRequest();
43-
generateSignatureToken(signatureXHR, options, (err, signaturObj) => {
42+
generateSignatureToken(options, (err, signaturObj) => {
4443
if (err) {
4544
return respond(true, err, callback)
4645
} else {
@@ -59,7 +58,8 @@ export const request = (uploadFileXHR: XMLHttpRequest,formData: FormData, option
5958
return uploadFileXHR;
6059
}
6160

62-
export const generateSignatureToken = (xhr:XMLHttpRequest, options: ImageKitOptions & { authenticationEndpoint: string }, callback: (err: Error | null, response: SignatureResponse | null) => void) => {
61+
export const generateSignatureToken = (options: ImageKitOptions & { authenticationEndpoint: string }, callback: (err: Error | null, response: SignatureResponse | null) => void) => {
62+
var xhr = new XMLHttpRequest();
6363
xhr.timeout = 60000;
6464
xhr.open('GET', options.authenticationEndpoint);
6565
xhr.ontimeout = function (e) {

test/upload.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ describe("File upload", function () {
418418
expect(arg.get('expire')).to.be.equal("123");
419419
expect(arg.get('signature')).to.be.equal("test_signature");
420420
expect(arg.get('publicKey')).to.be.equal('test_public_key');
421-
expect(arg.get('tags')).to.be.equal(undefined);
421+
expect(arg.get('tags')).to.be.equal('undefined');
422422
expect(arg.get('isPrivateFile')).to.be.equal(undefined);
423423
expect(arg.get('useUniqueFileName')).to.be.equal(undefined);
424424
expect(arg.get('customCoordinates')).to.be.equal(undefined);
@@ -445,7 +445,7 @@ describe("File upload", function () {
445445

446446
var arg = server.requests[0].requestBody;
447447

448-
expect(JSON.parse(arg.get('file')).data.length).to.be.eq(buffer.length);
448+
expect(arg.get('file').size).to.be.eq(buffer.length);
449449
expect(arg.get('fileName')).to.be.equal("test_file_name");
450450
expect(arg.get('token')).to.be.equal("test_token");
451451
expect(arg.get('expire')).to.be.equal("123");

0 commit comments

Comments
 (0)