@ekhein/http-request 1.0.18 → 1.0.20

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.
@@ -0,0 +1,5 @@
1
+ import { AxiosResponse, InternalAxiosRequestConfig } from 'axios';
2
+ import { HttpRequestException } from './request.exception';
3
+ export declare class HttpRequestAttemptException extends HttpRequestException {
4
+ constructor(message: string, config?: InternalAxiosRequestConfig, request?: any, response?: AxiosResponse);
5
+ }
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HttpRequestAttemptException = void 0;
4
+ const request_exception_1 = require("./request.exception");
5
+ class HttpRequestAttemptException extends request_exception_1.HttpRequestException {
6
+ constructor(message, config, request, response) {
7
+ super(message, "ERR_REQ_ATTEMPT", config, request, response);
8
+ }
9
+ }
10
+ exports.HttpRequestAttemptException = HttpRequestAttemptException;
@@ -1,2 +1,2 @@
1
- export { HttpRetryableError } from './retryableError';
2
- export { HttpRetryLimitError } from './retryLimitError';
1
+ export { HttpRequestAttemptException } from './attempt.exception';
2
+ export { HttpRequestException } from './request.exception';
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
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; } });
3
+ exports.HttpRequestException = exports.HttpRequestAttemptException = void 0;
4
+ var attempt_exception_1 = require("./attempt.exception");
5
+ Object.defineProperty(exports, "HttpRequestAttemptException", { enumerable: true, get: function () { return attempt_exception_1.HttpRequestAttemptException; } });
6
+ var request_exception_1 = require("./request.exception");
7
+ Object.defineProperty(exports, "HttpRequestException", { enumerable: true, get: function () { return request_exception_1.HttpRequestException; } });
@@ -0,0 +1 @@
1
+ export { AxiosError as HttpRequestException } from 'axios';
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HttpRequestException = void 0;
4
+ var axios_1 = require("axios");
5
+ Object.defineProperty(exports, "HttpRequestException", { enumerable: true, get: function () { return axios_1.AxiosError; } });
@@ -0,0 +1 @@
1
+ export { HttpRequestHeaders } from './request.headers';
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HttpRequestHeaders = void 0;
4
+ var request_headers_1 = require("./request.headers");
5
+ Object.defineProperty(exports, "HttpRequestHeaders", { enumerable: true, get: function () { return request_headers_1.HttpRequestHeaders; } });
@@ -0,0 +1 @@
1
+ export { AxiosHeaders as HttpRequestHeaders } from 'axios';
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HttpRequestHeaders = void 0;
4
+ var axios_1 = require("axios");
5
+ Object.defineProperty(exports, "HttpRequestHeaders", { enumerable: true, get: function () { return axios_1.AxiosHeaders; } });
@@ -0,0 +1,9 @@
1
+ import { AxiosInstance } from 'axios';
2
+ import { HttpRequestConfig } from '../types';
3
+ export declare class HttpRequest {
4
+ readonly instance: AxiosInstance;
5
+ constructor(options?: HttpRequestConfig);
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
+ }
@@ -54,29 +54,21 @@ 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 = exports.FormData = exports.HttpRequestAttemptException = exports.HttpRequestException = exports.HttpRequestHeaders = void 0;
58
- const axios_1 = __importStar(require("axios"));
57
+ exports.HttpRequest = void 0;
58
+ const axios_1 = __importDefault(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
- const form_data_1 = __importDefault(require("form-data"));
63
- Object.defineProperty(exports, "FormData", { enumerable: true, get: function () { return form_data_1.default; } });
64
62
  const tough_cookie_1 = require("tough-cookie");
65
63
  const common_1 = require("@nestjs/common");
66
64
  const async_retry_decorator_1 = require("@ekhein/async-retry-decorator");
67
65
  const http_error_by_code_util_1 = require("@nestjs/common/utils/http-error-by-code.util");
66
+ const libs_1 = require("./libs");
67
+ const headers_1 = require("./headers");
68
+ const exceptions_1 = require("./exceptions");
68
69
  const agent_1 = require("./agent");
69
70
  const agent_2 = require("./agent");
70
71
  const agent_3 = require("./agent");
71
- class HttpRequestHeaders extends axios_1.AxiosHeaders {
72
- }
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;
80
72
  class HttpRequest {
81
73
  constructor(options = {}) {
82
74
  var _a, _b, _c, _d;
@@ -86,7 +78,7 @@ class HttpRequest {
86
78
  ["headers"]: {},
87
79
  ["proxy"]: false,
88
80
  }, options)));
89
- /* Set Http Agent */
81
+ /* Agent */
90
82
  this.instance.interceptors.request.use((request) => __awaiter(this, void 0, void 0, function* () {
91
83
  const jar = Object(request.jar);
92
84
  const cookies = Object({ jar });
@@ -129,13 +121,13 @@ class HttpRequest {
129
121
  }
130
122
  return request;
131
123
  }));
132
- /* Set Cookie Jar */
124
+ /* Cookie Jar */
133
125
  this.instance.interceptors.request.use((request) => __awaiter(this, void 0, void 0, function* () {
134
126
  if (!request.jar)
135
127
  request.jar = new tough_cookie_1.CookieJar();
136
128
  return request;
137
129
  }));
138
- /* Headers Format */
130
+ /* Headers */
139
131
  this.instance.interceptors.request.use((request) => __awaiter(this, void 0, void 0, function* () {
140
132
  if (request.headers) {
141
133
  const headers = Object.entries(request.headers);
@@ -149,14 +141,14 @@ class HttpRequest {
149
141
  }
150
142
  return request;
151
143
  }));
152
- /* Set Body */
144
+ /* Body */
153
145
  this.instance.interceptors.request.use((request) => __awaiter(this, void 0, void 0, function* () {
154
146
  if (request.body) {
155
147
  request.headers.setContentType("application/json");
156
148
  request.data = request.body;
157
149
  }
158
150
  if (request.form) {
159
- if (request.form instanceof form_data_1.default) {
151
+ if (request.form instanceof libs_1.FormData) {
160
152
  request.data = request.form;
161
153
  }
162
154
  else {
@@ -188,14 +180,14 @@ class HttpRequest {
188
180
  }
189
181
  return request;
190
182
  }));
191
- /* Call Custom Hook */
183
+ /* Request Hook */
192
184
  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
- /* Call Custom Hook */
185
+ /* Response Hook */
194
186
  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);
195
187
  /* Transform */
196
188
  this.instance.interceptors.response.use((response) => __awaiter(this, void 0, void 0, function* () {
197
189
  if (typeof response.headers == "object") {
198
- if (response.headers instanceof axios_1.AxiosHeaders) {
190
+ if (response.headers instanceof headers_1.HttpRequestHeaders) {
199
191
  if (response.config.serviceType == 1) {
200
192
  if (http_error_by_code_util_1.HttpErrorByCode[response.data.err])
201
193
  throw new http_error_by_code_util_1.HttpErrorByCode[response.data.err](response.data.msg);
@@ -216,18 +208,6 @@ class HttpRequest {
216
208
  }
217
209
  return response;
218
210
  }));
219
- /* Handler Attempt */
220
- this.instance.interceptors.response.use((response) => __awaiter(this, void 0, void 0, function* () { return response; }), (error) => __awaiter(this, void 0, void 0, function* () {
221
- if (error instanceof axios_1.AxiosError) {
222
- switch (error.code) {
223
- case axios_1.AxiosError.ETIMEDOUT:
224
- case axios_1.AxiosError.ERR_NETWORK:
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);
227
- }
228
- }
229
- throw error;
230
- }));
231
211
  }
232
212
  execute(options) {
233
213
  return __awaiter(this, void 0, void 0, function* () {
@@ -253,9 +233,16 @@ __decorate([
253
233
  (0, async_retry_decorator_1.Attempt)({
254
234
  retries: 2,
255
235
  ignore: [Error],
256
- onError(e) {
257
- if (e instanceof HttpRequestAttemptException)
258
- throw e;
236
+ onError(error) {
237
+ if (error instanceof exceptions_1.HttpRequestAttemptException)
238
+ throw error;
239
+ if (error instanceof exceptions_1.HttpRequestException) {
240
+ switch (error.code) {
241
+ case exceptions_1.HttpRequestException.ETIMEDOUT:
242
+ case exceptions_1.HttpRequestException.ERR_NETWORK:
243
+ case exceptions_1.HttpRequestException.ECONNABORTED: throw error;
244
+ }
245
+ }
259
246
  },
260
247
  }),
261
248
  __metadata("design:type", Function),
@@ -0,0 +1,4 @@
1
+ export * from './exceptions';
2
+ export * from './libs';
3
+ export * from './headers';
4
+ export * from './http-request';
package/dist/index.js ADDED
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./exceptions"), exports);
18
+ __exportStar(require("./libs"), exports);
19
+ __exportStar(require("./headers"), exports);
20
+ __exportStar(require("./http-request"), exports);
@@ -0,0 +1 @@
1
+ export { default as FormData } from 'form-data';
@@ -4,7 +4,5 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
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;
7
+ var form_data_1 = require("form-data");
8
+ Object.defineProperty(exports, "FormData", { enumerable: true, get: function () { return __importDefault(form_data_1).default; } });
@@ -1 +1 @@
1
- export { FormData } from './formData';
1
+ export { FormData } from './form-data';
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.FormData = void 0;
4
- var formData_1 = require("./formData");
5
- Object.defineProperty(exports, "FormData", { enumerable: true, get: function () { return formData_1.FormData; } });
4
+ var form_data_1 = require("./form-data");
5
+ Object.defineProperty(exports, "FormData", { enumerable: true, get: function () { return form_data_1.FormData; } });
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@ekhein/http-request",
3
- "version": "1.0.18",
3
+ "version": "1.0.20",
4
4
  "license": "MIT",
5
5
  "author": "ekhein",
6
- "main": "./dist/main.js",
6
+ "main": "./dist/index.js",
7
+ "types": "./types/index.d.ts",
7
8
  "files": [
8
- "dist",
9
- "types"
9
+ "dist"
10
10
  ],
11
11
  "scripts": {
12
12
  "build": "tsc"
@@ -24,7 +24,7 @@
24
24
  "typescript": "^5.8.3"
25
25
  },
26
26
  "dependencies": {
27
- "@ekhein/async-retry-decorator": "^1.10.3",
27
+ "@ekhein/async-retry-decorator": "^1.10.5",
28
28
  "@nestjs/common": "^10.0.0",
29
29
  "async-retry": "^1.3.3",
30
30
  "axios": "^1.12.2",
@@ -1,4 +0,0 @@
1
- import { AxiosError, InternalAxiosRequestConfig } from "axios";
2
- export declare class HttpRetryLimitError extends AxiosError {
3
- constructor(config: InternalAxiosRequestConfig);
4
- }
@@ -1,12 +0,0 @@
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;
@@ -1,4 +0,0 @@
1
- import { AxiosError, InternalAxiosRequestConfig } from "axios";
2
- export declare class HttpRetryableError extends AxiosError {
3
- constructor(config: InternalAxiosRequestConfig);
4
- }
@@ -1,12 +0,0 @@
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;
@@ -1,3 +0,0 @@
1
- import { default as _FormData } from "form-data";
2
- export declare class FormData extends _FormData {
3
- }
package/dist/main.d.ts DELETED
@@ -1,18 +0,0 @@
1
- import { AxiosError, AxiosHeaders, AxiosInstance } from "axios";
2
- import { default as FormData } from 'form-data';
3
- import { HttpRequestConfig } from "../types/http-request";
4
- export declare class HttpRequestHeaders extends AxiosHeaders {
5
- }
6
- export declare class HttpRequestException extends AxiosError {
7
- }
8
- export declare class HttpRequestAttemptException extends HttpRequestException {
9
- }
10
- export { FormData };
11
- export declare class HttpRequest {
12
- readonly instance: AxiosInstance;
13
- constructor(options?: HttpRequestConfig);
14
- execute<R = any>(options: HttpRequestConfig): Promise<R>;
15
- post<R = any>(url: string, data?: any, opts?: HttpRequestConfig): Promise<R>;
16
- invoke<R = any>(path: string, data: object, opts?: HttpRequestConfig): Promise<R>;
17
- }
18
- export type * from '../types/http-request';
package/types/axios.d.ts DELETED
@@ -1,5 +0,0 @@
1
- import { ExtendAxiosRequestConfig } from "./http-request"
2
-
3
- declare module "axios" {
4
- interface AxiosRequestConfig extends ExtendAxiosRequestConfig {}
5
- }
@@ -1,48 +0,0 @@
1
- import { JSDOM } from 'jsdom';
2
- import { CookieJar } from 'tough-cookie';
3
- import { AxiosRequestConfig, InternalAxiosRequestConfig, AxiosResponse, AxiosRequestHeaders } from 'axios';
4
-
5
- export interface HttpLiuGuanProxy {
6
- adcode?: string
7
- visitorId?: string
8
- unbindTime?: string
9
- }
10
-
11
- export interface HttpPhone4GProxy {
12
- clientIp: string
13
- }
14
-
15
- export interface HttpServiceTypes {
16
- NATIVE: 1
17
- SEKIRO: 2
18
- }
19
-
20
- export interface ExtendInternalAxiosRequestConfig extends HttpRequestConfig, InternalAxiosRequestConfig {
21
- headers: AxiosRequestHeaders;
22
- }
23
-
24
- export interface ExtendAxiosResponse extends AxiosResponse {
25
- config: ExtendInternalAxiosRequestConfig
26
- }
27
-
28
- export interface ExtendAxiosRequestConfig extends Record<string, any> {
29
- form?: any
30
- body?: any
31
- html?: boolean
32
- resolveBodyOnly?: boolean
33
- serviceType?: HttpServiceTypes[keyof HttpServiceTypes]
34
- useProxy?: boolean | HttpLiuGuanProxy | HttpPhone4GProxy
35
- jar?: CookieJar
36
- interceptorHooks?: {
37
- requestInterceptor?: (config: ExtendInternalAxiosRequestConfig) => ExtendInternalAxiosRequestConfig | Promise<ExtendInternalAxiosRequestConfig>
38
- requestInterceptorCatch?: (error: any) => any
39
- respondInterceptor?: (response: ExtendAxiosResponse) => ExtendAxiosResponse | Promise<ExtendAxiosResponse | any>
40
- respondInterceptorCatch?: (error: any) => any
41
- }
42
- }
43
-
44
- export interface HttpRequestConfig extends ExtendAxiosRequestConfig, AxiosRequestConfig {}
45
-
46
- export interface HttpResponse extends ExtendAxiosResponse {}
47
-
48
- export interface HTMLResponse extends JSDOM {}