@ekhein/http-request 1.0.16 → 1.0.18

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/main.d.ts CHANGED
@@ -1,15 +1,18 @@
1
- import { AxiosHeaders, AxiosInstance } from "axios";
1
+ import { AxiosError, AxiosHeaders, AxiosInstance } from "axios";
2
+ import { default as FormData } from 'form-data';
2
3
  import { HttpRequestConfig } from "../types/http-request";
3
4
  export declare class HttpRequestHeaders extends AxiosHeaders {
4
5
  }
6
+ export declare class HttpRequestException extends AxiosError {
7
+ }
8
+ export declare class HttpRequestAttemptException extends HttpRequestException {
9
+ }
10
+ export { FormData };
5
11
  export declare class HttpRequest {
6
12
  readonly instance: AxiosInstance;
7
13
  constructor(options?: HttpRequestConfig);
8
14
  execute<R = any>(options: HttpRequestConfig): Promise<R>;
9
15
  post<R = any>(url: string, data?: any, opts?: HttpRequestConfig): Promise<R>;
10
16
  invoke<R = any>(path: string, data: object, opts?: HttpRequestConfig): Promise<R>;
11
- call<R = any>(url: string, args?: Array<any>, opts?: HttpRequestConfig): Promise<R>;
12
17
  }
13
- export * from './libs';
14
- export * from './exceptions';
15
18
  export type * from '../types/http-request';
package/dist/main.js CHANGED
@@ -15,6 +15,12 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
15
15
  }) : function(o, v) {
16
16
  o["default"] = v;
17
17
  });
18
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
19
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
20
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
21
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
22
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
23
+ };
18
24
  var __importStar = (this && this.__importStar) || (function () {
19
25
  var ownKeys = function(o) {
20
26
  ownKeys = Object.getOwnPropertyNames || function (o) {
@@ -32,8 +38,8 @@ var __importStar = (this && this.__importStar) || (function () {
32
38
  return result;
33
39
  };
34
40
  })();
35
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
36
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
41
+ var __metadata = (this && this.__metadata) || function (k, v) {
42
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
37
43
  };
38
44
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
39
45
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
@@ -48,35 +54,38 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
48
54
  return (mod && mod.__esModule) ? mod : { "default": mod };
49
55
  };
50
56
  Object.defineProperty(exports, "__esModule", { value: true });
51
- exports.HttpRequest = exports.HttpRequestHeaders = void 0;
57
+ exports.HttpRequest = exports.FormData = exports.HttpRequestAttemptException = exports.HttpRequestException = exports.HttpRequestHeaders = void 0;
52
58
  const axios_1 = __importStar(require("axios"));
53
59
  const tldts_1 = __importDefault(require("tldts"));
54
60
  const cookie = __importStar(require("cookie"));
55
61
  const jsdom_1 = require("jsdom");
56
- const lodash_1 = require("lodash");
62
+ const form_data_1 = __importDefault(require("form-data"));
63
+ Object.defineProperty(exports, "FormData", { enumerable: true, get: function () { return form_data_1.default; } });
57
64
  const tough_cookie_1 = require("tough-cookie");
58
65
  const common_1 = require("@nestjs/common");
66
+ const async_retry_decorator_1 = require("@ekhein/async-retry-decorator");
59
67
  const http_error_by_code_util_1 = require("@nestjs/common/utils/http-error-by-code.util");
60
- const libs_1 = require("./libs");
61
68
  const agent_1 = require("./agent");
62
69
  const agent_2 = require("./agent");
63
70
  const agent_3 = require("./agent");
64
- const exceptions_1 = require("./exceptions");
65
71
  class HttpRequestHeaders extends axios_1.AxiosHeaders {
66
72
  }
67
73
  exports.HttpRequestHeaders = HttpRequestHeaders;
74
+ class HttpRequestException extends axios_1.AxiosError {
75
+ }
76
+ exports.HttpRequestException = HttpRequestException;
77
+ class HttpRequestAttemptException extends HttpRequestException {
78
+ }
79
+ exports.HttpRequestAttemptException = HttpRequestAttemptException;
68
80
  class HttpRequest {
69
81
  constructor(options = {}) {
70
82
  var _a, _b, _c, _d;
71
83
  /* Axios */
72
84
  this.instance = axios_1.default.create(Object.assign({}, Object.assign({
73
- ["headers"]: {},
74
85
  ["timeout"]: 30e3,
75
- ["retryLimit"]: 3,
76
- }, options, {
86
+ ["headers"]: {},
77
87
  ["proxy"]: false,
78
- ["retryCount"]: 0,
79
- })));
88
+ }, options)));
80
89
  /* Set Http Agent */
81
90
  this.instance.interceptors.request.use((request) => __awaiter(this, void 0, void 0, function* () {
82
91
  const jar = Object(request.jar);
@@ -147,7 +156,7 @@ class HttpRequest {
147
156
  request.data = request.body;
148
157
  }
149
158
  if (request.form) {
150
- if (request.form instanceof libs_1.FormData) {
159
+ if (request.form instanceof form_data_1.default) {
151
160
  request.data = request.form;
152
161
  }
153
162
  else {
@@ -162,20 +171,9 @@ class HttpRequest {
162
171
  if (request.baseURL)
163
172
  return request;
164
173
  if (request.serviceType == 1)
165
- request.baseURL = String(process.env.CORE_URL);
174
+ request.baseURL = process.env.CORE_URL;
166
175
  if (request.serviceType == 2)
167
- request.baseURL = String(process.env.SEKIRO_URL);
168
- if (request.serviceType == 3)
169
- request.baseURL = String("http://*:65000/script").replace("*", request.clientIp);
170
- return request;
171
- }));
172
- /* Validate Status */
173
- this.instance.interceptors.request.use((request) => __awaiter(this, void 0, void 0, function* () {
174
- if (request.serviceType == 1) { }
175
- if (request.serviceType == 2) { }
176
- if (request.serviceType == 3) {
177
- request.validateStatus = status => [200, 410, 500, 400].includes(status);
178
- }
176
+ request.baseURL = process.env.SEKIRO_URL;
179
177
  return request;
180
178
  }));
181
179
  /* Transform */
@@ -188,19 +186,10 @@ class HttpRequest {
188
186
  request.url = String("/business-demo/invoke");
189
187
  request.params = Object({ group, action, invoke_timeout });
190
188
  }
191
- if (request.serviceType == 3) {
192
- const args = JSON.stringify(request.args);
193
- request.form = Object({ args });
194
- }
195
189
  return request;
196
190
  }));
197
191
  /* Call Custom Hook */
198
192
  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);
199
- /* Save Origin Config */
200
- this.instance.interceptors.request.use((request) => __awaiter(this, void 0, void 0, function* () {
201
- request.__origin__ = (0, lodash_1.cloneDeep)(request);
202
- return (request);
203
- }));
204
193
  /* Call Custom Hook */
205
194
  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);
206
195
  /* Transform */
@@ -217,14 +206,6 @@ class HttpRequest {
217
206
  throw new common_1.ServiceUnavailableException(response.data.message);
218
207
  return response.data.data;
219
208
  }
220
- if (response.config.serviceType == 3) {
221
- if (response.status == 200)
222
- return response.data.result;
223
- const exception = String(response.data.exception);
224
- const reason = Array.from(response.data.args).at(0);
225
- const message = Array.from([exception, reason]).join("::");
226
- throw new common_1.ServiceUnavailableException(message);
227
- }
228
209
  if (response.config.html) {
229
210
  return new jsdom_1.JSDOM(response.data);
230
211
  }
@@ -235,33 +216,18 @@ class HttpRequest {
235
216
  }
236
217
  return response;
237
218
  }));
238
- /* Handler Timeout */
219
+ /* Handler Attempt */
239
220
  this.instance.interceptors.response.use((response) => __awaiter(this, void 0, void 0, function* () { return response; }), (error) => __awaiter(this, void 0, void 0, function* () {
240
221
  if (error instanceof axios_1.AxiosError) {
241
- if (!error.code)
242
- throw new exceptions_1.HttpRetryableError(error.config);
243
222
  switch (error.code) {
244
223
  case axios_1.AxiosError.ETIMEDOUT:
245
224
  case axios_1.AxiosError.ERR_NETWORK:
246
- case axios_1.AxiosError.ECONNABORTED:
247
- throw new exceptions_1.HttpRetryableError(error.config);
225
+ case axios_1.AxiosError.ECONNABORTED: throw new HttpRequestAttemptException(error.message, error.code, error.config, error.request, error.response);
226
+ default: throw new HttpRequestException(error.message, error.code, error.config, error.request, error.response);
248
227
  }
249
228
  }
250
229
  throw error;
251
230
  }));
252
- /* Auto Retry */
253
- this.instance.interceptors.response.use((response) => __awaiter(this, void 0, void 0, function* () { return response; }), (error) => __awaiter(this, void 0, void 0, function* () {
254
- if (error instanceof exceptions_1.HttpRetryableError) {
255
- const retryCount = error.config.__origin__.retryCount++;
256
- const retryLimit = error.config.__origin__.retryLimit;
257
- if (retryCount < retryLimit)
258
- return this.instance.request(error.config.__origin__);
259
- throw new exceptions_1.HttpRetryLimitError(error.config);
260
- }
261
- else {
262
- throw error;
263
- }
264
- }));
265
231
  }
266
232
  execute(options) {
267
233
  return __awaiter(this, void 0, void 0, function* () {
@@ -281,13 +247,18 @@ class HttpRequest {
281
247
  .request(Object.assign(Object.assign({}, opts), { method: "POST", serviceType: 2, data, path }));
282
248
  });
283
249
  }
284
- call(url_1) {
285
- return __awaiter(this, arguments, void 0, function* (url, args = [], opts = {}) {
286
- return this.instance
287
- .request(Object.assign(Object.assign({}, opts), { method: "POST", serviceType: 3, args, url }));
288
- });
289
- }
290
250
  }
291
251
  exports.HttpRequest = HttpRequest;
292
- __exportStar(require("./libs"), exports);
293
- __exportStar(require("./exceptions"), exports);
252
+ __decorate([
253
+ (0, async_retry_decorator_1.Attempt)({
254
+ retries: 2,
255
+ ignore: [Error],
256
+ onError(e) {
257
+ if (e instanceof HttpRequestAttemptException)
258
+ throw e;
259
+ },
260
+ }),
261
+ __metadata("design:type", Function),
262
+ __metadata("design:paramtypes", [Object]),
263
+ __metadata("design:returntype", Promise)
264
+ ], HttpRequest.prototype, "execute", null);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ekhein/http-request",
3
- "version": "1.0.16",
3
+ "version": "1.0.18",
4
4
  "license": "MIT",
5
5
  "author": "ekhein",
6
6
  "main": "./dist/main.js",
@@ -15,6 +15,7 @@
15
15
  "node": ">=18.0.0"
16
16
  },
17
17
  "devDependencies": {
18
+ "@types/async-retry": "^1.4.9",
18
19
  "@types/jsdom": "^21.1.7",
19
20
  "@types/lodash": "^4.17.20",
20
21
  "@types/node": "^22.15.18",
@@ -23,7 +24,9 @@
23
24
  "typescript": "^5.8.3"
24
25
  },
25
26
  "dependencies": {
27
+ "@ekhein/async-retry-decorator": "^1.10.3",
26
28
  "@nestjs/common": "^10.0.0",
29
+ "async-retry": "^1.3.3",
27
30
  "axios": "^1.12.2",
28
31
  "cookie": "^1.0.2",
29
32
  "form-data": "^4.0.4",
@@ -15,7 +15,6 @@ export interface HttpPhone4GProxy {
15
15
  export interface HttpServiceTypes {
16
16
  NATIVE: 1
17
17
  SEKIRO: 2
18
- DEVICE: 3
19
18
  }
20
19
 
21
20
  export interface ExtendInternalAxiosRequestConfig extends HttpRequestConfig, InternalAxiosRequestConfig {
@@ -31,7 +30,6 @@ export interface ExtendAxiosRequestConfig extends Record<string, any> {
31
30
  body?: any
32
31
  html?: boolean
33
32
  resolveBodyOnly?: boolean
34
- retryLimit?: number
35
33
  serviceType?: HttpServiceTypes[keyof HttpServiceTypes]
36
34
  useProxy?: boolean | HttpLiuGuanProxy | HttpPhone4GProxy
37
35
  jar?: CookieJar