@ekhein/http-request 1.0.6 → 1.0.8
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/dist/exceptions/index.d.ts +2 -2
- package/dist/exceptions/index.js +5 -5
- package/dist/exceptions/retryLimitError.d.ts +4 -0
- package/dist/exceptions/retryLimitError.js +12 -0
- package/dist/exceptions/retryableError.d.ts +4 -0
- package/dist/exceptions/retryableError.js +12 -0
- package/dist/libs/formData.d.ts +3 -0
- package/dist/libs/formData.js +10 -0
- package/dist/libs/index.d.ts +1 -0
- package/dist/libs/index.js +5 -0
- package/dist/main.d.ts +6 -3
- package/dist/main.js +42 -27
- package/package.json +2 -1
- package/types/http-request.d.ts +4 -2
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export {
|
|
1
|
+
export { HttpRetryableError } from './retryableError';
|
|
2
|
+
export { HttpRetryLimitError } from './retryLimitError';
|
package/dist/exceptions/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
var
|
|
5
|
-
Object.defineProperty(exports, "
|
|
6
|
-
var
|
|
7
|
-
Object.defineProperty(exports, "
|
|
3
|
+
exports.HttpRetryLimitError = exports.HttpRetryableError = void 0;
|
|
4
|
+
var retryableError_1 = require("./retryableError");
|
|
5
|
+
Object.defineProperty(exports, "HttpRetryableError", { enumerable: true, get: function () { return retryableError_1.HttpRetryableError; } });
|
|
6
|
+
var retryLimitError_1 = require("./retryLimitError");
|
|
7
|
+
Object.defineProperty(exports, "HttpRetryLimitError", { enumerable: true, get: function () { return retryLimitError_1.HttpRetryLimitError; } });
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HttpRetryLimitError = void 0;
|
|
4
|
+
const axios_1 = require("axios");
|
|
5
|
+
class HttpRetryLimitError extends axios_1.AxiosError {
|
|
6
|
+
constructor(config) {
|
|
7
|
+
const code = String("AXIOS_RETRY_LIMIT_ERROR");
|
|
8
|
+
const message = String("Axios retry limit error");
|
|
9
|
+
super(message, code, config);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
exports.HttpRetryLimitError = HttpRetryLimitError;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HttpRetryableError = void 0;
|
|
4
|
+
const axios_1 = require("axios");
|
|
5
|
+
class HttpRetryableError extends axios_1.AxiosError {
|
|
6
|
+
constructor(config) {
|
|
7
|
+
const code = String("AXIOS_RETRYABLE_ERROR");
|
|
8
|
+
const message = String("Axios retryable error");
|
|
9
|
+
super(message, code, config);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
exports.HttpRetryableError = HttpRetryableError;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.FormData = void 0;
|
|
7
|
+
const form_data_1 = __importDefault(require("form-data"));
|
|
8
|
+
class FormData extends form_data_1.default {
|
|
9
|
+
}
|
|
10
|
+
exports.FormData = FormData;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { FormData } from './formData';
|
package/dist/main.d.ts
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
import { AxiosInstance } from "axios";
|
|
1
|
+
import { AxiosHeaders, AxiosInstance } from "axios";
|
|
2
2
|
import { HttpRequestConfig } from "../types/http-request";
|
|
3
|
+
export declare class HttpRequestHeaders extends AxiosHeaders {
|
|
4
|
+
}
|
|
3
5
|
export declare class HttpRequest {
|
|
4
6
|
readonly instance: AxiosInstance;
|
|
5
7
|
constructor(options: HttpRequestConfig);
|
|
6
8
|
execute<R = any>(options: HttpRequestConfig): Promise<R>;
|
|
7
|
-
post<R = any>(url: string, data?: any): Promise<R>;
|
|
8
|
-
call<R = any>(path: string, data: object): Promise<R>;
|
|
9
|
+
post<R = any>(url: string, data?: any, opts?: HttpRequestConfig): Promise<R>;
|
|
10
|
+
call<R = any>(path: string, data: object, opts?: HttpRequestConfig): Promise<R>;
|
|
9
11
|
}
|
|
12
|
+
export * from './libs';
|
|
10
13
|
export * from './exceptions';
|
|
11
14
|
export type * from '../types/http-request';
|
package/dist/main.js
CHANGED
|
@@ -45,7 +45,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
45
45
|
});
|
|
46
46
|
};
|
|
47
47
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
48
|
-
exports.HttpRequest = void 0;
|
|
48
|
+
exports.HttpRequest = exports.HttpRequestHeaders = void 0;
|
|
49
49
|
const axios_1 = __importStar(require("axios"));
|
|
50
50
|
const jsdom_1 = require("jsdom");
|
|
51
51
|
const lodash_1 = require("lodash");
|
|
@@ -54,10 +54,14 @@ const cookie_1 = require("cookie");
|
|
|
54
54
|
const tldts_1 = require("tldts");
|
|
55
55
|
const common_1 = require("@nestjs/common");
|
|
56
56
|
const http_error_by_code_util_1 = require("@nestjs/common/utils/http-error-by-code.util");
|
|
57
|
+
const libs_1 = require("./libs");
|
|
57
58
|
const agent_1 = require("./agent");
|
|
58
59
|
const agent_2 = require("./agent");
|
|
59
60
|
const agent_3 = require("./agent");
|
|
60
61
|
const exceptions_1 = require("./exceptions");
|
|
62
|
+
class HttpRequestHeaders extends axios_1.AxiosHeaders {
|
|
63
|
+
}
|
|
64
|
+
exports.HttpRequestHeaders = HttpRequestHeaders;
|
|
61
65
|
class HttpRequest {
|
|
62
66
|
constructor(options) {
|
|
63
67
|
var _a, _b, _c, _d;
|
|
@@ -137,8 +141,13 @@ class HttpRequest {
|
|
|
137
141
|
request.data = request.body;
|
|
138
142
|
}
|
|
139
143
|
if (request.form) {
|
|
140
|
-
request.
|
|
141
|
-
|
|
144
|
+
if (request.form instanceof libs_1.FormData) {
|
|
145
|
+
request.data = request.form;
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
request.headers.setContentType("application/x-www-form-urlencoded");
|
|
149
|
+
request.data = request.form;
|
|
150
|
+
}
|
|
142
151
|
}
|
|
143
152
|
return request;
|
|
144
153
|
}));
|
|
@@ -161,49 +170,54 @@ class HttpRequest {
|
|
|
161
170
|
request.__origin__ = (0, lodash_1.cloneDeep)(request);
|
|
162
171
|
return (request);
|
|
163
172
|
}));
|
|
173
|
+
/* Call Custom Hook */
|
|
174
|
+
this.instance.interceptors.response.use((_c = options.interceptorHooks) === null || _c === void 0 ? void 0 : _c.respondInterceptor, (_d = options.interceptorHooks) === null || _d === void 0 ? void 0 : _d.respondInterceptorCatch);
|
|
164
175
|
/* Transform */
|
|
165
176
|
this.instance.interceptors.response.use((response) => __awaiter(this, void 0, void 0, function* () {
|
|
166
|
-
if (response.
|
|
167
|
-
if (
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
177
|
+
if (typeof response.headers == "object") {
|
|
178
|
+
if (response.headers instanceof axios_1.AxiosHeaders) {
|
|
179
|
+
if (response.config.serviceType == 1) {
|
|
180
|
+
if (http_error_by_code_util_1.HttpErrorByCode[response.data.err])
|
|
181
|
+
throw new http_error_by_code_util_1.HttpErrorByCode[response.data.err](response.data.msg);
|
|
182
|
+
return response.data.res;
|
|
183
|
+
}
|
|
184
|
+
if (response.config.serviceType == 2) {
|
|
185
|
+
if (response.data.status)
|
|
186
|
+
throw new common_1.ServiceUnavailableException(response.data.message);
|
|
187
|
+
return response.data.data;
|
|
188
|
+
}
|
|
189
|
+
if (response.config.html) {
|
|
190
|
+
return new jsdom_1.JSDOM(response.data);
|
|
191
|
+
}
|
|
192
|
+
if (response.config.resolveBodyOnly != false) {
|
|
193
|
+
return response.data;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
180
196
|
}
|
|
181
197
|
return response;
|
|
182
198
|
}));
|
|
183
|
-
/* Call Custom Hook */
|
|
184
|
-
this.instance.interceptors.response.use((_c = options.interceptorHooks) === null || _c === void 0 ? void 0 : _c.respondInterceptor, (_d = options.interceptorHooks) === null || _d === void 0 ? void 0 : _d.respondInterceptorCatch);
|
|
185
199
|
/* Handler Timeout */
|
|
186
200
|
this.instance.interceptors.response.use((response) => __awaiter(this, void 0, void 0, function* () { return response; }), (error) => __awaiter(this, void 0, void 0, function* () {
|
|
187
201
|
if (error instanceof axios_1.AxiosError) {
|
|
188
202
|
if (!error.code)
|
|
189
|
-
throw new exceptions_1.
|
|
203
|
+
throw new exceptions_1.HttpRetryableError(error.config);
|
|
190
204
|
switch (error.code) {
|
|
191
205
|
case axios_1.AxiosError.ETIMEDOUT:
|
|
192
206
|
case axios_1.AxiosError.ERR_NETWORK:
|
|
193
207
|
case axios_1.AxiosError.ECONNABORTED:
|
|
194
|
-
throw new exceptions_1.
|
|
208
|
+
throw new exceptions_1.HttpRetryableError(error.config);
|
|
195
209
|
}
|
|
196
210
|
}
|
|
197
211
|
throw error;
|
|
198
212
|
}));
|
|
199
213
|
/* Auto Retry */
|
|
200
214
|
this.instance.interceptors.response.use((response) => __awaiter(this, void 0, void 0, function* () { return response; }), (error) => __awaiter(this, void 0, void 0, function* () {
|
|
201
|
-
if (error instanceof exceptions_1.
|
|
215
|
+
if (error instanceof exceptions_1.HttpRetryableError) {
|
|
202
216
|
const retryCount = error.config.__origin__.retryCount++;
|
|
203
217
|
const retryLimit = error.config.__origin__.retryLimit;
|
|
204
218
|
if (retryCount < retryLimit)
|
|
205
219
|
return this.instance.request(error.config.__origin__);
|
|
206
|
-
throw new exceptions_1.
|
|
220
|
+
throw new exceptions_1.HttpRetryLimitError(error.config);
|
|
207
221
|
}
|
|
208
222
|
else {
|
|
209
223
|
throw error;
|
|
@@ -216,18 +230,19 @@ class HttpRequest {
|
|
|
216
230
|
.request(options);
|
|
217
231
|
});
|
|
218
232
|
}
|
|
219
|
-
post(url, data) {
|
|
233
|
+
post(url, data, opts) {
|
|
220
234
|
return __awaiter(this, void 0, void 0, function* () {
|
|
221
235
|
return this.instance
|
|
222
|
-
.request({ method: "POST", serviceType: 1, url, data });
|
|
236
|
+
.request(Object.assign(Object.assign({}, opts), { method: "POST", serviceType: 1, url, data }));
|
|
223
237
|
});
|
|
224
238
|
}
|
|
225
|
-
call(path, data) {
|
|
239
|
+
call(path, data, opts) {
|
|
226
240
|
return __awaiter(this, void 0, void 0, function* () {
|
|
227
241
|
return this.instance
|
|
228
|
-
.request({ method: "POST", serviceType: 2, path, data });
|
|
242
|
+
.request(Object.assign(Object.assign({}, opts), { method: "POST", serviceType: 2, path, data }));
|
|
229
243
|
});
|
|
230
244
|
}
|
|
231
245
|
}
|
|
232
246
|
exports.HttpRequest = HttpRequest;
|
|
247
|
+
__exportStar(require("./libs"), exports);
|
|
233
248
|
__exportStar(require("./exceptions"), exports);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ekhein/http-request",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.8",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "ekhein",
|
|
6
6
|
"main": "./dist/main.js",
|
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
"@nestjs/common": "^10.0.0",
|
|
26
26
|
"axios": "^1.12.2",
|
|
27
27
|
"cookie": "^1.0.2",
|
|
28
|
+
"form-data": "^4.0.4",
|
|
28
29
|
"http-cookie-agent": "^7.0.2",
|
|
29
30
|
"jsdom": "^25.0.1",
|
|
30
31
|
"lodash": "^4.17.21",
|
package/types/http-request.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { CookieJar } from
|
|
1
|
+
import { JSDOM } from 'jsdom';
|
|
2
|
+
import { CookieJar } from 'tough-cookie';
|
|
3
|
+
import { AxiosRequestConfig, InternalAxiosRequestConfig, AxiosResponse, AxiosRequestHeaders } from 'axios';
|
|
3
4
|
|
|
4
5
|
export interface HttpLiuGuanProxy {
|
|
5
6
|
adcode?: string
|
|
@@ -45,3 +46,4 @@ export interface HttpRequestConfig extends ExtendAxiosRequestConfig, AxiosReques
|
|
|
45
46
|
|
|
46
47
|
export interface HttpResponse extends ExtendAxiosResponse {}
|
|
47
48
|
|
|
49
|
+
export interface HTMLResponse extends JSDOM {}
|