Skip to content

Commit 596c6e1

Browse files
committed
wip
1 parent f30ad6f commit 596c6e1

File tree

7 files changed

+193
-309
lines changed

7 files changed

+193
-309
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
node_modules/*
1+
node_modules
22
.vscode
33
.nyc_output
44
coverage.lcov

libs/manage/file.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ const listFiles = function (
145145
}
146146

147147
var requestOptions = {
148-
url: "https://api.imagekit.io/v1/files/",
148+
url: `https://api.imagekit.io/v1/files/`,
149149
method: "GET",
150150
qs: listOptions || {},
151151
json: true,

libs/upload/index.ts

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import respond from "../../utils/respond";
44
import request from "../../utils/request";
55
import { IKCallback } from "../interfaces/IKCallback";
66
import { ImageKitOptions, UploadOptions, UploadResponse } from "../interfaces";
7+
const FormData = require('form-data');
78

89
type Modify<T, R> = Omit<T, keyof R> & R;
910
type FormDataOptions = Modify<
@@ -44,36 +45,59 @@ export default function (
4445

4546
var formData = {} as FormDataOptions;
4647

48+
const form = new FormData();
49+
4750
let key: keyof typeof uploadOptions;
4851
for (key in uploadOptions) {
4952
if (key) {
5053
if (key == "file" && typeof uploadOptions.file != "string") {
51-
formData.file = {
52-
value: uploadOptions.file,
53-
options: {
54-
filename: uploadOptions.fileName,
55-
contentType: null,
56-
},
57-
};
54+
// form.append('file', uploadOptions.file);
55+
form.append('file', uploadOptions.file, String(uploadOptions.fileName));
5856
} else if (key == "tags" && Array.isArray(uploadOptions.tags)) {
59-
formData.tags = uploadOptions.tags.join(",");
57+
form.append('tags', uploadOptions.tags.join(","));
6058
}
61-
else if(key == "extensions" && Array.isArray(uploadOptions.extensions)){
62-
formData.extensions = JSON.stringify(uploadOptions.extensions);
59+
else if (key == "extensions" && Array.isArray(uploadOptions.extensions)) {
60+
form.append('extensions', JSON.stringify(uploadOptions.extensions));
6361
} else if (key === "customMetadata" && typeof uploadOptions.customMetadata === "object" &&
64-
!Array.isArray(uploadOptions.customMetadata) && uploadOptions.customMetadata !== null) {
65-
formData.customMetadata = JSON.stringify(uploadOptions.customMetadata)
62+
!Array.isArray(uploadOptions.customMetadata) && uploadOptions.customMetadata !== null) {
63+
form.append('customMetadata', JSON.stringify(uploadOptions.customMetadata));
6664
}
67-
else {
68-
formData[key] = String(uploadOptions[key]);
65+
else {
66+
form.append(key, String(uploadOptions[key]));
6967
}
7068
}
7169
}
7270

71+
// let key: keyof typeof uploadOptions;
72+
// for (key in uploadOptions) {
73+
// if (key) {
74+
// if (key == "file" && typeof uploadOptions.file != "string") {
75+
// formData.file = {
76+
// value: uploadOptions.file,
77+
// options: {
78+
// filename: uploadOptions.fileName,
79+
// contentType: null,
80+
// },
81+
// };
82+
// } else if (key == "tags" && Array.isArray(uploadOptions.tags)) {
83+
// formData.tags = uploadOptions.tags.join(",");
84+
// }
85+
// else if(key == "extensions" && Array.isArray(uploadOptions.extensions)){
86+
// formData.extensions = JSON.stringify(uploadOptions.extensions);
87+
// } else if (key === "customMetadata" && typeof uploadOptions.customMetadata === "object" &&
88+
// !Array.isArray(uploadOptions.customMetadata) && uploadOptions.customMetadata !== null) {
89+
// formData.customMetadata = JSON.stringify(uploadOptions.customMetadata)
90+
// }
91+
// else {
92+
// formData[key] = String(uploadOptions[key]);
93+
// }
94+
// }
95+
// }
96+
7397
var requestOptions = {
7498
url: defaultOptions.uploadEndpoint || "https://upload.imagekit.io/api/v1/files/upload",
7599
method: "POST",
76-
formData: formData,
100+
formData: form,
77101
json: true,
78102
};
79103

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
"homepage": "https://imagekit.io",
1515
"license": "MIT",
1616
"dependencies": {
17+
"axios": "^0.27.2",
18+
"form-data": "^4.0.0",
1719
"hamming-distance": "^1.0.0",
1820
"lodash": "^4.17.15",
19-
"request": "^2.88.0",
2021
"uuid": "^3.3.3"
2122
},
2223
"engines": {

utils/authorization.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
interface RequestOptions {
22
url: string;
3+
headers?: Object;
34
method: string;
45
formData?: Object;
6+
qs?: Object;
57
json?: any;
68
auth?: {
79
user: string;

utils/request.ts

Lines changed: 124 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,141 @@
1-
import request from "request";
1+
// import request from "request";
22
import respond from "../utils/respond";
33
import { addAuthorization, RequestOptions } from "../utils/authorization";
44
import { ImageKitOptions } from "../libs/interfaces/";
55
import { IKCallback } from "../libs/interfaces/IKCallback";
6+
const axios = require('axios');
67

78
export default function (
89
requestOptions: RequestOptions,
910
defaultOptions: ImageKitOptions,
1011
callback?: IKCallback<any, any>,
1112
) {
12-
addAuthorization(requestOptions, defaultOptions.privateKey);
13-
request(requestOptions, function (err, response, body) {
14-
if (typeof callback != "function") return;
1513

16-
if (err) {
17-
respond(true, err, callback);
18-
return;
14+
var options: any = {
15+
method: requestOptions.method,
16+
url: requestOptions.url,
17+
auth: {
18+
username: defaultOptions.privateKey || "",
19+
password: ""
1920
}
21+
}
2022

21-
if (response && response.statusCode === 429) {
22-
respond(
23-
true,
24-
{
25-
...body,
26-
"X-RateLimit-Limit": parseInt(response.caseless.get("X-RateLimit-Limit"), 10),
27-
"X-RateLimit-Reset": parseInt(response.caseless.get("X-RateLimit-Reset"), 10),
28-
"X-RateLimit-Interval": parseInt(response.caseless.get("X-RateLimit-Interval")),
29-
},
30-
callback,
31-
);
32-
return;
33-
}
23+
if (typeof requestOptions.json === "object") options.data = requestOptions.json;
24+
else if(typeof requestOptions.formData === "object") options.data = requestOptions.formData;
3425

35-
if (response && response.statusCode >= 400) {
36-
respond(true, body, callback);
37-
return;
26+
if (typeof requestOptions.qs === "object") options.params = requestOptions.qs;
27+
if (typeof requestOptions.headers === "object") options.headers = requestOptions.headers;
28+
29+
axios(options).then((response: any) => {
30+
if (typeof callback != "function") return;
31+
const { data, status, headers } = response;
32+
if (status >= 200 && status <= 299) {
33+
respond(false, data, callback);
34+
} else {
35+
respond(true, data, callback);
3836
}
37+
}, (error: any) => {
38+
if (typeof callback != "function") return;
39+
if (error.response) {
40+
// The request was made and the server responded with a status code
41+
// that falls out of the range of 2xx
42+
if(error.response?.status === 429) {
43+
respond(true, {
44+
...error.response.data,
45+
"X-RateLimit-Limit": parseInt(error.response.headers["x-ratelimit-limit"], 10),
46+
"X-RateLimit-Reset": parseInt(error.response.headers["x-ratelimit-reset"], 10),
47+
"X-RateLimit-Interval": parseInt(error.response.headers["x-ratelimit-interval"], 10),
48+
}, callback);
49+
} else {
50+
respond(true, error.response.data, callback);
51+
}
52+
53+
} else if (error.request) {
54+
// The request was made but no response was received
55+
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
56+
// http.ClientRequest in node.js
57+
respond(true, error.request, callback);
58+
} else {
59+
// Something happened in setting up the request that triggered an Error
60+
respond(true, error.message, callback);
61+
}
62+
})
63+
64+
// var req = https.request({
65+
// ...urlToHttpOptions(new URL(requestOptions.url)),
66+
// method: requestOptions.method,
67+
// headers: {
68+
// Authorization: 'Basic ' + Buffer.from(defaultOptions.privateKey + ':').toString('base64')
69+
// }
70+
// }, (response: IncomingMessage) => {
71+
// const { statusCode = 0, headers: resHeaders } = response;
72+
// response.setEncoding('utf8');
73+
// var rawBody = '';
74+
// var JSONBody;
75+
// response.on("data", (chunk) => {
76+
// rawBody += chunk;
77+
// });
78+
// response.on("end", () => {
79+
// try {
80+
// var result = {
81+
// responseMetadata: {
82+
// statusCode,
83+
// rawBody,
84+
// headers: resHeaders
85+
// }
86+
// };
87+
// JSONBody = JSON.parse(rawBody);
88+
// if (statusCode >= 200 && statusCode <= 299) {
89+
// respond(false, JSONBody, callback);
90+
// return;
91+
// } else {
92+
// respond(true, JSONBody, callback);
93+
// }
94+
// } catch (e) {
95+
// respond(true, {
96+
// reason: "JSON_PARSE_ERROR"
97+
// }, callback);
98+
// return;
99+
// }
100+
// })
101+
// });
102+
103+
// var requestPayload = null;
104+
// if (requestOptions.formData) requestPayload = requestOptions.formData;
105+
// else if (requestOptions.json) requestPayload = requestOptions.json;
106+
107+
// if (requestPayload) req.write(requestPayload);
108+
// req.end();
109+
110+
// return;
111+
// addAuthorization(requestOptions, defaultOptions.privateKey);
112+
// request(requestOptions, function (err, response, body) {
113+
// if (typeof callback != "function") return;
114+
115+
// if (err) {
116+
// respond(true, err, callback);
117+
// return;
118+
// }
119+
120+
// if (response && response.statusCode === 429) {
121+
// respond(
122+
// true,
123+
// {
124+
// ...body,
125+
// "X-RateLimit-Limit": parseInt(response.caseless.get("X-RateLimit-Limit"), 10),
126+
// "X-RateLimit-Reset": parseInt(response.caseless.get("X-RateLimit-Reset"), 10),
127+
// "X-RateLimit-Interval": parseInt(response.caseless.get("X-RateLimit-Interval")),
128+
// },
129+
// callback,
130+
// );
131+
// return;
132+
// }
133+
134+
// if (response && response.statusCode >= 400) {
135+
// respond(true, body, callback);
136+
// return;
137+
// }
39138

40-
respond(false, body, callback);
41-
});
139+
// respond(false, body, callback);
140+
// });
42141
}

0 commit comments

Comments
 (0)