Skip to content

Commit f9c18d6

Browse files
committed
fixed type issues and updated test cases
1 parent e11e356 commit f9c18d6

File tree

6 files changed

+15
-11
lines changed

6 files changed

+15
-11
lines changed

src/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { version } from "../package.json";
22
import errorMessages from "./constants/errorMessages";
33
import { ImageKitOptions, UploadOptions, UploadResponse, UrlOptions } from "./interfaces";
4+
import IKResponse from "./interfaces/IKResponse";
45
import { upload } from "./upload/index";
56
import { url } from "./url/index";
67
import transformationUtils from "./utils/transformation";
@@ -83,9 +84,9 @@ class ImageKit {
8384
*
8485
* @param uploadOptions
8586
*/
86-
upload(uploadOptions: UploadOptions, options?: Partial<ImageKitOptions>): Promise<UploadResponse>
87-
upload(uploadOptions: UploadOptions, callback: (err: Error | null, response: UploadResponse | null) => void, options?: Partial<ImageKitOptions>): XMLHttpRequest;
88-
upload(uploadOptions: UploadOptions, callbackOrOptions?: ((err: Error | null, response: UploadResponse | null) => void) | Partial<ImageKitOptions>, options?: Partial<ImageKitOptions>): XMLHttpRequest | Promise<UploadResponse> {
87+
upload(uploadOptions: UploadOptions, options?: Partial<ImageKitOptions>): Promise<IKResponse<UploadResponse>>
88+
upload(uploadOptions: UploadOptions, callback: (err: Error | null, response: IKResponse<UploadResponse> | null) => void, options?: Partial<ImageKitOptions>): XMLHttpRequest;
89+
upload(uploadOptions: UploadOptions, callbackOrOptions?: ((err: Error | null, response: IKResponse<UploadResponse> | null) => void) | Partial<ImageKitOptions>, options?: Partial<ImageKitOptions>): XMLHttpRequest | Promise<IKResponse<UploadResponse>> {
8990
let callback;
9091
if (typeof callbackOrOptions === 'function') {
9192
callback = callbackOrOptions;
@@ -97,7 +98,7 @@ class ImageKit {
9798
...options,
9899
};
99100
const xhr = new XMLHttpRequest();
100-
const promise = promisify<UploadResponse>(this, upload)(xhr, uploadOptions, mergedOptions, callback);
101+
const promise = promisify<IKResponse<UploadResponse>>(this, upload)(xhr, uploadOptions, mergedOptions, callback);
101102
if (typeof promise === "object" && typeof promise.then === "function") {
102103
return promise
103104
} else {

src/interfaces/UploadOptions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,5 @@ export interface UploadOptions {
9797
* Stringified JSON key-value data to be associated with the asset. Checkout overwriteCustomMetadata parameter to understand default behaviour.
9898
* Before setting any custom metadata on an asset you have to create the field using custom metadata fields API.
9999
*/
100-
customMetadata?: Record<string, string | number | boolean>
100+
customMetadata?: string | Record<string, string | number | boolean | Array<string | number | boolean>>
101101
}

src/interfaces/UploadResponse.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,4 @@ export interface UploadResponse {
159159
* Field object which will contain the status of each extension at the time of completion of the update/upload request.
160160
*/
161161
extensionStatus?: { [key: string]: string }
162-
/*
163-
* Metadata for the response containing headers and status
164-
*/
165-
$ResponseMetadata?: object
166162
}

src/upload/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ export const upload = (
5050
else if(Array.isArray(param)) {
5151
formData.append(i, JSON.stringify(param));
5252
}
53+
else if(typeof param === "object" && !(param instanceof File || param instanceof Blob)) {
54+
formData.append(i, JSON.stringify(param));
55+
}
5356
else {
5457
formData.append(i, param);
5558
}

src/utils/request.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@ interface SignatureResponse {
1111

1212
function getResponseHeaderMap(xhr: XMLHttpRequest) {
1313
const headers: Record<string, string | number | boolean> = {};
14-
xhr.getAllResponseHeaders()
14+
const responseHeaders = xhr.getAllResponseHeaders();
15+
if (Object.keys(responseHeaders).length) {
16+
responseHeaders
1517
.trim()
1618
.split(/[\r\n]+/)
1719
.map(value => value.split(/: /))
1820
.forEach(keyValue => {
1921
headers[keyValue[0].trim()] = keyValue[1].trim();
2022
});
23+
}
2124
return headers;
2225
}
2326

test/upload.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,8 @@ describe("File upload", function () {
444444
successUploadResponse();
445445

446446
var arg = server.requests[0].requestBody;
447-
expect(arg.get('file').size).to.be.eq(buffer.length);
447+
448+
expect(JSON.parse(arg.get('file')).data.length).to.be.eq(buffer.length);
448449
expect(arg.get('fileName')).to.be.equal("test_file_name");
449450
expect(arg.get('token')).to.be.equal("test_token");
450451
expect(arg.get('expire')).to.be.equal("123");

0 commit comments

Comments
 (0)