@ekhein/http-request 2.0.7 → 2.0.9
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/http-request.d.ts +18 -6
- package/dist/http-request.js +135 -77
- package/package.json +1 -1
- package/types/http-request.d.ts +12 -9
package/dist/http-request.d.ts
CHANGED
|
@@ -1,11 +1,23 @@
|
|
|
1
1
|
import { AxiosInstance } from 'axios';
|
|
2
|
-
import { HttpRequestConfig } from '../types';
|
|
2
|
+
import { HttpRequestConfig, NativeClientConfig, ScriptClientConfig, SekiroClientConfig, JaysonClientConfig } from '../types';
|
|
3
3
|
export declare class HttpRequest {
|
|
4
|
-
readonly instance: AxiosInstance;
|
|
4
|
+
protected readonly instance: AxiosInstance;
|
|
5
5
|
constructor(options?: HttpRequestConfig);
|
|
6
6
|
execute<R = any>(options: HttpRequestConfig): Promise<R>;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
}
|
|
8
|
+
export declare class NativeClient extends HttpRequest {
|
|
9
|
+
constructor(options?: NativeClientConfig);
|
|
10
|
+
post<R = any>(url: string, body?: any): Promise<R>;
|
|
11
|
+
}
|
|
12
|
+
export declare class SekiroClient extends HttpRequest {
|
|
13
|
+
constructor(options?: SekiroClientConfig);
|
|
14
|
+
invoke<R = any>(path: string, params?: Record<string, string>): Promise<R>;
|
|
15
|
+
}
|
|
16
|
+
export declare class ScriptClient extends HttpRequest {
|
|
17
|
+
constructor(options: ScriptClientConfig);
|
|
18
|
+
hook<R = any>(url: string, args: ArrayLike<any>): Promise<R>;
|
|
19
|
+
}
|
|
20
|
+
export declare class JaysonClient extends HttpRequest {
|
|
21
|
+
constructor(options: JaysonClientConfig);
|
|
22
|
+
call<R = any>(method: string, args: ArrayLike<any>): Promise<R>;
|
|
11
23
|
}
|
package/dist/http-request.js
CHANGED
|
@@ -54,14 +54,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
54
54
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
55
55
|
};
|
|
56
56
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
57
|
-
exports.HttpRequest = void 0;
|
|
57
|
+
exports.JaysonClient = exports.ScriptClient = exports.SekiroClient = exports.NativeClient = exports.HttpRequest = void 0;
|
|
58
58
|
const axios_1 = __importStar(require("axios"));
|
|
59
59
|
const tldts_1 = __importDefault(require("tldts"));
|
|
60
60
|
const cookie = __importStar(require("cookie"));
|
|
61
61
|
const jsdom_1 = require("jsdom");
|
|
62
62
|
const crypto_1 = require("crypto");
|
|
63
63
|
const lodash_1 = require("lodash");
|
|
64
|
-
const path_to_regexp_1 = require("path-to-regexp");
|
|
65
64
|
const tough_cookie_1 = require("tough-cookie");
|
|
66
65
|
const common_1 = require("@nestjs/common");
|
|
67
66
|
const async_retry_decorator_1 = require("@ekhein/async-retry-decorator");
|
|
@@ -137,7 +136,6 @@ class HttpRequest {
|
|
|
137
136
|
.entries(request.headers)
|
|
138
137
|
.forEach(([key, value]) => {
|
|
139
138
|
const name = (0, lodash_1.kebabCase)(key);
|
|
140
|
-
request.headers.delete(key);
|
|
141
139
|
request.headers.set(name, value);
|
|
142
140
|
});
|
|
143
141
|
}
|
|
@@ -160,63 +158,14 @@ class HttpRequest {
|
|
|
160
158
|
}
|
|
161
159
|
return request;
|
|
162
160
|
}));
|
|
163
|
-
/* Transform */
|
|
164
|
-
this.instance.interceptors.request.use((request) => __awaiter(this, void 0, void 0, function* () {
|
|
165
|
-
var _a;
|
|
166
|
-
if (request.serviceType == 1) {
|
|
167
|
-
request.baseURL = String((_a = request.baseURL) !== null && _a !== void 0 ? _a : process.env.CORE_URL);
|
|
168
|
-
}
|
|
169
|
-
if (request.serviceType == 2) {
|
|
170
|
-
const group = (0, path_to_regexp_1.compile)(request.path)(request.params).split("/").at(-2);
|
|
171
|
-
const action = (0, path_to_regexp_1.compile)(request.path)(request.params).split("/").at(-1);
|
|
172
|
-
const invoke_timeout = Number(request.timeout - 5e3);
|
|
173
|
-
request.params = Object({ group, action, invoke_timeout });
|
|
174
|
-
request.baseURL = String(process.env.SEKIRO_URL);
|
|
175
|
-
request.url = String("/business-demo/invoke");
|
|
176
|
-
}
|
|
177
|
-
if (request.serviceType == 3) {
|
|
178
|
-
request.validateStatus = status => status <= 500;
|
|
179
|
-
request.baseURL = String(`http://${request.clientIp}:65000/script`);
|
|
180
|
-
request.form = Object({
|
|
181
|
-
args: JSON.stringify([...request.args])
|
|
182
|
-
});
|
|
183
|
-
}
|
|
184
|
-
if (request.serviceType == 4) {
|
|
185
|
-
request.baseURL = String(`http://${request.clientIp}:65001/`);
|
|
186
|
-
request.body.params = Array.from(request.args);
|
|
187
|
-
request.body.jsonrpc = String("2.0");
|
|
188
|
-
request.body.id = (0, crypto_1.randomUUID)();
|
|
189
|
-
}
|
|
190
|
-
return request;
|
|
191
|
-
}));
|
|
192
161
|
/* Request Hook */
|
|
193
162
|
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);
|
|
194
163
|
/* Response Hook */
|
|
195
164
|
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);
|
|
196
165
|
/* Transform */
|
|
197
166
|
this.instance.interceptors.response.use((response) => __awaiter(this, void 0, void 0, function* () {
|
|
198
|
-
if (typeof response.headers == "object") {
|
|
167
|
+
if (typeof (response === null || response === void 0 ? void 0 : response.headers) == "object") {
|
|
199
168
|
if (response.headers instanceof headers_1.HttpRequestHeaders) {
|
|
200
|
-
if (response.config.serviceType == 1) {
|
|
201
|
-
if (http_error_by_code_util_1.HttpErrorByCode[response.data.err])
|
|
202
|
-
throw new http_error_by_code_util_1.HttpErrorByCode[response.data.err](response.data.msg);
|
|
203
|
-
return response.data.res;
|
|
204
|
-
}
|
|
205
|
-
if (response.config.serviceType == 2) {
|
|
206
|
-
if (response.data.status)
|
|
207
|
-
throw new common_1.ServiceUnavailableException(response.data.message);
|
|
208
|
-
return response.data.data;
|
|
209
|
-
}
|
|
210
|
-
if (response.config.serviceType == 3) {
|
|
211
|
-
if (response.status != axios_1.HttpStatusCode.Ok)
|
|
212
|
-
throw new common_1.ServiceUnavailableException(response.data.exception);
|
|
213
|
-
return response.data.result;
|
|
214
|
-
}
|
|
215
|
-
if (response.config.serviceType == 4) {
|
|
216
|
-
if (response.data.error)
|
|
217
|
-
throw new common_1.ServiceUnavailableException(response.data.error.message);
|
|
218
|
-
return response.data.result;
|
|
219
|
-
}
|
|
220
169
|
if (response.config.html) {
|
|
221
170
|
return new jsdom_1.JSDOM(response.data);
|
|
222
171
|
}
|
|
@@ -234,30 +183,6 @@ class HttpRequest {
|
|
|
234
183
|
.request(options);
|
|
235
184
|
});
|
|
236
185
|
}
|
|
237
|
-
post(url_1, data_1) {
|
|
238
|
-
return __awaiter(this, arguments, void 0, function* (url, data, opts = {}) {
|
|
239
|
-
return this.instance
|
|
240
|
-
.request(Object.assign(Object.assign({}, opts), { method: "POST", serviceType: 1, data, url }));
|
|
241
|
-
});
|
|
242
|
-
}
|
|
243
|
-
invoke(path_1, data_1) {
|
|
244
|
-
return __awaiter(this, arguments, void 0, function* (path, data, opts = {}) {
|
|
245
|
-
return this.instance
|
|
246
|
-
.request(Object.assign(Object.assign({}, opts), { method: "POST", serviceType: 2, data, path }));
|
|
247
|
-
});
|
|
248
|
-
}
|
|
249
|
-
hook(url_1, args_1) {
|
|
250
|
-
return __awaiter(this, arguments, void 0, function* (url, args, opts = {}) {
|
|
251
|
-
return this.instance
|
|
252
|
-
.request(Object.assign(Object.assign({}, opts), { method: "POST", serviceType: 3, args, url }));
|
|
253
|
-
});
|
|
254
|
-
}
|
|
255
|
-
call(method_1, args_1) {
|
|
256
|
-
return __awaiter(this, arguments, void 0, function* (method, args, opts = {}) {
|
|
257
|
-
return this.instance
|
|
258
|
-
.request(Object.assign(Object.assign({}, opts), { method: "POST", serviceType: 4, args, body: { method } }));
|
|
259
|
-
});
|
|
260
|
-
}
|
|
261
186
|
}
|
|
262
187
|
exports.HttpRequest = HttpRequest;
|
|
263
188
|
__decorate([
|
|
@@ -280,3 +205,136 @@ __decorate([
|
|
|
280
205
|
__metadata("design:paramtypes", [Object]),
|
|
281
206
|
__metadata("design:returntype", Promise)
|
|
282
207
|
], HttpRequest.prototype, "execute", null);
|
|
208
|
+
class NativeClient extends HttpRequest {
|
|
209
|
+
constructor(options) {
|
|
210
|
+
var _a, _b;
|
|
211
|
+
super({
|
|
212
|
+
timeout: (_a = options === null || options === void 0 ? void 0 : options.timeout) !== null && _a !== void 0 ? _a : 10e3,
|
|
213
|
+
baseURL: (_b = options === null || options === void 0 ? void 0 : options.baseURL) !== null && _b !== void 0 ? _b : process.env.CORE_URL,
|
|
214
|
+
headers: options === null || options === void 0 ? void 0 : options.headers,
|
|
215
|
+
interceptorHooks: {
|
|
216
|
+
respondInterceptor(response) {
|
|
217
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
218
|
+
const errcode = response.data.err;
|
|
219
|
+
if (http_error_by_code_util_1.HttpErrorByCode[errcode])
|
|
220
|
+
throw new http_error_by_code_util_1.HttpErrorByCode[errcode](response.data.msg);
|
|
221
|
+
return response.data.res;
|
|
222
|
+
});
|
|
223
|
+
},
|
|
224
|
+
},
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
post(url, body) {
|
|
228
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
229
|
+
return this.instance
|
|
230
|
+
.request({
|
|
231
|
+
method: "POST",
|
|
232
|
+
url,
|
|
233
|
+
body,
|
|
234
|
+
});
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
exports.NativeClient = NativeClient;
|
|
239
|
+
class SekiroClient extends HttpRequest {
|
|
240
|
+
constructor(options) {
|
|
241
|
+
var _a, _b, _c;
|
|
242
|
+
super({
|
|
243
|
+
timeout: (_a = options === null || options === void 0 ? void 0 : options.timeout) !== null && _a !== void 0 ? _a : 10e3 * 1.2,
|
|
244
|
+
baseURL: (_b = options === null || options === void 0 ? void 0 : options.baseURL) !== null && _b !== void 0 ? _b : process.env.SEKIRO_URL,
|
|
245
|
+
params: {
|
|
246
|
+
invoke_timeout: (_c = options === null || options === void 0 ? void 0 : options.timeout) !== null && _c !== void 0 ? _c : 10e3
|
|
247
|
+
},
|
|
248
|
+
interceptorHooks: {
|
|
249
|
+
respondInterceptor(response) {
|
|
250
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
251
|
+
if (response.data.status)
|
|
252
|
+
throw new common_1.ServiceUnavailableException(response.data.message);
|
|
253
|
+
return response.data.data;
|
|
254
|
+
});
|
|
255
|
+
},
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
invoke(path_1) {
|
|
260
|
+
return __awaiter(this, arguments, void 0, function* (path, params = {}) {
|
|
261
|
+
const group = String(path).split("/").at(-2);
|
|
262
|
+
const action = String(path).split("/").at(-1);
|
|
263
|
+
return this.instance
|
|
264
|
+
.request({
|
|
265
|
+
method: "POST",
|
|
266
|
+
url: "/business-demo/invoke",
|
|
267
|
+
body: Object.assign(Object.assign({}, params), { group, action })
|
|
268
|
+
});
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
exports.SekiroClient = SekiroClient;
|
|
273
|
+
class ScriptClient extends HttpRequest {
|
|
274
|
+
constructor(options) {
|
|
275
|
+
super({
|
|
276
|
+
timeout: 10e3,
|
|
277
|
+
baseURL: `http://${options.clientIp}:65000/script`,
|
|
278
|
+
validateStatus: status => status <= 500,
|
|
279
|
+
interceptorHooks: {
|
|
280
|
+
respondInterceptor(response) {
|
|
281
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
282
|
+
var _a;
|
|
283
|
+
if (response.status != axios_1.HttpStatusCode.Ok) {
|
|
284
|
+
const reason = Array.from([response.data.exception, (_a = response.data.args) === null || _a === void 0 ? void 0 : _a[0]]).join("::");
|
|
285
|
+
throw new common_1.ServiceUnavailableException(reason);
|
|
286
|
+
}
|
|
287
|
+
return response.data.result;
|
|
288
|
+
});
|
|
289
|
+
},
|
|
290
|
+
}
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
hook(url, args) {
|
|
294
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
295
|
+
args = Array.from(args);
|
|
296
|
+
args = JSON.stringify(args);
|
|
297
|
+
return this.instance
|
|
298
|
+
.request({
|
|
299
|
+
method: "POST",
|
|
300
|
+
url,
|
|
301
|
+
form: {
|
|
302
|
+
args
|
|
303
|
+
}
|
|
304
|
+
});
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
exports.ScriptClient = ScriptClient;
|
|
309
|
+
class JaysonClient extends HttpRequest {
|
|
310
|
+
constructor(options) {
|
|
311
|
+
super({
|
|
312
|
+
timeout: 10e3,
|
|
313
|
+
baseURL: `http://${options.clientIp}:65001/`,
|
|
314
|
+
interceptorHooks: {
|
|
315
|
+
respondInterceptor(response) {
|
|
316
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
317
|
+
if (response.data.error)
|
|
318
|
+
throw new common_1.ServiceUnavailableException(response.data.error.message);
|
|
319
|
+
return response.data.result;
|
|
320
|
+
});
|
|
321
|
+
},
|
|
322
|
+
}
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
call(method, args) {
|
|
326
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
327
|
+
const id = (0, crypto_1.randomUUID)();
|
|
328
|
+
const params = Array.from(args);
|
|
329
|
+
const jsonrpc = String("2.0");
|
|
330
|
+
return this.instance
|
|
331
|
+
.request({
|
|
332
|
+
method: "POST",
|
|
333
|
+
body: {
|
|
334
|
+
method, params, jsonrpc, id
|
|
335
|
+
}
|
|
336
|
+
});
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
exports.JaysonClient = JaysonClient;
|
package/package.json
CHANGED
package/types/http-request.d.ts
CHANGED
|
@@ -12,13 +12,6 @@ export interface HttpAndroidProxy {
|
|
|
12
12
|
clientIp: string
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
export interface HttpServiceTypes {
|
|
16
|
-
NATIVE: 1
|
|
17
|
-
SEKIRO: 2
|
|
18
|
-
SCRIPT: 3
|
|
19
|
-
JAYSON: 4
|
|
20
|
-
}
|
|
21
|
-
|
|
22
15
|
export interface ExtendInternalAxiosRequestConfig extends HttpRequestConfig, InternalAxiosRequestConfig {
|
|
23
16
|
headers: AxiosRequestHeaders;
|
|
24
17
|
}
|
|
@@ -31,9 +24,7 @@ export interface ExtendAxiosRequestConfig extends Record<string, any> {
|
|
|
31
24
|
form?: any
|
|
32
25
|
body?: any
|
|
33
26
|
html?: boolean
|
|
34
|
-
clientIp?: string
|
|
35
27
|
resolveBodyOnly?: boolean
|
|
36
|
-
serviceType?: HttpServiceTypes[keyof HttpServiceTypes]
|
|
37
28
|
useProxy?: boolean | HttpLiuGuanProxy | HttpAndroidProxy
|
|
38
29
|
jar?: CookieJar
|
|
39
30
|
interceptorHooks?: {
|
|
@@ -46,6 +37,18 @@ export interface ExtendAxiosRequestConfig extends Record<string, any> {
|
|
|
46
37
|
|
|
47
38
|
export interface HttpRequestConfig extends ExtendAxiosRequestConfig, AxiosRequestConfig {}
|
|
48
39
|
|
|
40
|
+
export interface NativeClientConfig extends Pick<HttpRequestConfig, "baseURL" | "timeout" | "headers"> {}
|
|
41
|
+
|
|
42
|
+
export interface SekiroClientConfig extends Pick<HttpRequestConfig, "baseURL" | "timeout"> {}
|
|
43
|
+
|
|
44
|
+
export interface ScriptClientConfig {
|
|
45
|
+
clientIp: string
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface JaysonClientConfig {
|
|
49
|
+
clientIp: string
|
|
50
|
+
}
|
|
51
|
+
|
|
49
52
|
export interface HttpResponse extends ExtendAxiosResponse {}
|
|
50
53
|
|
|
51
54
|
export interface HTMLResponse extends JSDOM {}
|