@ekhein/http-request 2.0.2 → 2.0.4

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.
@@ -6,4 +6,6 @@ export declare class HttpRequest {
6
6
  execute<R = any>(options: HttpRequestConfig): Promise<R>;
7
7
  post<R = any>(url: string, data?: any, opts?: HttpRequestConfig): Promise<R>;
8
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, params: ArrayLike<any>, opts?: HttpRequestConfig): Promise<R>;
9
11
  }
@@ -55,10 +55,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
55
55
  };
56
56
  Object.defineProperty(exports, "__esModule", { value: true });
57
57
  exports.HttpRequest = void 0;
58
- const axios_1 = __importDefault(require("axios"));
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
+ const crypto_1 = require("crypto");
62
63
  const lodash_1 = require("lodash");
63
64
  const path_to_regexp_1 = require("path-to-regexp");
64
65
  const tough_cookie_1 = require("tough-cookie");
@@ -159,26 +160,31 @@ class HttpRequest {
159
160
  }
160
161
  return request;
161
162
  }));
162
- /* BaseURL */
163
- this.instance.interceptors.request.use((request) => __awaiter(this, void 0, void 0, function* () {
164
- if (request.baseURL)
165
- return request;
166
- if (request.serviceType == 1)
167
- request.baseURL = process.env.CORE_URL;
168
- if (request.serviceType == 2)
169
- request.baseURL = process.env.SEKIRO_URL;
170
- return request;
171
- }));
172
163
  /* Transform */
173
164
  this.instance.interceptors.request.use((request) => __awaiter(this, void 0, void 0, function* () {
174
- if (request.serviceType == 1) { }
165
+ if (request.serviceType == 1) {
166
+ request.baseURL = String(process.env.CORE_URL);
167
+ }
175
168
  if (request.serviceType == 2) {
176
169
  const group = (0, path_to_regexp_1.compile)(request.path)(request.params).split("/").at(-2);
177
170
  const action = (0, path_to_regexp_1.compile)(request.path)(request.params).split("/").at(-1);
178
171
  const invoke_timeout = Number(request.timeout - 5e3);
179
172
  request.params = Object({ group, action, invoke_timeout });
173
+ request.baseURL = String(process.env.SEKIRO_URL);
180
174
  request.url = String("/business-demo/invoke");
181
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}:3000/`);
185
+ request.body.jsonrpc = String("2.0");
186
+ request.body.id = (0, crypto_1.randomUUID)();
187
+ }
182
188
  return request;
183
189
  }));
184
190
  /* Request Hook */
@@ -199,6 +205,16 @@ class HttpRequest {
199
205
  throw new common_1.ServiceUnavailableException(response.data.message);
200
206
  return response.data.data;
201
207
  }
208
+ if (response.config.serviceType == 3) {
209
+ if (response.status != axios_1.HttpStatusCode.Ok)
210
+ throw new common_1.ServiceUnavailableException(response.data.exception);
211
+ return response.data.result;
212
+ }
213
+ if (response.config.serviceType == 4) {
214
+ if (response.data.error)
215
+ throw new common_1.ServiceUnavailableException(response.data.error.message);
216
+ return response.data.result;
217
+ }
202
218
  if (response.config.html) {
203
219
  return new jsdom_1.JSDOM(response.data);
204
220
  }
@@ -228,6 +244,18 @@ class HttpRequest {
228
244
  .request(Object.assign(Object.assign({}, opts), { method: "POST", serviceType: 2, data, path }));
229
245
  });
230
246
  }
247
+ hook(url_1, args_1) {
248
+ return __awaiter(this, arguments, void 0, function* (url, args, opts = {}) {
249
+ return this.instance
250
+ .request(Object.assign(Object.assign({}, opts), { method: "POST", serviceType: 3, args, url }));
251
+ });
252
+ }
253
+ call(method_1, params_1) {
254
+ return __awaiter(this, arguments, void 0, function* (method, params, opts = {}) {
255
+ return this.instance
256
+ .request(Object.assign(Object.assign({}, opts), { method: "POST", serviceType: 4, body: { method, params } }));
257
+ });
258
+ }
231
259
  }
232
260
  exports.HttpRequest = HttpRequest;
233
261
  __decorate([
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ekhein/http-request",
3
- "version": "2.0.2",
3
+ "version": "2.0.4",
4
4
  "license": "MIT",
5
5
  "author": "ekhein",
6
6
  "main": "./dist/index.js",
@@ -15,6 +15,8 @@ export interface HttpAndroidProxy {
15
15
  export interface HttpServiceTypes {
16
16
  NATIVE: 1
17
17
  SEKIRO: 2
18
+ SCRIPT: 3
19
+ JAYSON: 4
18
20
  }
19
21
 
20
22
  export interface ExtendInternalAxiosRequestConfig extends HttpRequestConfig, InternalAxiosRequestConfig {
@@ -29,6 +31,7 @@ export interface ExtendAxiosRequestConfig extends Record<string, any> {
29
31
  form?: any
30
32
  body?: any
31
33
  html?: boolean
34
+ clientIp?: string
32
35
  resolveBodyOnly?: boolean
33
36
  serviceType?: HttpServiceTypes[keyof HttpServiceTypes]
34
37
  useProxy?: boolean | HttpLiuGuanProxy | HttpAndroidProxy