|
1 | | -import request from "request"; |
| 1 | +// import request from "request"; |
2 | 2 | import respond from "../utils/respond"; |
3 | 3 | import { addAuthorization, RequestOptions } from "../utils/authorization"; |
4 | 4 | import { ImageKitOptions } from "../libs/interfaces/"; |
5 | 5 | import { IKCallback } from "../libs/interfaces/IKCallback"; |
| 6 | +const axios = require('axios'); |
6 | 7 |
|
7 | 8 | export default function ( |
8 | 9 | requestOptions: RequestOptions, |
9 | 10 | defaultOptions: ImageKitOptions, |
10 | 11 | callback?: IKCallback<any, any>, |
11 | 12 | ) { |
12 | | - addAuthorization(requestOptions, defaultOptions.privateKey); |
13 | | - request(requestOptions, function (err, response, body) { |
14 | | - if (typeof callback != "function") return; |
15 | 13 |
|
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: "" |
19 | 20 | } |
| 21 | + } |
20 | 22 |
|
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; |
34 | 25 |
|
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); |
38 | 36 | } |
| 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 | + // } |
39 | 138 |
|
40 | | - respond(false, body, callback); |
41 | | - }); |
| 139 | + // respond(false, body, callback); |
| 140 | + // }); |
42 | 141 | } |
0 commit comments