Skip to content

Commit 91ce0a2

Browse files
committed
added promise support, new api parameters
1 parent dc44520 commit 91ce0a2

File tree

4 files changed

+61
-9
lines changed

4 files changed

+61
-9
lines changed

src/constants/supportedTransforms.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,11 @@ const supportedTransforms: { [key: string]: string } = {
281281
* @link https://docs.imagekit.io/features/image-transformations/resize-crop-and-other-transformations#original-image-orig
282282
*/
283283
original: "orig",
284+
285+
/**
286+
* @link https://docs.imagekit.io/features/image-transformations/conditional-transformations
287+
*/
288+
raw: "raw",
284289
}
285290

286291

src/index.ts

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,31 @@ function privateKeyPassed(options: ImageKitOptions) {
1313
return typeof (options as any).privateKey != "undefined";
1414
}
1515

16+
const promisify = function <T = void>(thisContext: ImageKit, fn: Function) {
17+
return function (...args: any[]): Promise<T> | void {
18+
console.log('arg: ', args);
19+
if (args.length === fn.length && typeof args[args.length - 1] !== "undefined") {
20+
if (typeof args[args.length - 1] !== "function") {
21+
throw new Error("Callback must be a function.");
22+
}
23+
fn.call(thisContext, ...args);
24+
} else {
25+
return new Promise<T>((resolve, reject) => {
26+
const callback = function (err: Error, ...results: any[]) {
27+
if (err) {
28+
return reject(err);
29+
} else {
30+
resolve(results.length > 1 ? results : results[0]);
31+
}
32+
};
33+
args.pop()
34+
args.push(callback);
35+
fn.call(thisContext, ...args);
36+
});
37+
}
38+
};
39+
};
40+
1641
class ImageKit {
1742
options: ImageKitOptions = {
1843
sdkVersion: `javascript-${version}`,
@@ -58,16 +83,20 @@ class ImageKit {
5883
*
5984
* @param uploadOptions
6085
*/
61-
upload(
62-
uploadOptions: UploadOptions,
63-
callback?: (err: Error | null, response: UploadResponse | null) => void,
64-
options?: Partial<ImageKitOptions>,
65-
): void {
66-
var mergedOptions = {
86+
upload(uploadOptions: UploadOptions, options?: Partial<ImageKitOptions>): Promise<UploadResponse>
87+
upload(uploadOptions: UploadOptions, callback: (err: Error | null, response: UploadResponse | null) => void, options?: Partial<ImageKitOptions>): void;
88+
upload(uploadOptions: UploadOptions, callbackOrOptions?: ((err: Error | null, response: UploadResponse | null) => void) | Partial<ImageKitOptions>, options?: Partial<ImageKitOptions>): void | Promise<UploadResponse> {
89+
let callback;
90+
if (typeof callbackOrOptions === 'function') {
91+
callback = callbackOrOptions;
92+
} else {
93+
options = callbackOrOptions || {};
94+
}
95+
var mergedOptions = {
6796
...this.options,
6897
...options,
6998
};
70-
return upload(uploadOptions, mergedOptions, callback);
99+
return promisify<UploadResponse>(this, upload)(uploadOptions, mergedOptions, callback);
71100
}
72101
}
73102

src/interfaces/UploadOptions.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,22 @@ export interface UploadOptions {
7575
* Final status of pending extensions will be sent to this URL.
7676
*/
7777
webhookUrl?: string
78+
/*
79+
* Default is true. If overwriteFile is set to false and useUniqueFileName is also false, and a file already exists at the exact location, upload API will return an error immediately.
80+
*/
81+
overwriteFile?: boolean
82+
/*
83+
* Default is true. If set to true and a file already exists at the exact location, its AITags will be removed. Set overwriteAITags to false to preserve AITags.
84+
*/
85+
overwriteAITags?: boolean
86+
/*
87+
* Default is true. If the request does not have tags , overwriteTags is set to true and a file already exists at the exact location, exiting tags will be removed.
88+
* In case the request body has tags, setting overwriteTags to false has no effect and request's tags are set on the asset.
89+
*/
90+
overwriteTags?: boolean
91+
/*
92+
* Default is true. If the request does not have customMetadata , overwriteCustomMetadata is set to true and a file already exists at the exact location, exiting customMetadata will be removed.
93+
* In case the request body has customMetadata, setting overwriteCustomMetadata to false has no effect and request's customMetadata is set on the asset.
94+
*/
95+
overwriteCustomMetadata?: boolean
7896
}

test/url-generation.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,11 +337,11 @@ describe("URL generation", function () {
337337
effectContrast: true,
338338
effectGray: true,
339339
original: true,
340-
raw: "this_should_come_as_it_is"
340+
raw: "h-200,w-300,l-image,i-logo.png,l-end"
341341
}]
342342
})
343343

344-
expect(url).equal(`https://ik.imagekit.io/test_url_endpoint/tr:h-300,w-400,ar-4-3,q-40,c-force,cm-extract,fo-left,f-jpeg,r-50,bg-A94D34,b-5-A94D34,rt-90,bl-10,n-some_name,ox-35,oy-35,ofo-bottom,oh-20,ow-20,oi-folder@@file.jpg,oit-false,oiar-4:3,oibg-0F0F0F,oib-10_0F0F0F,oidpr-2,oiq-50,oic-force,ot-two%20words,ots-20,otf-Open%20Sans,otc-00FFFF,oa-5,ott-b,obg-00AAFF55,ote-b3ZlcmxheSBtYWRlIGVhc3k%3D,otw-50,otbg-00AAFF55,otp-40,otia-left,or-10,pr-true,lo-true,t-5,md-true,cp-true,di-folder@@file.jpg,dpr-3,e-sharpen-10,e-usm-2-2-0.8-0.024,e-contrast-true,e-grayscale-true,orig-true,this_should_come_as_it_is/test_path.jpg?ik-sdk-version=javascript-${pkg.version}`);
344+
expect(url).equal(`https://ik.imagekit.io/test_url_endpoint/tr:h-300,w-400,ar-4-3,q-40,c-force,cm-extract,fo-left,f-jpeg,r-50,bg-A94D34,b-5-A94D34,rt-90,bl-10,n-some_name,ox-35,oy-35,ofo-bottom,oh-20,ow-20,oi-folder@@file.jpg,oit-false,oiar-4:3,oibg-0F0F0F,oib-10_0F0F0F,oidpr-2,oiq-50,oic-force,ot-two%20words,ots-20,otf-Open%20Sans,otc-00FFFF,oa-5,ott-b,obg-00AAFF55,ote-b3ZlcmxheSBtYWRlIGVhc3k%3D,otw-50,otbg-00AAFF55,otp-40,otia-left,or-10,pr-true,lo-true,t-5,md-true,cp-true,di-folder@@file.jpg,dpr-3,e-sharpen-10,e-usm-2-2-0.8-0.024,e-contrast-true,e-grayscale-true,orig-true,h-200,w-300,l-image,i-logo.png,l-end/test_path.jpg?ik-sdk-version=javascript-${pkg.version}`);
345345
});
346346
});
347347

0 commit comments

Comments
 (0)