Skip to content

Commit 74b3073

Browse files
committed
refactor: update export types and remove unused ImageKitOptions interface
1 parent b15ed76 commit 74b3073

File tree

17 files changed

+1361
-1067
lines changed

17 files changed

+1361
-1067
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@ var imagekit = new ImageKit({
7575
});
7676
```
7777

78-
> Note: Never include your private API key in client-side code. The SDK will throw an error if you do.
79-
8078
### Initialization Options
8179

8280
| Option | Description | Example |

rollup.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ export default [
3333
{
3434
input: "src/index.ts",
3535
output: [
36-
{ file: pkg.main, format: "cjs", exports: "default" },
37-
{ file: pkg.module, format: "es", exports: "default" },
36+
{ file: pkg.main, format: "cjs", exports: "named" },
37+
{ file: pkg.module, format: "es", exports: "named" },
3838
],
3939
plugins: [
4040
nodeResolve({ extensions: [".ts"] }),

src/constants/errorMessages.ts

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
export default {
2-
MANDATORY_INITIALIZATION_MISSING: { message: "Missing urlEndpoint during SDK initialization", help: "" },
3-
INVALID_TRANSFORMATION_POSITION: { message: "Invalid transformationPosition parameter", help: "" },
4-
PRIVATE_KEY_CLIENT_SIDE: { message: "privateKey should not be passed on the client side", help: "" },
5-
MISSING_UPLOAD_DATA: { message: "Missing data for upload", help: "" },
6-
MISSING_UPLOAD_FILE_PARAMETER: { message: "Missing file parameter for upload", help: "" },
7-
MISSING_UPLOAD_FILENAME_PARAMETER: { message: "Missing fileName parameter for upload", help: "" },
8-
MISSING_AUTHENTICATION_ENDPOINT: { message: "Missing authentication endpoint for upload", help: "" },
9-
MISSING_PUBLIC_KEY: { message: "Missing public key for upload", help: "" },
10-
AUTH_ENDPOINT_TIMEOUT: { message: "The authenticationEndpoint you provided timed out in 60 seconds", help: "" },
11-
AUTH_ENDPOINT_NETWORK_ERROR: { message: "Request to authenticationEndpoint failed due to network error", help: "" },
12-
AUTH_INVALID_RESPONSE: { message: "Invalid response from authenticationEndpoint. The SDK expects a JSON response with three fields i.e. signature, token and expire.", help: "" },
2+
MANDATORY_INITIALIZATION_MISSING: { message: "Missing urlEndpoint during SDK initialization" },
3+
INVALID_TRANSFORMATION_POSITION: { message: "Invalid transformationPosition parameter" },
4+
PRIVATE_KEY_CLIENT_SIDE: { message: "privateKey should not be passed on the client side" },
5+
MISSING_UPLOAD_DATA: { message: "Missing data for upload" },
6+
MISSING_UPLOAD_FILE_PARAMETER: { message: "Missing file parameter for upload" },
7+
MISSING_UPLOAD_FILENAME_PARAMETER: { message: "Missing fileName parameter for upload" },
8+
MISSING_AUTHENTICATION_ENDPOINT: { message: "Missing authentication endpoint for upload" },
9+
MISSING_PUBLIC_KEY: { message: "Missing public key for upload" },
10+
AUTH_ENDPOINT_TIMEOUT: { message: "The authenticationEndpoint you provided timed out in 60 seconds" },
11+
AUTH_ENDPOINT_NETWORK_ERROR: { message: "Request to authenticationEndpoint failed due to network error" },
12+
AUTH_INVALID_RESPONSE: { message: "Invalid response from authenticationEndpoint. The SDK expects a JSON response with three fields i.e. signature, token and expire." },
1313
UPLOAD_ENDPOINT_NETWORK_ERROR: {
14-
message: "Request to ImageKit upload endpoint failed due to network error",
15-
help: "",
14+
message: "Request to ImageKit upload endpoint failed due to network error"
1615
},
17-
INVALID_UPLOAD_OPTIONS: { message: "Invalid uploadOptions parameter", help: "" },
18-
MISSING_SIGNATURE: { message: "Missing signature for upload. The SDK expects token, signature and expire for authentication.", help: "" },
19-
MISSING_TOKEN: { message: "Missing token for upload. The SDK expects token, signature and expire for authentication.", help: "" },
20-
MISSING_EXPIRE: { message: "Missing expire for upload. The SDK expects token, signature and expire for authentication.", help: "" },
21-
INVALID_TRANSFORMATION: { message: "Invalid transformation parameter. Please include at least pre, post, or both.", help: "" },
22-
INVALID_PRE_TRANSFORMATION: { message: "Invalid pre transformation parameter.", help: "" },
23-
INVALID_POST_TRANSFORMATION: { message: "Invalid post transformation parameter.", help: "" },
24-
UPLOAD_ABORTED: { message: "Request aborted by the user", help: "" },
16+
INVALID_UPLOAD_OPTIONS: { message: "Invalid uploadOptions parameter" },
17+
MISSING_SIGNATURE: { message: "Missing signature for upload. The SDK expects token, signature and expire for authentication." },
18+
MISSING_TOKEN: { message: "Missing token for upload. The SDK expects token, signature and expire for authentication." },
19+
MISSING_EXPIRE: { message: "Missing expire for upload. The SDK expects token, signature and expire for authentication." },
20+
INVALID_TRANSFORMATION: { message: "Invalid transformation parameter. Please include at least pre, post, or both." },
21+
INVALID_PRE_TRANSFORMATION: { message: "Invalid pre transformation parameter." },
22+
INVALID_POST_TRANSFORMATION: { message: "Invalid post transformation parameter." },
23+
UPLOAD_ABORTED: { message: "Request aborted by the user" },
2524
};

src/index.ts

Lines changed: 8 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,11 @@
1-
import errorMessages from "./constants/errorMessages";
2-
import { ImageKitOptions, UploadOptions, UploadResponse, UrlOptions } from "./interfaces";
3-
import IKResponse from "./interfaces/IKResponse";
1+
import { UploadOptions, UploadResponse, UrlOptions } from "./interfaces";
42
import { upload } from "./upload";
5-
import { buildURL } from "./url/builder";
6-
import transformationUtils from "./utils/transformation";
7-
type MakeRequired<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>;
3+
import { buildURL, generateTransformationString } from "./url";
84

5+
export { buildURL, generateTransformationString, upload };
96

10-
function mandatoryParametersAvailable(options: ImageKitOptions) {
11-
return options.urlEndpoint;
12-
}
13-
14-
class ImageKit {
15-
options: MakeRequired<ImageKitOptions, "urlEndpoint"> = {
16-
publicKey: "",
17-
urlEndpoint: "",
18-
transformationPosition: transformationUtils.getDefault(),
19-
};
20-
21-
constructor(opts: MakeRequired<ImageKitOptions, "urlEndpoint">) {
22-
this.options = { ...this.options, ...(opts || {}) };
23-
if (!mandatoryParametersAvailable(this.options)) {
24-
throw errorMessages.MANDATORY_INITIALIZATION_MISSING;
25-
}
26-
27-
if (!transformationUtils.validParameters(this.options)) {
28-
throw errorMessages.INVALID_TRANSFORMATION_POSITION;
29-
}
30-
}
31-
32-
/**
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.
34-
*/
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);
42-
}
43-
44-
/**
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.
46-
*/
47-
static url(urlOptions: UrlOptions & Required<Pick<ImageKitOptions, "urlEndpoint">> & Pick<ImageKitOptions, "transformationPosition">): string {
48-
return buildURL(urlOptions);
49-
}
50-
51-
/**
52-
* 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.
53-
*/
54-
upload(uploadOptions: UploadOptions & Partial<Pick<ImageKitOptions, "publicKey">>): Promise<IKResponse<UploadResponse>> {
55-
// Merge the options with the instance options
56-
const options = {
57-
...this.options,
58-
...uploadOptions,
59-
};
60-
61-
return ImageKit.upload(options as UploadOptions & Required<Pick<ImageKitOptions, "publicKey">>);
62-
}
63-
64-
/**
65-
* 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.
66-
*/
67-
static upload(uploadOptions: UploadOptions & Required<Pick<ImageKitOptions, "publicKey">>): Promise<IKResponse<UploadResponse>> {
68-
if (!uploadOptions.publicKey || uploadOptions.publicKey.length === 0) {
69-
throw errorMessages.MISSING_PUBLIC_KEY;
70-
}
71-
72-
const { xhr: userProvidedXHR } = uploadOptions || {};
73-
delete uploadOptions.xhr;
74-
const xhr = userProvidedXHR || new XMLHttpRequest();
75-
76-
// Extract publicKey from uploadOptions
77-
const { publicKey, ...rest } = uploadOptions;
78-
return upload(
79-
xhr,
80-
rest,
81-
{
82-
publicKey,
83-
}
84-
)
85-
}
86-
}
87-
88-
export default ImageKit;
7+
export type {
8+
UrlOptions,
9+
UploadOptions,
10+
UploadResponse
11+
};

src/interfaces/ImageKitOptions.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/interfaces/UploadOptions.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ interface AbsObject {
1616

1717
type PostTransformation = TransformationObject | GifToVideoOrThumbnailObject | AbsObject;
1818

19-
interface Transformation{
19+
interface Transformation {
2020
pre?: string
2121
post?: PostTransformation[]
2222
}
@@ -35,28 +35,38 @@ export interface UploadOptions {
3535
* Pass the full URL, for example - https://www.example.com/rest-of-the-image-path.jpg.
3636
*/
3737
file: string | Blob | File;
38+
39+
/**
40+
* The name with which the file has to be uploaded.
41+
* The file name can contain:
42+
* - Alphanumeric Characters: a-z , A-Z , 0-9 (including unicode letters, marks, and numerals in other languages)
43+
* - Special Characters: . _ and -
44+
* Any other character including space will be replaced by _
45+
*/
46+
fileName: string;
47+
3848
/**
3949
* HMAC-SHA1 digest of the token+expire using your ImageKit.io private API key as a key. This should be in lowercase.
4050
* Warning: Signature must be calculated on the server-side. This field is required for authentication when uploading a file from the client-side.
4151
*/
4252
signature: string;
53+
4354
/**
4455
* A unique value generated by the client, which will be used by the ImageKit.io server to recognize and prevent subsequent retries for the same request. We suggest using V4 UUIDs, or another random string with enough entropy to avoid collisions.
4556
* Note: Sending a value that has been used in the past will result in a validation error. Even if your previous request resulted in an error, you should always send a new value for this field.
4657
*/
4758
token: string;
59+
4860
/**
4961
* The time until your signature is valid. It must be a Unix time in less than 1 hour into the future. It should be in seconds.
5062
*/
5163
expire: number;
64+
5265
/**
53-
* The name with which the file has to be uploaded.
54-
* The file name can contain:
55-
* - Alphanumeric Characters: a-z , A-Z , 0-9 (including unicode letters, marks, and numerals in other languages)
56-
* - Special Characters: . _ and -
57-
* Any other character including space will be replaced by _
66+
* The public API key of your ImageKit account. You can find it in the [ImageKit dashboard](https://imagekit.io/dashboard/developer/api-keys).
5867
*/
59-
fileName: string;
68+
publicKey: string;
69+
6070
/**
6171
* Whether to use a unique filename for this file or not.
6272
* - Accepts true or false.

src/interfaces/UploadResponse.ts

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -94,60 +94,66 @@ export interface Metadata {
9494
};
9595
}
9696

97+
export interface ResponseMetadata {
98+
statusCode: number;
99+
requestId: string;
100+
headers: Record<string, string | number | boolean>;
101+
}
102+
97103
/**
98-
* Response from uploading a file
104+
* Response from server when file is uploaded successfully.
99105
*
100106
* {@link https://imagekit.io/docs/api-reference/upload-file/upload-file#Responses}
101107
*/
102108
export interface UploadResponse {
103109
/**
104110
* Unique fileId. Store this fileld in your database, as this will be used to perform update action on this file.
105111
*/
106-
fileId: string;
112+
fileId?: string;
107113
/**
108114
* The name of the uploaded file.
109115
*/
110-
name: string;
116+
name?: string;
111117
/**
112118
* The URL of the file.
113119
*/
114-
url: string;
120+
url?: string;
115121
/**
116122
* In case of an image, a small thumbnail URL.
117123
*/
118-
thumbnailUrl: string;
124+
thumbnailUrl?: string;
119125
/**
120126
* Height of the uploaded image file. Only applicable when file type is image.
121127
*/
122-
height: number;
128+
height?: number;
123129
/**
124130
* Width of the uploaded image file. Only applicable when file type is image.
125131
*/
126-
width: number;
132+
width?: number;
127133
/**
128134
* Size of the uploaded file in bytes.
129135
*/
130-
size: number;
136+
size?: number;
131137
/**
132138
* Type of file. It can either be image or non-image.
133139
*/
134-
fileType: FileType;
140+
fileType?: FileType;
135141
/**
136142
* The path of the file uploaded. It includes any folder that you specified while uploading.
137143
*/
138-
filePath: string;
144+
filePath?: string;
139145
/**
140146
* Array of tags associated with the image.
141147
*/
142148
tags?: string[];
143149
/**
144150
* Is the file marked as private. It can be either true or false.
145151
*/
146-
isPrivateFile: boolean;
152+
isPrivateFile?: boolean;
147153
/**
148154
* Value of custom coordinates associated with the image in format x,y,width,height.
149155
*/
150-
customCoordinates: string | null;
156+
customCoordinates?: string | null;
151157
/**
152158
* The metadata of the upload file. Use responseFields property in request to get the metadata returned in response of upload API.
153159
*/
@@ -156,8 +162,21 @@ export interface UploadResponse {
156162
* AITags field is populated only because the google-auto-tagging extension was executed synchronously and it received a successresponse.
157163
*/
158164
AITags?: object[];
165+
159166
/*
160167
* Field object which will contain the status of each extension at the time of completion of the update/upload request.
161168
*/
162169
extensionStatus?: { [key: string]: string }
170+
171+
/**
172+
* Message indicating that the file upload is accepted. This field is only present when the upload is accepted but not yet processed.
173+
* This can happen when the file is being processed for pre-transformation for video.
174+
* The upload will be completed once the pre-transformation is done.
175+
*/
176+
message?: string
177+
178+
/**
179+
* Response metadata for debugging purposes.
180+
*/
181+
readonly $ResponseMetadata: ResponseMetadata;
163182
}

src/interfaces/UrlOptions.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import { Transformation } from "./Transformation";
2+
import { TransformationPosition } from ".";
23

34
export interface UrlOptions {
45
/**
56
* 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.
67
*/
78
src: string;
89

10+
/**
11+
* Get your urlEndpoint from the [ImageKit dashboard](https://imagekit.io/dashboard/url-endpoints).
12+
*/
13+
urlEndpoint: string;
14+
915
/**
1016
* 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.
1117
*
@@ -19,4 +25,9 @@ export interface UrlOptions {
1925
* Especially useful, if you want to add some versioning parameter to your URLs.
2026
*/
2127
queryParameters?: { [key: string]: string | number };
28+
29+
/**
30+
* 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`.
31+
*/
32+
transformationPosition?: TransformationPosition;
2233
}

src/interfaces/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { ImageKitOptions } from "./ImageKitOptions";
21
import { TransformationPosition } from "./Transformation";
32
import { UploadOptions } from "./UploadOptions";
4-
import { UploadResponse, FileType } from "./UploadResponse";
3+
import { FileType, UploadResponse } from "./UploadResponse";
54
import { UrlOptions } from "./UrlOptions";
65

7-
export type { ImageKitOptions, TransformationPosition, UploadOptions, UploadResponse, FileType, UrlOptions };
6+
export type { FileType, TransformationPosition, UploadOptions, UploadResponse, UrlOptions };
7+

0 commit comments

Comments
 (0)