@ekhein/http-request 2.0.6 → 2.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.
@@ -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
- post<R = any>(url: string, data?: any, opts?: HttpRequestConfig): Promise<R>;
8
- invoke<R = any>(path: string, data: object, opts?: HttpRequestConfig): Promise<R>;
9
- hook<R = any>(url: string, args: ArrayLike<any>, opts?: HttpRequestConfig): Promise<R>;
10
- call<R = any>(method: string, args: ArrayLike<any>, opts?: HttpRequestConfig): Promise<R>;
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
  }
@@ -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,34 +158,6 @@ 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
- if (request.serviceType == 1) {
166
- request.baseURL = String(process.env.CORE_URL);
167
- }
168
- if (request.serviceType == 2) {
169
- const group = (0, path_to_regexp_1.compile)(request.path)(request.params).split("/").at(-2);
170
- const action = (0, path_to_regexp_1.compile)(request.path)(request.params).split("/").at(-1);
171
- const invoke_timeout = Number(request.timeout - 5e3);
172
- request.params = Object({ group, action, invoke_timeout });
173
- request.baseURL = String(process.env.SEKIRO_URL);
174
- request.url = String("/business-demo/invoke");
175
- }
176
- if (request.serviceType == 3) {
177
- request.validateStatus = status => status <= 500;
178
- request.baseURL = String(`http://${request.clientIp}:65000/script`);
179
- request.form = Object({
180
- args: JSON.stringify([...request.args])
181
- });
182
- }
183
- if (request.serviceType == 4) {
184
- request.baseURL = String(`http://${request.clientIp}:65001/`);
185
- request.body.params = Array.from(request.args);
186
- request.body.jsonrpc = String("2.0");
187
- request.body.id = (0, crypto_1.randomUUID)();
188
- }
189
- return request;
190
- }));
191
161
  /* Request Hook */
192
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);
193
163
  /* Response Hook */
@@ -196,26 +166,6 @@ class HttpRequest {
196
166
  this.instance.interceptors.response.use((response) => __awaiter(this, void 0, void 0, function* () {
197
167
  if (typeof response.headers == "object") {
198
168
  if (response.headers instanceof headers_1.HttpRequestHeaders) {
199
- if (response.config.serviceType == 1) {
200
- if (http_error_by_code_util_1.HttpErrorByCode[response.data.err])
201
- throw new http_error_by_code_util_1.HttpErrorByCode[response.data.err](response.data.msg);
202
- return response.data.res;
203
- }
204
- if (response.config.serviceType == 2) {
205
- if (response.data.status)
206
- throw new common_1.ServiceUnavailableException(response.data.message);
207
- return response.data.data;
208
- }
209
- if (response.config.serviceType == 3) {
210
- if (response.status != axios_1.HttpStatusCode.Ok)
211
- throw new common_1.ServiceUnavailableException(response.data.exception);
212
- return response.data.result;
213
- }
214
- if (response.config.serviceType == 4) {
215
- if (response.data.error)
216
- throw new common_1.ServiceUnavailableException(response.data.error.message);
217
- return response.data.result;
218
- }
219
169
  if (response.config.html) {
220
170
  return new jsdom_1.JSDOM(response.data);
221
171
  }
@@ -233,30 +183,6 @@ class HttpRequest {
233
183
  .request(options);
234
184
  });
235
185
  }
236
- post(url_1, data_1) {
237
- return __awaiter(this, arguments, void 0, function* (url, data, opts = {}) {
238
- return this.instance
239
- .request(Object.assign(Object.assign({}, opts), { method: "POST", serviceType: 1, data, url }));
240
- });
241
- }
242
- invoke(path_1, data_1) {
243
- return __awaiter(this, arguments, void 0, function* (path, data, opts = {}) {
244
- return this.instance
245
- .request(Object.assign(Object.assign({}, opts), { method: "POST", serviceType: 2, data, path }));
246
- });
247
- }
248
- hook(url_1, args_1) {
249
- return __awaiter(this, arguments, void 0, function* (url, args, opts = {}) {
250
- return this.instance
251
- .request(Object.assign(Object.assign({}, opts), { method: "POST", serviceType: 3, args, url }));
252
- });
253
- }
254
- call(method_1, args_1) {
255
- return __awaiter(this, arguments, void 0, function* (method, args, opts = {}) {
256
- return this.instance
257
- .request(Object.assign(Object.assign({}, opts), { method: "POST", serviceType: 4, args, body: { method } }));
258
- });
259
- }
260
186
  }
261
187
  exports.HttpRequest = HttpRequest;
262
188
  __decorate([
@@ -279,3 +205,136 @@ __decorate([
279
205
  __metadata("design:paramtypes", [Object]),
280
206
  __metadata("design:returntype", Promise)
281
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ekhein/http-request",
3
- "version": "2.0.6",
3
+ "version": "2.0.8",
4
4
  "license": "MIT",
5
5
  "author": "ekhein",
6
6
  "main": "./dist/index.js",
@@ -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 {}