@imagekit/javascript 5.1.0-beta.1 → 5.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -3
- package/dist/imagekit.cjs.js +236 -228
- package/dist/imagekit.esm.js +236 -228
- package/dist/imagekit.min.js +1 -1
- package/dist/index.d.ts +4 -5
- package/dist/interfaces/UploadOptions.d.ts +205 -102
- package/dist/interfaces/UploadResponse.d.ts +363 -122
- package/dist/interfaces/index.d.ts +1 -2
- package/dist/interfaces/shared.d.ts +1475 -0
- package/dist/responsive.d.ts +1 -53
- package/dist/url.d.ts +1 -2
- package/package.json +1 -1
- package/dist/interfaces/SrcOptions.d.ts +0 -32
- package/dist/interfaces/Transformation.d.ts +0 -637
package/README.md
CHANGED
|
@@ -20,16 +20,27 @@ You can install the SDK in your project using npm or yarn.
|
|
|
20
20
|
npm install @imagekit/javascript
|
|
21
21
|
```
|
|
22
22
|
|
|
23
|
+
## Documentation
|
|
24
|
+
|
|
25
|
+
Refer to the ImageKit [official documentation](https://imagekit.io/docs/integration/javascript) for more details on how to use the SDK.
|
|
26
|
+
|
|
23
27
|
## TypeScript support
|
|
24
28
|
|
|
25
29
|
The SDK is written in TypeScript, offering first-class TypeScript support. Enjoy excellent type safety and IntelliSense in your IDE. You can use it in your TypeScript projects without any additional configuration.
|
|
26
30
|
|
|
27
|
-
|
|
28
31
|
To enable type checking in JavaScript projects, add `//@ts-check` at the top of your JavaScript files. This will activate type checking in your IDE.
|
|
29
32
|
|
|
30
|
-
|
|
33
|
+
### TypeScript and the SDK versioning policy
|
|
31
34
|
|
|
32
|
-
|
|
35
|
+
The TypeScript types in this SDK always reflect the latest shape of the ImageKit API. When we make improvements to the type definitions to better reflect the actual runtime behavior, we may release these changes in minor or patch versions. While these changes align types more closely with reality and are not breaking changes at runtime, they might cause new type errors when you upgrade.
|
|
36
|
+
|
|
37
|
+
We judge this approach to be better than the alternatives: outdated, inaccurate types, or vastly more frequent major releases that would distract from any truly breaking runtime changes. If you encounter type errors after upgrading, you can resolve them by:
|
|
38
|
+
|
|
39
|
+
- Adding appropriate type guards or assertions
|
|
40
|
+
- Updating your code to match the corrected types
|
|
41
|
+
- Using `// @ts-ignore` temporarily if you need more time to adjust
|
|
42
|
+
|
|
43
|
+
Please feel welcome to share your thoughts about the versioning policy in a GitHub issue.
|
|
33
44
|
|
|
34
45
|
## Changelog
|
|
35
46
|
|
package/dist/imagekit.cjs.js
CHANGED
|
@@ -2,234 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var errorMessages = {
|
|
6
|
-
MISSING_UPLOAD_FILE_PARAMETER: {
|
|
7
|
-
message: "Missing file parameter for upload"
|
|
8
|
-
},
|
|
9
|
-
MISSING_UPLOAD_FILENAME_PARAMETER: {
|
|
10
|
-
message: "Missing fileName parameter for upload"
|
|
11
|
-
},
|
|
12
|
-
MISSING_PUBLIC_KEY: {
|
|
13
|
-
message: "Missing public key for upload"
|
|
14
|
-
},
|
|
15
|
-
UPLOAD_ENDPOINT_NETWORK_ERROR: {
|
|
16
|
-
message: "Request to ImageKit upload endpoint failed due to network error"
|
|
17
|
-
},
|
|
18
|
-
MISSING_SIGNATURE: {
|
|
19
|
-
message: "Missing signature for upload. The SDK expects token, signature and expire for authentication."
|
|
20
|
-
},
|
|
21
|
-
MISSING_TOKEN: {
|
|
22
|
-
message: "Missing token for upload. The SDK expects token, signature and expire for authentication."
|
|
23
|
-
},
|
|
24
|
-
MISSING_EXPIRE: {
|
|
25
|
-
message: "Missing expire for upload. The SDK expects token, signature and expire for authentication."
|
|
26
|
-
},
|
|
27
|
-
INVALID_TRANSFORMATION: {
|
|
28
|
-
message: "Invalid transformation parameter. Please include at least pre, post, or both."
|
|
29
|
-
},
|
|
30
|
-
INVALID_PRE_TRANSFORMATION: {
|
|
31
|
-
message: "Invalid pre transformation parameter."
|
|
32
|
-
},
|
|
33
|
-
INVALID_POST_TRANSFORMATION: {
|
|
34
|
-
message: "Invalid post transformation parameter."
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
class ImageKitInvalidRequestError extends Error {
|
|
39
|
-
constructor(message, responseMetadata) {
|
|
40
|
-
super(message);
|
|
41
|
-
this.$ResponseMetadata = void 0;
|
|
42
|
-
this.name = "ImageKitInvalidRequestError";
|
|
43
|
-
this.$ResponseMetadata = responseMetadata;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
class ImageKitAbortError extends Error {
|
|
47
|
-
constructor(message, reason) {
|
|
48
|
-
super(message);
|
|
49
|
-
this.reason = void 0;
|
|
50
|
-
this.name = "ImageKitAbortError";
|
|
51
|
-
this.reason = reason;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
class ImageKitUploadNetworkError extends Error {
|
|
55
|
-
constructor(message) {
|
|
56
|
-
super(message);
|
|
57
|
-
this.name = "ImageKitUploadNetworkError";
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
class ImageKitServerError extends Error {
|
|
61
|
-
constructor(message, responseMetadata) {
|
|
62
|
-
super(message);
|
|
63
|
-
this.$ResponseMetadata = void 0;
|
|
64
|
-
this.name = "ImageKitServerError";
|
|
65
|
-
this.$ResponseMetadata = responseMetadata;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
const upload = uploadOptions => {
|
|
69
|
-
if (!uploadOptions) {
|
|
70
|
-
return Promise.reject(new ImageKitInvalidRequestError("Invalid options provided for upload"));
|
|
71
|
-
}
|
|
72
|
-
return new Promise((resolve, reject) => {
|
|
73
|
-
const {
|
|
74
|
-
xhr: userProvidedXHR
|
|
75
|
-
} = uploadOptions || {};
|
|
76
|
-
delete uploadOptions.xhr;
|
|
77
|
-
const xhr = userProvidedXHR || new XMLHttpRequest();
|
|
78
|
-
if (!uploadOptions.file) {
|
|
79
|
-
return reject(new ImageKitInvalidRequestError(errorMessages.MISSING_UPLOAD_FILE_PARAMETER.message));
|
|
80
|
-
}
|
|
81
|
-
if (!uploadOptions.fileName) {
|
|
82
|
-
return reject(new ImageKitInvalidRequestError(errorMessages.MISSING_UPLOAD_FILENAME_PARAMETER.message));
|
|
83
|
-
}
|
|
84
|
-
if (!uploadOptions.publicKey || uploadOptions.publicKey.length === 0) {
|
|
85
|
-
return reject(new ImageKitInvalidRequestError(errorMessages.MISSING_PUBLIC_KEY.message));
|
|
86
|
-
}
|
|
87
|
-
if (!uploadOptions.token) {
|
|
88
|
-
return reject(new ImageKitInvalidRequestError(errorMessages.MISSING_TOKEN.message));
|
|
89
|
-
}
|
|
90
|
-
if (!uploadOptions.signature) {
|
|
91
|
-
return reject(new ImageKitInvalidRequestError(errorMessages.MISSING_SIGNATURE.message));
|
|
92
|
-
}
|
|
93
|
-
if (!uploadOptions.expire) {
|
|
94
|
-
return reject(new ImageKitInvalidRequestError(errorMessages.MISSING_EXPIRE.message));
|
|
95
|
-
}
|
|
96
|
-
if (uploadOptions.transformation) {
|
|
97
|
-
if (!(Object.keys(uploadOptions.transformation).includes("pre") || Object.keys(uploadOptions.transformation).includes("post"))) {
|
|
98
|
-
return reject(new ImageKitInvalidRequestError(errorMessages.INVALID_TRANSFORMATION.message));
|
|
99
|
-
}
|
|
100
|
-
if (Object.keys(uploadOptions.transformation).includes("pre") && !uploadOptions.transformation.pre) {
|
|
101
|
-
return reject(new ImageKitInvalidRequestError(errorMessages.INVALID_PRE_TRANSFORMATION.message));
|
|
102
|
-
}
|
|
103
|
-
if (Object.keys(uploadOptions.transformation).includes("post")) {
|
|
104
|
-
if (Array.isArray(uploadOptions.transformation.post)) {
|
|
105
|
-
for (let transformation of uploadOptions.transformation.post) {
|
|
106
|
-
if (transformation.type === "abs" && !(transformation.protocol || transformation.value)) {
|
|
107
|
-
return reject(new ImageKitInvalidRequestError(errorMessages.INVALID_POST_TRANSFORMATION.message));
|
|
108
|
-
} else if (transformation.type === "transformation" && !transformation.value) {
|
|
109
|
-
return reject(new ImageKitInvalidRequestError(errorMessages.INVALID_POST_TRANSFORMATION.message));
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
} else {
|
|
113
|
-
return reject(new ImageKitInvalidRequestError(errorMessages.INVALID_POST_TRANSFORMATION.message));
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
var formData = new FormData();
|
|
118
|
-
let key;
|
|
119
|
-
for (key in uploadOptions) {
|
|
120
|
-
if (key) {
|
|
121
|
-
if (key === "file" && typeof uploadOptions.file != "string") {
|
|
122
|
-
formData.set('file', uploadOptions.file, String(uploadOptions.fileName));
|
|
123
|
-
} else if (key === "tags" && Array.isArray(uploadOptions.tags)) {
|
|
124
|
-
formData.set('tags', uploadOptions.tags.join(","));
|
|
125
|
-
} else if (key === 'signature') {
|
|
126
|
-
formData.set("signature", uploadOptions.signature);
|
|
127
|
-
} else if (key === 'expire') {
|
|
128
|
-
formData.set("expire", String(uploadOptions.expire));
|
|
129
|
-
} else if (key === 'token') {
|
|
130
|
-
formData.set("token", uploadOptions.token);
|
|
131
|
-
} else if (key === "responseFields" && Array.isArray(uploadOptions.responseFields)) {
|
|
132
|
-
formData.set('responseFields', uploadOptions.responseFields.join(","));
|
|
133
|
-
} else if (key === "extensions" && Array.isArray(uploadOptions.extensions)) {
|
|
134
|
-
formData.set('extensions', JSON.stringify(uploadOptions.extensions));
|
|
135
|
-
} else if (key === "customMetadata" && typeof uploadOptions.customMetadata === "object" && !Array.isArray(uploadOptions.customMetadata) && uploadOptions.customMetadata !== null) {
|
|
136
|
-
formData.set('customMetadata', JSON.stringify(uploadOptions.customMetadata));
|
|
137
|
-
} else if (key === "transformation" && typeof uploadOptions.transformation === "object" && uploadOptions.transformation !== null) {
|
|
138
|
-
formData.set(key, JSON.stringify(uploadOptions.transformation));
|
|
139
|
-
} else if (key === 'checks' && uploadOptions.checks) {
|
|
140
|
-
formData.set("checks", uploadOptions.checks);
|
|
141
|
-
} else if (uploadOptions[key] !== undefined) {
|
|
142
|
-
if (["onProgress", "abortSignal"].includes(key)) continue;
|
|
143
|
-
formData.set(key, String(uploadOptions[key]));
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
if (uploadOptions.onProgress) {
|
|
148
|
-
xhr.upload.onprogress = function (event) {
|
|
149
|
-
if (uploadOptions.onProgress) uploadOptions.onProgress(event);
|
|
150
|
-
};
|
|
151
|
-
}
|
|
152
|
-
function onAbortHandler() {
|
|
153
|
-
var _uploadOptions$abortS;
|
|
154
|
-
xhr.abort();
|
|
155
|
-
return reject(new ImageKitAbortError("Upload aborted", (_uploadOptions$abortS = uploadOptions.abortSignal) === null || _uploadOptions$abortS === void 0 ? void 0 : _uploadOptions$abortS.reason));
|
|
156
|
-
}
|
|
157
|
-
if (uploadOptions.abortSignal) {
|
|
158
|
-
if (uploadOptions.abortSignal.aborted) {
|
|
159
|
-
var _uploadOptions$abortS2;
|
|
160
|
-
return reject(new ImageKitAbortError("Upload aborted", (_uploadOptions$abortS2 = uploadOptions.abortSignal) === null || _uploadOptions$abortS2 === void 0 ? void 0 : _uploadOptions$abortS2.reason));
|
|
161
|
-
}
|
|
162
|
-
uploadOptions.abortSignal.addEventListener("abort", onAbortHandler);
|
|
163
|
-
xhr.addEventListener("loadend", () => {
|
|
164
|
-
if (uploadOptions.abortSignal) {
|
|
165
|
-
uploadOptions.abortSignal.removeEventListener("abort", onAbortHandler);
|
|
166
|
-
}
|
|
167
|
-
});
|
|
168
|
-
}
|
|
169
|
-
xhr.open('POST', 'https://upload.imagekit.io/api/v1/files/upload');
|
|
170
|
-
xhr.onerror = function (e) {
|
|
171
|
-
return reject(new ImageKitUploadNetworkError(errorMessages.UPLOAD_ENDPOINT_NETWORK_ERROR.message));
|
|
172
|
-
};
|
|
173
|
-
xhr.onload = function () {
|
|
174
|
-
if (xhr.status >= 200 && xhr.status < 300) {
|
|
175
|
-
try {
|
|
176
|
-
var body = JSON.parse(xhr.responseText);
|
|
177
|
-
var uploadResponse = addResponseHeadersAndBody(body, xhr);
|
|
178
|
-
return resolve(uploadResponse);
|
|
179
|
-
} catch (ex) {
|
|
180
|
-
return reject(ex);
|
|
181
|
-
}
|
|
182
|
-
} else if (xhr.status >= 400 && xhr.status < 500) {
|
|
183
|
-
try {
|
|
184
|
-
var body = JSON.parse(xhr.responseText);
|
|
185
|
-
return reject(new ImageKitInvalidRequestError(body.message ?? "Invalid request. Please check the parameters.", getResponseMetadata(xhr)));
|
|
186
|
-
} catch (ex) {
|
|
187
|
-
return reject(ex);
|
|
188
|
-
}
|
|
189
|
-
} else {
|
|
190
|
-
try {
|
|
191
|
-
var body = JSON.parse(xhr.responseText);
|
|
192
|
-
return reject(new ImageKitServerError(body.message ?? "Server error occurred while uploading the file. This is rare and usually temporary.", getResponseMetadata(xhr)));
|
|
193
|
-
} catch (ex) {
|
|
194
|
-
return reject(new ImageKitServerError("Server error occurred while uploading the file. This is rare and usually temporary.", getResponseMetadata(xhr)));
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
};
|
|
198
|
-
xhr.send(formData);
|
|
199
|
-
});
|
|
200
|
-
};
|
|
201
|
-
const addResponseHeadersAndBody = (body, xhr) => {
|
|
202
|
-
let response = {
|
|
203
|
-
...body
|
|
204
|
-
};
|
|
205
|
-
const responseMetadata = getResponseMetadata(xhr);
|
|
206
|
-
Object.defineProperty(response, "$ResponseMetadata", {
|
|
207
|
-
value: responseMetadata,
|
|
208
|
-
enumerable: false,
|
|
209
|
-
writable: false
|
|
210
|
-
});
|
|
211
|
-
return response;
|
|
212
|
-
};
|
|
213
|
-
const getResponseMetadata = xhr => {
|
|
214
|
-
const headers = getResponseHeaderMap(xhr);
|
|
215
|
-
const responseMetadata = {
|
|
216
|
-
statusCode: xhr.status,
|
|
217
|
-
headers: headers,
|
|
218
|
-
requestId: headers["x-request-id"]
|
|
219
|
-
};
|
|
220
|
-
return responseMetadata;
|
|
221
|
-
};
|
|
222
|
-
function getResponseHeaderMap(xhr) {
|
|
223
|
-
const headers = {};
|
|
224
|
-
const responseHeaders = xhr.getAllResponseHeaders();
|
|
225
|
-
if (Object.keys(responseHeaders).length) {
|
|
226
|
-
responseHeaders.trim().split(/[\r\n]+/).map(value => value.split(/: /)).forEach(keyValue => {
|
|
227
|
-
headers[keyValue[0].trim().toLowerCase()] = keyValue[1].trim();
|
|
228
|
-
});
|
|
229
|
-
}
|
|
230
|
-
return headers;
|
|
231
|
-
}
|
|
232
|
-
|
|
233
5
|
const supportedTransforms = {
|
|
234
6
|
width: "w",
|
|
235
7
|
height: "h",
|
|
@@ -265,6 +37,7 @@ const supportedTransforms = {
|
|
|
265
37
|
aiVariation: "e-genvar",
|
|
266
38
|
aiDropShadow: "e-dropshadow",
|
|
267
39
|
aiChangeBackground: "e-changebg",
|
|
40
|
+
aiEdit: "e-edit",
|
|
268
41
|
aiRemoveBackground: "e-bgremove",
|
|
269
42
|
aiRemoveBackgroundExternal: "e-removedotbg",
|
|
270
43
|
contrastStretch: "e-contrast",
|
|
@@ -272,6 +45,8 @@ const supportedTransforms = {
|
|
|
272
45
|
sharpen: "e-sharpen",
|
|
273
46
|
unsharpMask: "e-usm",
|
|
274
47
|
gradient: "e-gradient",
|
|
48
|
+
colorReplace: "cr",
|
|
49
|
+
distort: "e-distort",
|
|
275
50
|
progressive: "pr",
|
|
276
51
|
lossless: "lo",
|
|
277
52
|
colorProfile: "cp",
|
|
@@ -280,6 +55,7 @@ const supportedTransforms = {
|
|
|
280
55
|
trim: "t",
|
|
281
56
|
zoom: "z",
|
|
282
57
|
page: "pg",
|
|
58
|
+
layerMode: "lm",
|
|
283
59
|
fontSize: "fs",
|
|
284
60
|
fontFamily: "ff",
|
|
285
61
|
fontColor: "co",
|
|
@@ -421,6 +197,7 @@ function processOverlay(overlay) {
|
|
|
421
197
|
const entries = [];
|
|
422
198
|
const {
|
|
423
199
|
type,
|
|
200
|
+
layerMode,
|
|
424
201
|
position = {},
|
|
425
202
|
timing = {},
|
|
426
203
|
transformation = []
|
|
@@ -489,6 +266,9 @@ function processOverlay(overlay) {
|
|
|
489
266
|
}
|
|
490
267
|
break;
|
|
491
268
|
}
|
|
269
|
+
if (layerMode) {
|
|
270
|
+
entries.push(`lm-${layerMode}`);
|
|
271
|
+
}
|
|
492
272
|
const {
|
|
493
273
|
x,
|
|
494
274
|
y,
|
|
@@ -666,6 +446,234 @@ function computeCandidateWidths(params) {
|
|
|
666
446
|
};
|
|
667
447
|
}
|
|
668
448
|
|
|
449
|
+
var errorMessages = {
|
|
450
|
+
MISSING_UPLOAD_FILE_PARAMETER: {
|
|
451
|
+
message: "Missing file parameter for upload"
|
|
452
|
+
},
|
|
453
|
+
MISSING_UPLOAD_FILENAME_PARAMETER: {
|
|
454
|
+
message: "Missing fileName parameter for upload"
|
|
455
|
+
},
|
|
456
|
+
MISSING_PUBLIC_KEY: {
|
|
457
|
+
message: "Missing public key for upload"
|
|
458
|
+
},
|
|
459
|
+
UPLOAD_ENDPOINT_NETWORK_ERROR: {
|
|
460
|
+
message: "Request to ImageKit upload endpoint failed due to network error"
|
|
461
|
+
},
|
|
462
|
+
MISSING_SIGNATURE: {
|
|
463
|
+
message: "Missing signature for upload. The SDK expects token, signature and expire for authentication."
|
|
464
|
+
},
|
|
465
|
+
MISSING_TOKEN: {
|
|
466
|
+
message: "Missing token for upload. The SDK expects token, signature and expire for authentication."
|
|
467
|
+
},
|
|
468
|
+
MISSING_EXPIRE: {
|
|
469
|
+
message: "Missing expire for upload. The SDK expects token, signature and expire for authentication."
|
|
470
|
+
},
|
|
471
|
+
INVALID_TRANSFORMATION: {
|
|
472
|
+
message: "Invalid transformation parameter. Please include at least pre, post, or both."
|
|
473
|
+
},
|
|
474
|
+
INVALID_PRE_TRANSFORMATION: {
|
|
475
|
+
message: "Invalid pre transformation parameter."
|
|
476
|
+
},
|
|
477
|
+
INVALID_POST_TRANSFORMATION: {
|
|
478
|
+
message: "Invalid post transformation parameter."
|
|
479
|
+
}
|
|
480
|
+
};
|
|
481
|
+
|
|
482
|
+
class ImageKitInvalidRequestError extends Error {
|
|
483
|
+
constructor(message, responseMetadata) {
|
|
484
|
+
super(message);
|
|
485
|
+
this.$ResponseMetadata = void 0;
|
|
486
|
+
this.name = "ImageKitInvalidRequestError";
|
|
487
|
+
this.$ResponseMetadata = responseMetadata;
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
class ImageKitAbortError extends Error {
|
|
491
|
+
constructor(message, reason) {
|
|
492
|
+
super(message);
|
|
493
|
+
this.reason = void 0;
|
|
494
|
+
this.name = "ImageKitAbortError";
|
|
495
|
+
this.reason = reason;
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
class ImageKitUploadNetworkError extends Error {
|
|
499
|
+
constructor(message) {
|
|
500
|
+
super(message);
|
|
501
|
+
this.name = "ImageKitUploadNetworkError";
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
class ImageKitServerError extends Error {
|
|
505
|
+
constructor(message, responseMetadata) {
|
|
506
|
+
super(message);
|
|
507
|
+
this.$ResponseMetadata = void 0;
|
|
508
|
+
this.name = "ImageKitServerError";
|
|
509
|
+
this.$ResponseMetadata = responseMetadata;
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
const upload = uploadOptions => {
|
|
513
|
+
if (!uploadOptions) {
|
|
514
|
+
return Promise.reject(new ImageKitInvalidRequestError("Invalid options provided for upload"));
|
|
515
|
+
}
|
|
516
|
+
return new Promise((resolve, reject) => {
|
|
517
|
+
const {
|
|
518
|
+
xhr: userProvidedXHR
|
|
519
|
+
} = uploadOptions || {};
|
|
520
|
+
delete uploadOptions.xhr;
|
|
521
|
+
const xhr = userProvidedXHR || new XMLHttpRequest();
|
|
522
|
+
if (!uploadOptions.file) {
|
|
523
|
+
return reject(new ImageKitInvalidRequestError(errorMessages.MISSING_UPLOAD_FILE_PARAMETER.message));
|
|
524
|
+
}
|
|
525
|
+
if (!uploadOptions.fileName) {
|
|
526
|
+
return reject(new ImageKitInvalidRequestError(errorMessages.MISSING_UPLOAD_FILENAME_PARAMETER.message));
|
|
527
|
+
}
|
|
528
|
+
if (!uploadOptions.publicKey || uploadOptions.publicKey.length === 0) {
|
|
529
|
+
return reject(new ImageKitInvalidRequestError(errorMessages.MISSING_PUBLIC_KEY.message));
|
|
530
|
+
}
|
|
531
|
+
if (!uploadOptions.token) {
|
|
532
|
+
return reject(new ImageKitInvalidRequestError(errorMessages.MISSING_TOKEN.message));
|
|
533
|
+
}
|
|
534
|
+
if (!uploadOptions.signature) {
|
|
535
|
+
return reject(new ImageKitInvalidRequestError(errorMessages.MISSING_SIGNATURE.message));
|
|
536
|
+
}
|
|
537
|
+
if (!uploadOptions.expire) {
|
|
538
|
+
return reject(new ImageKitInvalidRequestError(errorMessages.MISSING_EXPIRE.message));
|
|
539
|
+
}
|
|
540
|
+
if (uploadOptions.transformation) {
|
|
541
|
+
if (!(Object.keys(uploadOptions.transformation).includes("pre") || Object.keys(uploadOptions.transformation).includes("post"))) {
|
|
542
|
+
return reject(new ImageKitInvalidRequestError(errorMessages.INVALID_TRANSFORMATION.message));
|
|
543
|
+
}
|
|
544
|
+
if (Object.keys(uploadOptions.transformation).includes("pre") && !uploadOptions.transformation.pre) {
|
|
545
|
+
return reject(new ImageKitInvalidRequestError(errorMessages.INVALID_PRE_TRANSFORMATION.message));
|
|
546
|
+
}
|
|
547
|
+
if (Object.keys(uploadOptions.transformation).includes("post")) {
|
|
548
|
+
if (Array.isArray(uploadOptions.transformation.post)) {
|
|
549
|
+
for (let transformation of uploadOptions.transformation.post) {
|
|
550
|
+
if (transformation.type === "abs" && !(transformation.protocol || transformation.value)) {
|
|
551
|
+
return reject(new ImageKitInvalidRequestError(errorMessages.INVALID_POST_TRANSFORMATION.message));
|
|
552
|
+
} else if (transformation.type === "transformation" && !transformation.value) {
|
|
553
|
+
return reject(new ImageKitInvalidRequestError(errorMessages.INVALID_POST_TRANSFORMATION.message));
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
} else {
|
|
557
|
+
return reject(new ImageKitInvalidRequestError(errorMessages.INVALID_POST_TRANSFORMATION.message));
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
var formData = new FormData();
|
|
562
|
+
let key;
|
|
563
|
+
for (key in uploadOptions) {
|
|
564
|
+
if (key) {
|
|
565
|
+
if (key === "file" && typeof uploadOptions.file != "string") {
|
|
566
|
+
formData.set('file', uploadOptions.file, String(uploadOptions.fileName));
|
|
567
|
+
} else if (key === "tags" && Array.isArray(uploadOptions.tags)) {
|
|
568
|
+
formData.set('tags', uploadOptions.tags.join(","));
|
|
569
|
+
} else if (key === 'signature') {
|
|
570
|
+
formData.set("signature", uploadOptions.signature);
|
|
571
|
+
} else if (key === 'expire') {
|
|
572
|
+
formData.set("expire", String(uploadOptions.expire));
|
|
573
|
+
} else if (key === 'token') {
|
|
574
|
+
formData.set("token", uploadOptions.token);
|
|
575
|
+
} else if (key === "responseFields" && Array.isArray(uploadOptions.responseFields)) {
|
|
576
|
+
formData.set('responseFields', uploadOptions.responseFields.join(","));
|
|
577
|
+
} else if (key === "extensions" && Array.isArray(uploadOptions.extensions)) {
|
|
578
|
+
formData.set('extensions', JSON.stringify(uploadOptions.extensions));
|
|
579
|
+
} else if (key === "customMetadata" && typeof uploadOptions.customMetadata === "object" && !Array.isArray(uploadOptions.customMetadata) && uploadOptions.customMetadata !== null) {
|
|
580
|
+
formData.set('customMetadata', JSON.stringify(uploadOptions.customMetadata));
|
|
581
|
+
} else if (key === "transformation" && typeof uploadOptions.transformation === "object" && uploadOptions.transformation !== null) {
|
|
582
|
+
formData.set(key, JSON.stringify(uploadOptions.transformation));
|
|
583
|
+
} else if (key === 'checks' && uploadOptions.checks) {
|
|
584
|
+
formData.set("checks", uploadOptions.checks);
|
|
585
|
+
} else if (uploadOptions[key] !== undefined) {
|
|
586
|
+
if (["onProgress", "abortSignal"].includes(key)) continue;
|
|
587
|
+
formData.set(key, String(uploadOptions[key]));
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
if (uploadOptions.onProgress) {
|
|
592
|
+
xhr.upload.onprogress = function (event) {
|
|
593
|
+
if (uploadOptions.onProgress) uploadOptions.onProgress(event);
|
|
594
|
+
};
|
|
595
|
+
}
|
|
596
|
+
function onAbortHandler() {
|
|
597
|
+
var _uploadOptions$abortS;
|
|
598
|
+
xhr.abort();
|
|
599
|
+
return reject(new ImageKitAbortError("Upload aborted", (_uploadOptions$abortS = uploadOptions.abortSignal) === null || _uploadOptions$abortS === void 0 ? void 0 : _uploadOptions$abortS.reason));
|
|
600
|
+
}
|
|
601
|
+
if (uploadOptions.abortSignal) {
|
|
602
|
+
if (uploadOptions.abortSignal.aborted) {
|
|
603
|
+
var _uploadOptions$abortS2;
|
|
604
|
+
return reject(new ImageKitAbortError("Upload aborted", (_uploadOptions$abortS2 = uploadOptions.abortSignal) === null || _uploadOptions$abortS2 === void 0 ? void 0 : _uploadOptions$abortS2.reason));
|
|
605
|
+
}
|
|
606
|
+
uploadOptions.abortSignal.addEventListener("abort", onAbortHandler);
|
|
607
|
+
xhr.addEventListener("loadend", () => {
|
|
608
|
+
if (uploadOptions.abortSignal) {
|
|
609
|
+
uploadOptions.abortSignal.removeEventListener("abort", onAbortHandler);
|
|
610
|
+
}
|
|
611
|
+
});
|
|
612
|
+
}
|
|
613
|
+
xhr.open('POST', 'https://upload.imagekit.io/api/v1/files/upload');
|
|
614
|
+
xhr.onerror = function (e) {
|
|
615
|
+
return reject(new ImageKitUploadNetworkError(errorMessages.UPLOAD_ENDPOINT_NETWORK_ERROR.message));
|
|
616
|
+
};
|
|
617
|
+
xhr.onload = function () {
|
|
618
|
+
if (xhr.status >= 200 && xhr.status < 300) {
|
|
619
|
+
try {
|
|
620
|
+
var body = JSON.parse(xhr.responseText);
|
|
621
|
+
var uploadResponse = addResponseHeadersAndBody(body, xhr);
|
|
622
|
+
return resolve(uploadResponse);
|
|
623
|
+
} catch (ex) {
|
|
624
|
+
return reject(ex);
|
|
625
|
+
}
|
|
626
|
+
} else if (xhr.status >= 400 && xhr.status < 500) {
|
|
627
|
+
try {
|
|
628
|
+
var body = JSON.parse(xhr.responseText);
|
|
629
|
+
return reject(new ImageKitInvalidRequestError(body.message ?? "Invalid request. Please check the parameters.", getResponseMetadata(xhr)));
|
|
630
|
+
} catch (ex) {
|
|
631
|
+
return reject(ex);
|
|
632
|
+
}
|
|
633
|
+
} else {
|
|
634
|
+
try {
|
|
635
|
+
var body = JSON.parse(xhr.responseText);
|
|
636
|
+
return reject(new ImageKitServerError(body.message ?? "Server error occurred while uploading the file. This is rare and usually temporary.", getResponseMetadata(xhr)));
|
|
637
|
+
} catch (ex) {
|
|
638
|
+
return reject(new ImageKitServerError("Server error occurred while uploading the file. This is rare and usually temporary.", getResponseMetadata(xhr)));
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
};
|
|
642
|
+
xhr.send(formData);
|
|
643
|
+
});
|
|
644
|
+
};
|
|
645
|
+
const addResponseHeadersAndBody = (body, xhr) => {
|
|
646
|
+
let response = {
|
|
647
|
+
...body
|
|
648
|
+
};
|
|
649
|
+
const responseMetadata = getResponseMetadata(xhr);
|
|
650
|
+
Object.defineProperty(response, "$ResponseMetadata", {
|
|
651
|
+
value: responseMetadata,
|
|
652
|
+
enumerable: false,
|
|
653
|
+
writable: false
|
|
654
|
+
});
|
|
655
|
+
return response;
|
|
656
|
+
};
|
|
657
|
+
const getResponseMetadata = xhr => {
|
|
658
|
+
const headers = getResponseHeaderMap(xhr);
|
|
659
|
+
const responseMetadata = {
|
|
660
|
+
statusCode: xhr.status,
|
|
661
|
+
headers: headers,
|
|
662
|
+
requestId: headers["x-request-id"]
|
|
663
|
+
};
|
|
664
|
+
return responseMetadata;
|
|
665
|
+
};
|
|
666
|
+
function getResponseHeaderMap(xhr) {
|
|
667
|
+
const headers = {};
|
|
668
|
+
const responseHeaders = xhr.getAllResponseHeaders();
|
|
669
|
+
if (Object.keys(responseHeaders).length) {
|
|
670
|
+
responseHeaders.trim().split(/[\r\n]+/).map(value => value.split(/: /)).forEach(keyValue => {
|
|
671
|
+
headers[keyValue[0].trim().toLowerCase()] = keyValue[1].trim();
|
|
672
|
+
});
|
|
673
|
+
}
|
|
674
|
+
return headers;
|
|
675
|
+
}
|
|
676
|
+
|
|
669
677
|
exports.ImageKitAbortError = ImageKitAbortError;
|
|
670
678
|
exports.ImageKitInvalidRequestError = ImageKitInvalidRequestError;
|
|
671
679
|
exports.ImageKitServerError = ImageKitServerError;
|