@ekhein/http-request 1.0.5 → 1.0.7
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 +2 -1
- package/dist/main.js +87 -63
- package/dist/proxy/liuguan.proxy.js +4 -4
- package/package.json +4 -3
- 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
|
@@ -7,5 +7,6 @@ export declare class HttpRequest {
|
|
|
7
7
|
post<R = any>(url: string, data?: any): Promise<R>;
|
|
8
8
|
call<R = any>(path: string, data: object): Promise<R>;
|
|
9
9
|
}
|
|
10
|
+
export * from './libs';
|
|
10
11
|
export * from './exceptions';
|
|
11
|
-
export * from '../types/http-request';
|
|
12
|
+
export type * from '../types/http-request';
|
package/dist/main.js
CHANGED
|
@@ -35,6 +35,15 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
35
35
|
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
36
36
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
37
37
|
};
|
|
38
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
39
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
40
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
41
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
42
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
43
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
44
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
45
|
+
});
|
|
46
|
+
};
|
|
38
47
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
48
|
exports.HttpRequest = void 0;
|
|
40
49
|
const axios_1 = __importStar(require("axios"));
|
|
@@ -45,25 +54,24 @@ const cookie_1 = require("cookie");
|
|
|
45
54
|
const tldts_1 = require("tldts");
|
|
46
55
|
const common_1 = require("@nestjs/common");
|
|
47
56
|
const http_error_by_code_util_1 = require("@nestjs/common/utils/http-error-by-code.util");
|
|
57
|
+
const libs_1 = require("./libs");
|
|
48
58
|
const agent_1 = require("./agent");
|
|
49
59
|
const agent_2 = require("./agent");
|
|
50
60
|
const agent_3 = require("./agent");
|
|
51
61
|
const exceptions_1 = require("./exceptions");
|
|
52
62
|
class HttpRequest {
|
|
53
|
-
instance;
|
|
54
63
|
constructor(options) {
|
|
64
|
+
var _a, _b, _c, _d;
|
|
55
65
|
/* Axios */
|
|
56
|
-
this.instance = axios_1.default.create({
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
})
|
|
64
|
-
});
|
|
66
|
+
this.instance = axios_1.default.create(Object.assign({}, Object.assign({
|
|
67
|
+
["headers"]: {},
|
|
68
|
+
["retryLimit"]: 3,
|
|
69
|
+
}, options, {
|
|
70
|
+
["proxy"]: false,
|
|
71
|
+
["retryCount"]: 0,
|
|
72
|
+
})));
|
|
65
73
|
/* Set Http Agent */
|
|
66
|
-
this.instance.interceptors.request.use(
|
|
74
|
+
this.instance.interceptors.request.use((request) => __awaiter(this, void 0, void 0, function* () {
|
|
67
75
|
const jar = Object(request.jar);
|
|
68
76
|
const cookies = Object({ jar });
|
|
69
77
|
if (request.useProxy) {
|
|
@@ -87,9 +95,9 @@ class HttpRequest {
|
|
|
87
95
|
request.httpsAgent = new agent_1.HttpsCookieAgent({ cookies });
|
|
88
96
|
}
|
|
89
97
|
return request;
|
|
90
|
-
});
|
|
98
|
+
}));
|
|
91
99
|
/* Set Cookie */
|
|
92
|
-
this.instance.interceptors.request.use(
|
|
100
|
+
this.instance.interceptors.request.use((request) => __awaiter(this, void 0, void 0, function* () {
|
|
93
101
|
if (request.headers.cookie) {
|
|
94
102
|
const api = new URL(request.url, request.baseURL);
|
|
95
103
|
const domain = (0, tldts_1.getDomain)(api.href); /* 一级域名 */
|
|
@@ -102,15 +110,15 @@ class HttpRequest {
|
|
|
102
110
|
}
|
|
103
111
|
}
|
|
104
112
|
return request;
|
|
105
|
-
});
|
|
113
|
+
}));
|
|
106
114
|
/* Set Cookie Jar */
|
|
107
|
-
this.instance.interceptors.request.use(
|
|
115
|
+
this.instance.interceptors.request.use((request) => __awaiter(this, void 0, void 0, function* () {
|
|
108
116
|
if (!request.jar)
|
|
109
117
|
request.jar = new tough_cookie_1.CookieJar();
|
|
110
118
|
return request;
|
|
111
|
-
});
|
|
119
|
+
}));
|
|
112
120
|
/* Headers Format */
|
|
113
|
-
this.instance.interceptors.request.use(
|
|
121
|
+
this.instance.interceptors.request.use((request) => __awaiter(this, void 0, void 0, function* () {
|
|
114
122
|
if (request.headers) {
|
|
115
123
|
const headers = Object.entries(request.headers);
|
|
116
124
|
for (const [key, value] of headers) {
|
|
@@ -122,21 +130,26 @@ class HttpRequest {
|
|
|
122
130
|
}
|
|
123
131
|
}
|
|
124
132
|
return request;
|
|
125
|
-
});
|
|
133
|
+
}));
|
|
126
134
|
/* Set Body */
|
|
127
|
-
this.instance.interceptors.request.use(
|
|
135
|
+
this.instance.interceptors.request.use((request) => __awaiter(this, void 0, void 0, function* () {
|
|
128
136
|
if (request.body) {
|
|
129
137
|
request.headers.setContentType("application/json");
|
|
130
138
|
request.data = request.body;
|
|
131
139
|
}
|
|
132
140
|
if (request.form) {
|
|
133
|
-
request.
|
|
134
|
-
|
|
141
|
+
if (request.form instanceof libs_1.FormData) {
|
|
142
|
+
request.data = request.form;
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
request.headers.setContentType("application/x-www-form-urlencoded");
|
|
146
|
+
request.data = request.form;
|
|
147
|
+
}
|
|
135
148
|
}
|
|
136
149
|
return request;
|
|
137
|
-
});
|
|
150
|
+
}));
|
|
138
151
|
/* Transform */
|
|
139
|
-
this.instance.interceptors.request.use(
|
|
152
|
+
this.instance.interceptors.request.use((request) => __awaiter(this, void 0, void 0, function* () {
|
|
140
153
|
if (request.serviceType == 1) { }
|
|
141
154
|
if (request.serviceType == 2) {
|
|
142
155
|
const group = String(request.path).split("/").at(-2);
|
|
@@ -146,76 +159,87 @@ class HttpRequest {
|
|
|
146
159
|
request.params = Object({ group, action, invoke_timeout });
|
|
147
160
|
}
|
|
148
161
|
return request;
|
|
149
|
-
});
|
|
162
|
+
}));
|
|
150
163
|
/* Call Custom Hook */
|
|
151
|
-
this.instance.interceptors.request.use(options.interceptorHooks
|
|
164
|
+
this.instance.interceptors.request.use((_a = options.interceptorHooks) === null || _a === void 0 ? void 0 : _a.requestInterceptor, (_b = options.interceptorHooks) === null || _b === void 0 ? void 0 : _b.requestInterceptorCatch);
|
|
152
165
|
/* Save Origin Config */
|
|
153
|
-
this.instance.interceptors.request.use(
|
|
166
|
+
this.instance.interceptors.request.use((request) => __awaiter(this, void 0, void 0, function* () {
|
|
154
167
|
request.__origin__ = (0, lodash_1.cloneDeep)(request);
|
|
155
168
|
return (request);
|
|
156
|
-
});
|
|
169
|
+
}));
|
|
170
|
+
/* Call Custom Hook */
|
|
171
|
+
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);
|
|
157
172
|
/* Transform */
|
|
158
|
-
this.instance.interceptors.response.use(
|
|
159
|
-
if (response.
|
|
160
|
-
if (
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
+
this.instance.interceptors.response.use((response) => __awaiter(this, void 0, void 0, function* () {
|
|
174
|
+
if (typeof response.headers == "object") {
|
|
175
|
+
if (response.headers instanceof axios_1.AxiosHeaders) {
|
|
176
|
+
if (response.config.serviceType == 1) {
|
|
177
|
+
if (http_error_by_code_util_1.HttpErrorByCode[response.data.err])
|
|
178
|
+
throw new http_error_by_code_util_1.HttpErrorByCode[response.data.err](response.data.msg);
|
|
179
|
+
return response.data.res;
|
|
180
|
+
}
|
|
181
|
+
if (response.config.serviceType == 2) {
|
|
182
|
+
if (response.data.status)
|
|
183
|
+
throw new common_1.ServiceUnavailableException(response.data.message);
|
|
184
|
+
return response.data.data;
|
|
185
|
+
}
|
|
186
|
+
if (response.config.html) {
|
|
187
|
+
return new jsdom_1.JSDOM(response.data);
|
|
188
|
+
}
|
|
189
|
+
if (response.config.resolveBodyOnly != false) {
|
|
190
|
+
return response.data;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
173
193
|
}
|
|
174
194
|
return response;
|
|
175
|
-
});
|
|
176
|
-
/* Call Custom Hook */
|
|
177
|
-
this.instance.interceptors.response.use(options.interceptorHooks?.respondInterceptor, options.interceptorHooks?.respondInterceptorCatch);
|
|
195
|
+
}));
|
|
178
196
|
/* Handler Timeout */
|
|
179
|
-
this.instance.interceptors.response.use(
|
|
197
|
+
this.instance.interceptors.response.use((response) => __awaiter(this, void 0, void 0, function* () { return response; }), (error) => __awaiter(this, void 0, void 0, function* () {
|
|
180
198
|
if (error instanceof axios_1.AxiosError) {
|
|
181
199
|
if (!error.code)
|
|
182
|
-
throw new exceptions_1.
|
|
200
|
+
throw new exceptions_1.HttpRetryableError(error.config);
|
|
183
201
|
switch (error.code) {
|
|
184
202
|
case axios_1.AxiosError.ETIMEDOUT:
|
|
185
203
|
case axios_1.AxiosError.ERR_NETWORK:
|
|
186
204
|
case axios_1.AxiosError.ECONNABORTED:
|
|
187
|
-
throw new exceptions_1.
|
|
205
|
+
throw new exceptions_1.HttpRetryableError(error.config);
|
|
188
206
|
}
|
|
189
207
|
}
|
|
190
208
|
throw error;
|
|
191
|
-
});
|
|
209
|
+
}));
|
|
192
210
|
/* Auto Retry */
|
|
193
|
-
this.instance.interceptors.response.use(
|
|
194
|
-
if (error instanceof exceptions_1.
|
|
211
|
+
this.instance.interceptors.response.use((response) => __awaiter(this, void 0, void 0, function* () { return response; }), (error) => __awaiter(this, void 0, void 0, function* () {
|
|
212
|
+
if (error instanceof exceptions_1.HttpRetryableError) {
|
|
195
213
|
const retryCount = error.config.__origin__.retryCount++;
|
|
196
214
|
const retryLimit = error.config.__origin__.retryLimit;
|
|
197
215
|
if (retryCount < retryLimit)
|
|
198
216
|
return this.instance.request(error.config.__origin__);
|
|
199
|
-
throw new exceptions_1.
|
|
217
|
+
throw new exceptions_1.HttpRetryLimitError(error.config);
|
|
200
218
|
}
|
|
201
219
|
else {
|
|
202
220
|
throw error;
|
|
203
221
|
}
|
|
204
|
-
});
|
|
222
|
+
}));
|
|
205
223
|
}
|
|
206
|
-
|
|
207
|
-
return this
|
|
208
|
-
.
|
|
224
|
+
execute(options) {
|
|
225
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
226
|
+
return this.instance
|
|
227
|
+
.request(options);
|
|
228
|
+
});
|
|
209
229
|
}
|
|
210
|
-
|
|
211
|
-
return this
|
|
212
|
-
.
|
|
230
|
+
post(url, data) {
|
|
231
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
232
|
+
return this.instance
|
|
233
|
+
.request({ method: "POST", serviceType: 1, url, data });
|
|
234
|
+
});
|
|
213
235
|
}
|
|
214
|
-
|
|
215
|
-
return this
|
|
216
|
-
.
|
|
236
|
+
call(path, data) {
|
|
237
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
238
|
+
return this.instance
|
|
239
|
+
.request({ method: "POST", serviceType: 2, path, data });
|
|
240
|
+
});
|
|
217
241
|
}
|
|
218
242
|
}
|
|
219
243
|
exports.HttpRequest = HttpRequest;
|
|
244
|
+
__exportStar(require("./libs"), exports);
|
|
220
245
|
__exportStar(require("./exceptions"), exports);
|
|
221
|
-
__exportStar(require("../types/http-request"), exports);
|
|
@@ -21738,10 +21738,6 @@ exports.LIUGUAN_ADCODE_MAP = {
|
|
|
21738
21738
|
}
|
|
21739
21739
|
};
|
|
21740
21740
|
class LiuGuanProxy extends proxy_agent_1.ProxyAgent {
|
|
21741
|
-
orderNo = "O23113017495247478361"; /* 订单编号 */
|
|
21742
|
-
orderPwd = "123456aa.."; /* 订单密码 */
|
|
21743
|
-
duplicate = "1"; /* 是否按天去重 - 0:不去重 1:按天去重 */
|
|
21744
|
-
switch = "0"; /* 异常自动切换 - 0:自动切换 1:不自动切换,返回代理异常 */
|
|
21745
21741
|
constructor(opts) {
|
|
21746
21742
|
super({
|
|
21747
21743
|
getProxyForUrl: () => {
|
|
@@ -21767,6 +21763,10 @@ class LiuGuanProxy extends proxy_agent_1.ProxyAgent {
|
|
|
21767
21763
|
return (proxyURL.href);
|
|
21768
21764
|
},
|
|
21769
21765
|
});
|
|
21766
|
+
this.orderNo = "O23113017495247478361"; /* 订单编号 */
|
|
21767
|
+
this.orderPwd = "123456aa.."; /* 订单密码 */
|
|
21768
|
+
this.duplicate = "1"; /* 是否按天去重 - 0:不去重 1:按天去重 */
|
|
21769
|
+
this.switch = "0"; /* 异常自动切换 - 0:自动切换 1:不自动切换,返回代理异常 */
|
|
21770
21770
|
}
|
|
21771
21771
|
}
|
|
21772
21772
|
exports.LiuGuanProxy = LiuGuanProxy;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ekhein/http-request",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.7",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "ekhein",
|
|
6
6
|
"main": "./dist/main.js",
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"node": ">=18.0.0"
|
|
16
16
|
},
|
|
17
17
|
"devDependencies": {
|
|
18
|
-
"@types/jsdom": "^
|
|
18
|
+
"@types/jsdom": "^21.1.7",
|
|
19
19
|
"@types/lodash": "^4.17.20",
|
|
20
20
|
"@types/node": "^22.15.18",
|
|
21
21
|
"tsx": "^4.19.4",
|
|
@@ -25,8 +25,9 @@
|
|
|
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
|
-
"jsdom": "^
|
|
30
|
+
"jsdom": "^25.0.1",
|
|
30
31
|
"lodash": "^4.17.21",
|
|
31
32
|
"proxy-agent": "^6.5.0",
|
|
32
33
|
"tldts": "^7.0.16",
|
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 {}
|