@ekhein/http-request 1.0.6 → 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.
@@ -1,2 +1,2 @@
1
- export { AxiosRetryableError } from './axiosRetryableError';
2
- export { AxiosRetryLimitError } from './axiosRetryLimitError';
1
+ export { HttpRetryableError } from './retryableError';
2
+ export { HttpRetryLimitError } from './retryLimitError';
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AxiosRetryLimitError = exports.AxiosRetryableError = void 0;
4
- var axiosRetryableError_1 = require("./axiosRetryableError");
5
- Object.defineProperty(exports, "AxiosRetryableError", { enumerable: true, get: function () { return axiosRetryableError_1.AxiosRetryableError; } });
6
- var axiosRetryLimitError_1 = require("./axiosRetryLimitError");
7
- Object.defineProperty(exports, "AxiosRetryLimitError", { enumerable: true, get: function () { return axiosRetryLimitError_1.AxiosRetryLimitError; } });
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,4 @@
1
+ import { AxiosError, InternalAxiosRequestConfig } from "axios";
2
+ export declare class HttpRetryLimitError extends AxiosError {
3
+ constructor(config: InternalAxiosRequestConfig);
4
+ }
@@ -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,4 @@
1
+ import { AxiosError, InternalAxiosRequestConfig } from "axios";
2
+ export declare class HttpRetryableError extends AxiosError {
3
+ constructor(config: InternalAxiosRequestConfig);
4
+ }
@@ -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,3 @@
1
+ import { default as _FormData } from "form-data";
2
+ export declare class FormData extends _FormData {
3
+ }
@@ -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';
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FormData = void 0;
4
+ var formData_1 = require("./formData");
5
+ Object.defineProperty(exports, "FormData", { enumerable: true, get: function () { return formData_1.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
12
  export type * from '../types/http-request';
package/dist/main.js CHANGED
@@ -54,6 +54,7 @@ const cookie_1 = require("cookie");
54
54
  const tldts_1 = require("tldts");
55
55
  const common_1 = require("@nestjs/common");
56
56
  const http_error_by_code_util_1 = require("@nestjs/common/utils/http-error-by-code.util");
57
+ const libs_1 = require("./libs");
57
58
  const agent_1 = require("./agent");
58
59
  const agent_2 = require("./agent");
59
60
  const agent_3 = require("./agent");
@@ -137,8 +138,13 @@ class HttpRequest {
137
138
  request.data = request.body;
138
139
  }
139
140
  if (request.form) {
140
- request.headers.setContentType("application/x-www-form-urlencoded");
141
- request.data = request.form;
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
+ }
142
148
  }
143
149
  return request;
144
150
  }));
@@ -161,49 +167,54 @@ class HttpRequest {
161
167
  request.__origin__ = (0, lodash_1.cloneDeep)(request);
162
168
  return (request);
163
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);
164
172
  /* Transform */
165
173
  this.instance.interceptors.response.use((response) => __awaiter(this, void 0, void 0, function* () {
166
- if (response.config.serviceType == 1) {
167
- if (http_error_by_code_util_1.HttpErrorByCode[response.data.err])
168
- throw new http_error_by_code_util_1.HttpErrorByCode[response.data.err](response.data.msg);
169
- return response.data.res;
170
- }
171
- if (response.config.serviceType == 2) {
172
- if (response.data.status)
173
- throw new common_1.ServiceUnavailableException(response.data.message);
174
- return response.data.data;
175
- }
176
- if (response.config.resolveBodyOnly != false) {
177
- if (response.config.html != true)
178
- return response.data;
179
- return new jsdom_1.JSDOM(response.data);
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
+ }
180
193
  }
181
194
  return response;
182
195
  }));
183
- /* Call Custom Hook */
184
- 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);
185
196
  /* Handler Timeout */
186
197
  this.instance.interceptors.response.use((response) => __awaiter(this, void 0, void 0, function* () { return response; }), (error) => __awaiter(this, void 0, void 0, function* () {
187
198
  if (error instanceof axios_1.AxiosError) {
188
199
  if (!error.code)
189
- throw new exceptions_1.AxiosRetryableError(error.config);
200
+ throw new exceptions_1.HttpRetryableError(error.config);
190
201
  switch (error.code) {
191
202
  case axios_1.AxiosError.ETIMEDOUT:
192
203
  case axios_1.AxiosError.ERR_NETWORK:
193
204
  case axios_1.AxiosError.ECONNABORTED:
194
- throw new exceptions_1.AxiosRetryableError(error.config);
205
+ throw new exceptions_1.HttpRetryableError(error.config);
195
206
  }
196
207
  }
197
208
  throw error;
198
209
  }));
199
210
  /* Auto Retry */
200
211
  this.instance.interceptors.response.use((response) => __awaiter(this, void 0, void 0, function* () { return response; }), (error) => __awaiter(this, void 0, void 0, function* () {
201
- if (error instanceof exceptions_1.AxiosRetryableError) {
212
+ if (error instanceof exceptions_1.HttpRetryableError) {
202
213
  const retryCount = error.config.__origin__.retryCount++;
203
214
  const retryLimit = error.config.__origin__.retryLimit;
204
215
  if (retryCount < retryLimit)
205
216
  return this.instance.request(error.config.__origin__);
206
- throw new exceptions_1.AxiosRetryLimitError(error.config);
217
+ throw new exceptions_1.HttpRetryLimitError(error.config);
207
218
  }
208
219
  else {
209
220
  throw error;
@@ -230,4 +241,5 @@ class HttpRequest {
230
241
  }
231
242
  }
232
243
  exports.HttpRequest = HttpRequest;
244
+ __exportStar(require("./libs"), exports);
233
245
  __exportStar(require("./exceptions"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ekhein/http-request",
3
- "version": "1.0.6",
3
+ "version": "1.0.7",
4
4
  "license": "MIT",
5
5
  "author": "ekhein",
6
6
  "main": "./dist/main.js",
@@ -25,6 +25,7 @@
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
30
  "jsdom": "^25.0.1",
30
31
  "lodash": "^4.17.21",
@@ -1,5 +1,6 @@
1
- import { AxiosRequestConfig, InternalAxiosRequestConfig, AxiosResponse, AxiosRequestHeaders } from "axios"
2
- import { CookieJar } from "tough-cookie"
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 {}