Skip to content

Commit 09c47d2

Browse files
committed
expose url, upload static functions. Other updates
1 parent b4daa29 commit 09c47d2

File tree

9 files changed

+239
-313
lines changed

9 files changed

+239
-313
lines changed

rollup.config.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ export default [
1212
output: {
1313
name: "ImageKit",
1414
file: pkg.browser,
15-
format: "umd",
16-
sourceMap: true,
15+
format: "umd"
1716
},
1817
plugins: [
1918
nodeResolve({ extensions: [".ts"] }),

src/index.ts

Lines changed: 58 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,24 @@
1-
import { version } from "../package.json";
21
import errorMessages from "./constants/errorMessages";
32
import { ImageKitOptions, UploadOptions, UploadResponse, UrlOptions } from "./interfaces";
43
import IKResponse from "./interfaces/IKResponse";
5-
import { upload } from "./upload/index";
6-
import respond from "./utils/respond";
7-
import { url } from "./url/index";
4+
import { upload } from "./upload";
5+
import { buildURL } from "./url/builder";
86
import transformationUtils from "./utils/transformation";
7+
type MakeRequired<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>;
8+
99

1010
function mandatoryParametersAvailable(options: ImageKitOptions) {
1111
return options.urlEndpoint;
1212
}
1313

14-
const promisify = function <T = void>(thisContext: ImageKit, fn: Function) {
15-
return function (...args: any[]): Promise<T> | void {
16-
if (args.length === fn.length && typeof args[args.length - 1] !== "undefined") {
17-
if (typeof args[args.length - 1] !== "function") {
18-
throw new Error("Callback must be a function.");
19-
}
20-
fn.call(thisContext, ...args);
21-
} else {
22-
return new Promise<T>((resolve, reject) => {
23-
const callback = function (err: Error, ...results: any[]) {
24-
if (err) {
25-
return reject(err);
26-
} else {
27-
resolve(results.length > 1 ? results : results[0]);
28-
}
29-
};
30-
args.pop()
31-
args.push(callback);
32-
fn.call(thisContext, ...args);
33-
});
34-
}
35-
};
36-
};
37-
3814
class ImageKit {
39-
options: ImageKitOptions = {
15+
options: MakeRequired<ImageKitOptions, "urlEndpoint"> = {
4016
publicKey: "",
4117
urlEndpoint: "",
4218
transformationPosition: transformationUtils.getDefault(),
4319
};
4420

45-
constructor(opts: ImageKitOptions) {
21+
constructor(opts: MakeRequired<ImageKitOptions, "urlEndpoint">) {
4622
this.options = { ...this.options, ...(opts || {}) };
4723
if (!mandatoryParametersAvailable(this.options)) {
4824
throw errorMessages.MANDATORY_INITIALIZATION_MISSING;
@@ -54,38 +30,70 @@ class ImageKit {
5430
}
5531

5632
/**
57-
* A utility function to generate asset URL. It applies the specified transformations and other parameters to the URL.
33+
* An instance method to generate URL for the given transformation parameters. This method is useful when you want to generate URL using the instance of the SDK without passing common parameters like `urlEndpoint` and `transformationPosition` every time.
5834
*/
59-
url(urlOptions: UrlOptions): string {
60-
return url(urlOptions, this.options);
35+
url(urlOptions: UrlOptions & Partial<Pick<ImageKitOptions, "urlEndpoint" | "transformationPosition">>): string {
36+
// Merge the options with the instance options
37+
const options = {
38+
...this.options,
39+
...urlOptions,
40+
};
41+
return ImageKit.url(options);
6142
}
6243

6344
/**
64-
* For uploading files directly from the browser to ImageKit.io.
65-
*
66-
* {@link https://imagekit.io/docs/api-reference/upload-file/upload-file#how-to-implement-client-side-file-upload}
45+
* A static method to generate URL for the given transformation parameters. This method is useful when you want to generate URL without creating an instance of the SDK.
6746
*/
68-
upload(uploadOptions: UploadOptions, options?: Partial<ImageKitOptions>): Promise<IKResponse<UploadResponse>>
69-
upload(uploadOptions: UploadOptions, callback: (err: Error | null, response: IKResponse<UploadResponse> | null) => void, options?: Partial<ImageKitOptions>): void;
70-
upload(uploadOptions: UploadOptions, callbackOrOptions?: ((err: Error | null, response: IKResponse<UploadResponse> | null) => void) | Partial<ImageKitOptions>, options?: Partial<ImageKitOptions>): void | Promise<IKResponse<UploadResponse>> {
71-
let callback;
72-
if (typeof callbackOrOptions === 'function') {
73-
callback = callbackOrOptions;
74-
} else {
75-
options = callbackOrOptions || {};
47+
static url(urlOptions: UrlOptions & Required<Pick<ImageKitOptions, "urlEndpoint">> & Pick<ImageKitOptions, "transformationPosition">): string {
48+
const options = urlOptions;
49+
if (!options.urlEndpoint || options.urlEndpoint.length === 0) {
50+
throw {
51+
message: "urlEndpoint is required",
52+
}
7653
}
77-
if (!uploadOptions || typeof uploadOptions !== "object") {
78-
return respond(true, errorMessages.INVALID_UPLOAD_OPTIONS, callback);
54+
if (!options.src || options.src.length === 0) {
55+
throw {
56+
message: "src is required",
57+
}
7958
}
80-
var mergedOptions = {
59+
return buildURL(options);
60+
}
61+
62+
/**
63+
* An instance method to upload file to ImageKit.io. This method is useful when you want to upload file using the instance of the SDK without passing common parameters like `urlEndpoint` and `publicKey` every time.
64+
*/
65+
upload(uploadOptions: UploadOptions & Partial<Pick<ImageKitOptions, "publicKey">>): Promise<IKResponse<UploadResponse>> {
66+
// Merge the options with the instance options
67+
const options = {
8168
...this.options,
82-
...options,
69+
...uploadOptions,
8370
};
71+
72+
return ImageKit.upload(options as UploadOptions & Required<Pick<ImageKitOptions, "publicKey">>);
73+
}
74+
75+
/**
76+
* A static method to upload file to ImageKit.io. This method is useful when you want to upload file without creating an instance of the SDK.
77+
*/
78+
static upload(uploadOptions: UploadOptions & Required<Pick<ImageKitOptions, "publicKey">>): Promise<IKResponse<UploadResponse>> {
79+
if (!uploadOptions.publicKey || uploadOptions.publicKey.length === 0) {
80+
throw errorMessages.MISSING_PUBLIC_KEY;
81+
}
82+
8483
const { xhr: userProvidedXHR } = uploadOptions || {};
8584
delete uploadOptions.xhr;
8685
const xhr = userProvidedXHR || new XMLHttpRequest();
87-
return promisify<IKResponse<UploadResponse>>(this, upload)(xhr, uploadOptions, mergedOptions, callback);
86+
87+
// Extract publicKey from uploadOptions
88+
const { publicKey, ...rest } = uploadOptions;
89+
return upload(
90+
xhr,
91+
rest,
92+
{
93+
publicKey,
94+
}
95+
)
8896
}
8997
}
9098

91-
export default ImageKit;
99+
export default ImageKit;

src/interfaces/ImageKitOptions.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
import { TransformationPosition } from ".";
22

33
export interface ImageKitOptions {
4-
urlEndpoint: string;
4+
/**
5+
* Get your urlEndpoint from the [ImageKit dashboard](https://imagekit.io/dashboard/url-endpoints).
6+
*/
7+
urlEndpoint?: string;
8+
9+
/**
10+
* The public API key of your ImageKit account. You can find it in the [ImageKit dashboard](https://imagekit.io/dashboard/developer/api-keys).
11+
*/
512
publicKey?: string;
13+
14+
/**
15+
* By default, the transformation string is added as a query parameter in the URL e.g. `?tr=w-100,h-100`. If you want to add the transformation string in the path of the URL, set this to `path`.
16+
*/
617
transformationPosition?: TransformationPosition;
718
}

src/interfaces/UrlOptions.ts

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,22 @@
1-
import { TransformationPosition } from ".";
21
import { Transformation } from "./Transformation";
32

4-
export interface UrlOptionsBase {
3+
export interface UrlOptions {
54
/**
6-
* An array of objects specifying the transformations to be applied in the URL.
7-
* The transformation name and the value should be specified as a key-value pair in each object.
5+
* Accepts relative or absolute path of the resource. If relative path is provided, it is appended to the `urlEndpoint`. If absolute path is provided, `urlEndpoint` is ignored.
6+
*/
7+
src: string;
8+
9+
/**
10+
* An array of objects specifying the transformations to be applied in the URL. If more than one transformation is specified, they are applied in the order they are specified as chained transformations.
811
*
912
* {@link https://imagekit.io/docs/transformations#chained-transformations}
1013
*/
1114
transformation?: Array<Transformation>;
12-
/**
13-
* Default value is path that places the transformation string as a path parameter in the URL.
14-
* Can also be specified as query which adds the transformation string as the query parameter tr in the URL.
15-
* If you use src parameter to create the URL, then the transformation string is always added as a query parameter.
16-
*/
17-
transformationPosition?: TransformationPosition;
15+
1816
/**
1917
* These are the other query parameters that you want to add to the final URL.
2018
* These can be any query parameters and not necessarily related to ImageKit.
2119
* Especially useful, if you want to add some versioning parameter to your URLs.
2220
*/
2321
queryParameters?: { [key: string]: string | number };
24-
/**
25-
* The base URL to be appended before the path of the image.
26-
* If not specified, the URL Endpoint specified at the time of SDK initialization is used.
27-
*/
28-
urlEndpoint?: string;
29-
}
30-
31-
export interface UrlOptionsSrc extends UrlOptionsBase {
32-
/**
33-
* Conditional. This is the complete URL of an image already mapped to ImageKit.
34-
* For example, https://ik.imagekit.io/your_imagekit_id/endpoint/path/to/image.jpg.
35-
* Either the path or src parameter need to be specified for URL generation.
36-
*/
37-
src: string;
38-
path?: never;
39-
}
40-
41-
export interface UrlOptionsPath extends UrlOptionsBase {
42-
/**
43-
* Conditional. This is the path at which the image exists.
44-
* For example, /path/to/image.jpg. Either the path or src parameter need to be specified for URL generation.
45-
*/
46-
path: string;
47-
src?: never;
48-
}
49-
50-
/**
51-
* Options for generating an URL
52-
*
53-
* {@link https://github.com/imagekit-developer/imagekit-javascript#url-generation}
54-
*/
55-
export type UrlOptions = UrlOptionsSrc | UrlOptionsPath;
22+
}

0 commit comments

Comments
 (0)