@gopowerteam/request 0.1.19 → 0.1.21

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.
@@ -67,7 +67,7 @@ var _AxiosAdapter = class {
67
67
  encodeValuesOnly: true,
68
68
  encode: true,
69
69
  sort: (a, b) => a.localeCompare(b),
70
- filter: (_, value) => ["", void 0, null].some((v) => v === value) ? void 0 : value
70
+ filter: (_, value) => ["", void 0, null].includes(value) ? void 0 : value
71
71
  }
72
72
  )
73
73
  }
@@ -77,7 +77,6 @@ var _AxiosAdapter = class {
77
77
  }
78
78
  /**
79
79
  * 发送请求
80
- * @param options 请求参数
81
80
  * @returns AxiosResponse
82
81
  */
83
82
  request({
@@ -103,7 +102,7 @@ var _AxiosAdapter = class {
103
102
  /**
104
103
  * 转换Response
105
104
  * @param response
106
- * @returns
105
+ * @returns AdapterResponse
107
106
  */
108
107
  transformResponse(response) {
109
108
  return {
@@ -115,8 +114,7 @@ var _AxiosAdapter = class {
115
114
  }
116
115
  /**
117
116
  * 转换Response
118
- * @param response
119
- * @returns
117
+ * @returns AdapterResponse
120
118
  */
121
119
  transformException(exception) {
122
120
  return {
@@ -1,5 +1,5 @@
1
- import { c as RequestAdapter, R as RequestSetupConfig, d as RequestAdapterOptions, A as AdapterResponse } from '../request-plugin.interface-3df174bc.js';
2
1
  import { AxiosResponse, AxiosError } from 'axios';
2
+ import { c as RequestAdapter, R as RequestSetupConfig, d as RequestAdapterOptions, A as AdapterResponse } from '../request-adapter.interface-fef4fe40.js';
3
3
  import 'qs';
4
4
 
5
5
  declare class AxiosAdapter implements RequestAdapter {
@@ -12,20 +12,18 @@ declare class AxiosAdapter implements RequestAdapter {
12
12
  private getAxiosInstance;
13
13
  /**
14
14
  * 发送请求
15
- * @param options 请求参数
16
15
  * @returns AxiosResponse
17
16
  */
18
- request({ baseURL, pathURL, headers, method, paramsQuery, paramsBody, extraParams }: RequestAdapterOptions): Promise<AxiosResponse<any, any>>;
17
+ request({ baseURL, pathURL, headers, method, paramsQuery, paramsBody, extraParams, }: RequestAdapterOptions): Promise<AxiosResponse<any, any>>;
19
18
  /**
20
19
  * 转换Response
21
20
  * @param response
22
- * @returns
21
+ * @returns AdapterResponse
23
22
  */
24
23
  transformResponse(response: AxiosResponse): AdapterResponse;
25
24
  /**
26
25
  * 转换Response
27
- * @param response
28
- * @returns
26
+ * @returns AdapterResponse
29
27
  */
30
28
  transformException(exception: AxiosError): AdapterResponse;
31
29
  }
@@ -30,7 +30,7 @@ var _AxiosAdapter = class {
30
30
  encodeValuesOnly: true,
31
31
  encode: true,
32
32
  sort: (a, b) => a.localeCompare(b),
33
- filter: (_, value) => ["", void 0, null].some((v) => v === value) ? void 0 : value
33
+ filter: (_, value) => ["", void 0, null].includes(value) ? void 0 : value
34
34
  }
35
35
  )
36
36
  }
@@ -40,7 +40,6 @@ var _AxiosAdapter = class {
40
40
  }
41
41
  /**
42
42
  * 发送请求
43
- * @param options 请求参数
44
43
  * @returns AxiosResponse
45
44
  */
46
45
  request({
@@ -66,7 +65,7 @@ var _AxiosAdapter = class {
66
65
  /**
67
66
  * 转换Response
68
67
  * @param response
69
- * @returns
68
+ * @returns AdapterResponse
70
69
  */
71
70
  transformResponse(response) {
72
71
  return {
@@ -78,8 +77,7 @@ var _AxiosAdapter = class {
78
77
  }
79
78
  /**
80
79
  * 转换Response
81
- * @param response
82
- * @returns
80
+ * @returns AdapterResponse
83
81
  */
84
82
  transformException(exception) {
85
83
  return {
package/dist/index.cjs CHANGED
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
6
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
9
7
  var __export = (target, all) => {
@@ -18,14 +16,6 @@ var __copyProps = (to, from, except, desc) => {
18
16
  }
19
17
  return to;
20
18
  };
21
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
22
- // If the importer is in node compatibility mode or this is not an ESM
23
- // file that has been converted to a CommonJS file using a Babel-
24
- // compatible transform (i.e. "__esModule" has not been set), then set
25
- // "default" to the CommonJS "module.exports" for node compatibility.
26
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
27
- mod
28
- ));
29
19
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
30
20
  var __publicField = (obj, key, value) => {
31
21
  __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
@@ -43,8 +33,12 @@ __export(src_exports, {
43
33
  });
44
34
  module.exports = __toCommonJS(src_exports);
45
35
 
46
- // src/request-service.ts
47
- var import_qs = __toESM(require("qs"), 1);
36
+ // src/interfaces/request-generate.interface.ts
37
+ var RequestGenerateType = /* @__PURE__ */ ((RequestGenerateType2) => {
38
+ RequestGenerateType2["Request"] = "TO_REQUEST";
39
+ RequestGenerateType2["URL"] = "TO_URL";
40
+ return RequestGenerateType2;
41
+ })(RequestGenerateType || {});
48
42
 
49
43
  // src/interfaces/request-plugin.interface.ts
50
44
  var PluginLifecycle = /* @__PURE__ */ ((PluginLifecycle2) => {
@@ -54,6 +48,107 @@ var PluginLifecycle = /* @__PURE__ */ ((PluginLifecycle2) => {
54
48
  return PluginLifecycle2;
55
49
  })(PluginLifecycle || {});
56
50
 
51
+ // src/interfaces/request-send.interface.ts
52
+ var RequestMethod = /* @__PURE__ */ ((RequestMethod2) => {
53
+ RequestMethod2["Get"] = "GET";
54
+ RequestMethod2["Post"] = "POST";
55
+ RequestMethod2["Put"] = "PUT";
56
+ RequestMethod2["Delete"] = "DELETE";
57
+ RequestMethod2["Options"] = "OPTIONS";
58
+ RequestMethod2["Head"] = "HEAD";
59
+ RequestMethod2["Patch"] = "PATCH";
60
+ return RequestMethod2;
61
+ })(RequestMethod || {});
62
+
63
+ // src/utils/query-string.ts
64
+ function encodeValue(value, encode) {
65
+ if (!encode)
66
+ return value;
67
+ return encodeURIComponent(value);
68
+ }
69
+ function getKeyWithDot(key, childKey, allowDots) {
70
+ return allowDots ? `${key}.${childKey}` : `${key}[${childKey}]`;
71
+ }
72
+ function processValue(value, options, parentKey = "") {
73
+ const result = [];
74
+ if (value === null || value === void 0) {
75
+ if (!options.skipNulls) {
76
+ result.push(`${parentKey}=`);
77
+ }
78
+ return result;
79
+ }
80
+ if (Array.isArray(value)) {
81
+ if (options.arrayFormat === "comma") {
82
+ const filteredValues = value.filter((item) => item !== null && item !== void 0);
83
+ if (filteredValues.length > 0) {
84
+ const commaValue = filteredValues.map(
85
+ (item) => typeof item === "object" ? JSON.stringify(item) : item.toString()
86
+ ).join(",");
87
+ const shouldEncode = options.encode !== false;
88
+ const encodedValue = encodeValue(commaValue, shouldEncode);
89
+ result.push(`${parentKey}=${encodedValue}`);
90
+ }
91
+ } else {
92
+ value.forEach((item, index) => {
93
+ let itemKey;
94
+ const shouldEncode = options.encode !== false;
95
+ if (options.arrayFormat === "indices") {
96
+ itemKey = parentKey ? `${parentKey}[${index}]` : `[${index}]`;
97
+ } else if (options.arrayFormat === "brackets") {
98
+ itemKey = parentKey ? `${parentKey}[]` : "[]";
99
+ } else {
100
+ itemKey = parentKey;
101
+ }
102
+ if (typeof item === "object" && item !== null) {
103
+ result.push(...processValue(item, options, itemKey));
104
+ } else {
105
+ const encodedValue = encodeValue(item.toString(), shouldEncode);
106
+ result.push(`${itemKey}=${encodedValue}`);
107
+ }
108
+ });
109
+ }
110
+ } else if (typeof value === "object") {
111
+ Object.keys(value).forEach((childKey) => {
112
+ const fullKey = parentKey ? getKeyWithDot(parentKey, childKey, options.allowDots || false) : childKey;
113
+ result.push(...processValue(value[childKey], options, fullKey));
114
+ });
115
+ } else {
116
+ const shouldEncode = options.encode !== false;
117
+ const encodedValue = encodeValue(value.toString(), shouldEncode);
118
+ const encodedKey = options.encodeValuesOnly ? parentKey : encodeValue(parentKey, shouldEncode);
119
+ result.push(`${encodedKey}=${encodedValue}`);
120
+ }
121
+ return result;
122
+ }
123
+ function stringify(obj, options = {}) {
124
+ const {
125
+ arrayFormat = "indices",
126
+ skipNulls = true,
127
+ allowDots = true,
128
+ encodeValuesOnly = false,
129
+ encode = true,
130
+ addQueryPrefix = false
131
+ } = options;
132
+ if (!obj || typeof obj !== "object") {
133
+ return options.addQueryPrefix ? "?" : "";
134
+ }
135
+ const parts = [];
136
+ Object.keys(obj).forEach((key) => {
137
+ const value = obj[key];
138
+ const processedValues = processValue(value, {
139
+ arrayFormat,
140
+ skipNulls,
141
+ allowDots,
142
+ encodeValuesOnly,
143
+ encode,
144
+ addQueryPrefix
145
+ }, key);
146
+ parts.push(...processedValues);
147
+ });
148
+ const queryString = parts.join("&");
149
+ return addQueryPrefix ? queryString ? `?${queryString}` : "?" : queryString;
150
+ }
151
+
57
152
  // src/request-service.ts
58
153
  var _RequestService = class {
59
154
  /**
@@ -67,7 +162,7 @@ var _RequestService = class {
67
162
  }
68
163
  /**
69
164
  * 获取RequestAdatper
70
- * @returns
165
+ * @returns RequestAdapter
71
166
  */
72
167
  getRequestAdapter() {
73
168
  if (!_RequestService.config.adapter) {
@@ -99,17 +194,19 @@ var _RequestService = class {
99
194
  }
100
195
  /**
101
196
  * 执行前置插件逻辑
102
- * @param plugins
103
- * @param options
104
197
  */
105
198
  execResponsePlugin(leftcycle, plugins = [], options, response) {
106
199
  plugins.forEach((plugin) => {
107
200
  const leftcycleFn = plugin[leftcycle];
108
- leftcycleFn && leftcycleFn.bind(plugin)(response, options);
201
+ if (leftcycleFn) {
202
+ leftcycleFn.bind(plugin)(response, options);
203
+ }
109
204
  });
110
205
  _RequestService.config.plugins.forEach((plugin) => {
111
206
  const leftcycleFn = plugin[leftcycle];
112
- leftcycleFn && leftcycleFn.bind(plugin)(response, options);
207
+ if (leftcycleFn) {
208
+ leftcycleFn.bind(plugin)(response, options);
209
+ }
113
210
  });
114
211
  }
115
212
  /**
@@ -132,7 +229,7 @@ var _RequestService = class {
132
229
  * 开始请求
133
230
  * @param adapter
134
231
  * @param options
135
- * @returns
232
+ * @returns Promise
136
233
  */
137
234
  startRequest(adapter, options, extraParams) {
138
235
  return adapter.request({
@@ -152,7 +249,7 @@ var _RequestService = class {
152
249
  /**
153
250
  * 执行拦截器
154
251
  * @param response 请求响应对象
155
- * @returns
252
+ * @returns Promise
156
253
  */
157
254
  execInterceptors(response, hasException = false) {
158
255
  const interceptors = _RequestService.config?.interceptors;
@@ -172,8 +269,8 @@ var _RequestService = class {
172
269
  /**
173
270
  * 发送请求
174
271
  * @param {RequestSendOptions} options 请求选项
175
- * @param {RequestPlugin[]} plugins 请求插件
176
- * @returns
272
+ * @param {RequestPlugin[]} requestPlugins 请求插件
273
+ * @returns Promise
177
274
  */
178
275
  async send(options, requestPlugins = []) {
179
276
  if (!_RequestService.config) {
@@ -198,14 +295,17 @@ var _RequestService = class {
198
295
  * 生成请求路径
199
296
  * @param {RequestSendOptions} options 请求选项
200
297
  * @param {RequestPlugin[]} plugins 请求插件
201
- * @returns
298
+ * @returns string
202
299
  */
203
300
  toURL(options, plugins = []) {
204
301
  if (!_RequestService.config) {
205
302
  throw new Error("\u8BF7\u68C0\u67E5\u8BF7\u6C42\u914D\u7F6E\u662F\u5426\u5B8C\u6210");
206
303
  }
207
304
  if (plugins && plugins.length) {
208
- this.execRequestPlugin(plugins, options);
305
+ this.execRequestPlugin(
306
+ plugins.filter(Boolean),
307
+ options
308
+ );
209
309
  }
210
310
  const baseURL = _RequestService.config.gateway;
211
311
  const pathURL = this.parseRequestPath(
@@ -213,16 +313,14 @@ var _RequestService = class {
213
313
  options.paramsPath,
214
314
  options.service
215
315
  );
216
- const queryString = import_qs.default.stringify(options.paramsQuery, {
217
- ...{
218
- arrayFormat: "repeat",
219
- skipNulls: true,
220
- allowDots: true,
221
- encodeValuesOnly: true,
222
- encode: true
223
- },
224
- ..._RequestService.config.qs || {},
225
- addQueryPrefix: true
316
+ const queryString = stringify(options.paramsQuery || {}, {
317
+ arrayFormat: "repeat",
318
+ skipNulls: true,
319
+ allowDots: true,
320
+ encodeValuesOnly: true,
321
+ encode: true,
322
+ addQueryPrefix: true,
323
+ ..._RequestService.config.qs || {}
226
324
  });
227
325
  return `${baseURL}${pathURL}${queryString}`;
228
326
  }
@@ -236,25 +334,6 @@ function setup(config) {
236
334
  RequestService.config = config;
237
335
  RequestService.config?.adapter?.injectConfig?.(config);
238
336
  }
239
-
240
- // src/interfaces/request-send.interface.ts
241
- var RequestMethod = /* @__PURE__ */ ((RequestMethod2) => {
242
- RequestMethod2["Get"] = "GET";
243
- RequestMethod2["Post"] = "POST";
244
- RequestMethod2["Put"] = "PUT";
245
- RequestMethod2["Delete"] = "DELETE";
246
- RequestMethod2["Options"] = "OPTIONS";
247
- RequestMethod2["Head"] = "HEAD";
248
- RequestMethod2["Patch"] = "PATCH";
249
- return RequestMethod2;
250
- })(RequestMethod || {});
251
-
252
- // src/interfaces/request-generate.interface.ts
253
- var RequestGenerateType = /* @__PURE__ */ ((RequestGenerateType2) => {
254
- RequestGenerateType2["Request"] = "TO_REQUEST";
255
- RequestGenerateType2["URL"] = "TO_URL";
256
- return RequestGenerateType2;
257
- })(RequestGenerateType || {});
258
337
  // Annotate the CommonJS export names for ESM import in node:
259
338
  0 && (module.exports = {
260
339
  PluginLifecycle,
package/dist/index.d.ts CHANGED
@@ -1,7 +1,15 @@
1
- import { R as RequestSetupConfig, a as RequestSendOptions, b as RequestPlugin } from './request-plugin.interface-3df174bc.js';
2
- export { A as AdapterResponse, e as AdapterResponseHeaders, P as PluginLifecycle, c as RequestAdapter, d as RequestAdapterOptions, f as RequestLifecycle, h as RequestMethod, i as ResponseInterceptor, g as ResponseLifecycle } from './request-plugin.interface-3df174bc.js';
1
+ import { R as RequestSetupConfig, a as RequestSendOptions, b as RequestPlugin } from './request-adapter.interface-fef4fe40.js';
2
+ export { A as AdapterResponse, e as AdapterResponseHeaders, P as PluginLifecycle, c as RequestAdapter, d as RequestAdapterOptions, f as RequestLifecycle, h as RequestMethod, i as ResponseInterceptor, g as ResponseLifecycle } from './request-adapter.interface-fef4fe40.js';
3
3
  import 'qs';
4
4
 
5
+ declare enum RequestGenerateType {
6
+ Request = "TO_REQUEST",
7
+ URL = "TO_URL"
8
+ }
9
+ interface RequestGenerateOptions {
10
+ type?: RequestGenerateType;
11
+ }
12
+
5
13
  declare class RequestService {
6
14
  static config: RequestSetupConfig;
7
15
  static instance: RequestService;
@@ -11,7 +19,7 @@ declare class RequestService {
11
19
  static getInstance(): RequestService;
12
20
  /**
13
21
  * 获取RequestAdatper
14
- * @returns
22
+ * @returns RequestAdapter
15
23
  */
16
24
  private getRequestAdapter;
17
25
  /**
@@ -22,8 +30,6 @@ declare class RequestService {
22
30
  private execRequestPlugin;
23
31
  /**
24
32
  * 执行前置插件逻辑
25
- * @param plugins
26
- * @param options
27
33
  */
28
34
  private execResponsePlugin;
29
35
  /**
@@ -34,39 +40,31 @@ declare class RequestService {
34
40
  * 开始请求
35
41
  * @param adapter
36
42
  * @param options
37
- * @returns
43
+ * @returns Promise
38
44
  */
39
45
  private startRequest;
40
46
  /**
41
47
  * 执行拦截器
42
48
  * @param response 请求响应对象
43
- * @returns
49
+ * @returns Promise
44
50
  */
45
51
  private execInterceptors;
46
52
  /**
47
53
  * 发送请求
48
54
  * @param {RequestSendOptions} options 请求选项
49
- * @param {RequestPlugin[]} plugins 请求插件
50
- * @returns
55
+ * @param {RequestPlugin[]} requestPlugins 请求插件
56
+ * @returns Promise
51
57
  */
52
58
  send(options: RequestSendOptions, requestPlugins?: (RequestPlugin | undefined)[]): Promise<any>;
53
59
  /**
54
60
  * 生成请求路径
55
61
  * @param {RequestSendOptions} options 请求选项
56
62
  * @param {RequestPlugin[]} plugins 请求插件
57
- * @returns
63
+ * @returns string
58
64
  */
59
- toURL(options: RequestSendOptions, plugins?: RequestPlugin[]): string;
65
+ toURL(options: RequestSendOptions, plugins?: (RequestPlugin | undefined)[]): string;
60
66
  }
61
67
 
62
68
  declare function setup(config: RequestSetupConfig): void;
63
69
 
64
- declare enum RequestGenerateType {
65
- Request = "TO_REQUEST",
66
- URL = "TO_URL"
67
- }
68
- interface RequestGenerateOptions {
69
- type?: RequestGenerateType;
70
- }
71
-
72
70
  export { RequestGenerateOptions, RequestGenerateType, RequestPlugin, RequestSendOptions, RequestService, RequestSetupConfig, setup };
package/dist/index.js CHANGED
@@ -2,8 +2,12 @@ import {
2
2
  __publicField
3
3
  } from "./chunk-XXPGZHWZ.js";
4
4
 
5
- // src/request-service.ts
6
- import qs from "qs";
5
+ // src/interfaces/request-generate.interface.ts
6
+ var RequestGenerateType = /* @__PURE__ */ ((RequestGenerateType2) => {
7
+ RequestGenerateType2["Request"] = "TO_REQUEST";
8
+ RequestGenerateType2["URL"] = "TO_URL";
9
+ return RequestGenerateType2;
10
+ })(RequestGenerateType || {});
7
11
 
8
12
  // src/interfaces/request-plugin.interface.ts
9
13
  var PluginLifecycle = /* @__PURE__ */ ((PluginLifecycle2) => {
@@ -13,6 +17,107 @@ var PluginLifecycle = /* @__PURE__ */ ((PluginLifecycle2) => {
13
17
  return PluginLifecycle2;
14
18
  })(PluginLifecycle || {});
15
19
 
20
+ // src/interfaces/request-send.interface.ts
21
+ var RequestMethod = /* @__PURE__ */ ((RequestMethod2) => {
22
+ RequestMethod2["Get"] = "GET";
23
+ RequestMethod2["Post"] = "POST";
24
+ RequestMethod2["Put"] = "PUT";
25
+ RequestMethod2["Delete"] = "DELETE";
26
+ RequestMethod2["Options"] = "OPTIONS";
27
+ RequestMethod2["Head"] = "HEAD";
28
+ RequestMethod2["Patch"] = "PATCH";
29
+ return RequestMethod2;
30
+ })(RequestMethod || {});
31
+
32
+ // src/utils/query-string.ts
33
+ function encodeValue(value, encode) {
34
+ if (!encode)
35
+ return value;
36
+ return encodeURIComponent(value);
37
+ }
38
+ function getKeyWithDot(key, childKey, allowDots) {
39
+ return allowDots ? `${key}.${childKey}` : `${key}[${childKey}]`;
40
+ }
41
+ function processValue(value, options, parentKey = "") {
42
+ const result = [];
43
+ if (value === null || value === void 0) {
44
+ if (!options.skipNulls) {
45
+ result.push(`${parentKey}=`);
46
+ }
47
+ return result;
48
+ }
49
+ if (Array.isArray(value)) {
50
+ if (options.arrayFormat === "comma") {
51
+ const filteredValues = value.filter((item) => item !== null && item !== void 0);
52
+ if (filteredValues.length > 0) {
53
+ const commaValue = filteredValues.map(
54
+ (item) => typeof item === "object" ? JSON.stringify(item) : item.toString()
55
+ ).join(",");
56
+ const shouldEncode = options.encode !== false;
57
+ const encodedValue = encodeValue(commaValue, shouldEncode);
58
+ result.push(`${parentKey}=${encodedValue}`);
59
+ }
60
+ } else {
61
+ value.forEach((item, index) => {
62
+ let itemKey;
63
+ const shouldEncode = options.encode !== false;
64
+ if (options.arrayFormat === "indices") {
65
+ itemKey = parentKey ? `${parentKey}[${index}]` : `[${index}]`;
66
+ } else if (options.arrayFormat === "brackets") {
67
+ itemKey = parentKey ? `${parentKey}[]` : "[]";
68
+ } else {
69
+ itemKey = parentKey;
70
+ }
71
+ if (typeof item === "object" && item !== null) {
72
+ result.push(...processValue(item, options, itemKey));
73
+ } else {
74
+ const encodedValue = encodeValue(item.toString(), shouldEncode);
75
+ result.push(`${itemKey}=${encodedValue}`);
76
+ }
77
+ });
78
+ }
79
+ } else if (typeof value === "object") {
80
+ Object.keys(value).forEach((childKey) => {
81
+ const fullKey = parentKey ? getKeyWithDot(parentKey, childKey, options.allowDots || false) : childKey;
82
+ result.push(...processValue(value[childKey], options, fullKey));
83
+ });
84
+ } else {
85
+ const shouldEncode = options.encode !== false;
86
+ const encodedValue = encodeValue(value.toString(), shouldEncode);
87
+ const encodedKey = options.encodeValuesOnly ? parentKey : encodeValue(parentKey, shouldEncode);
88
+ result.push(`${encodedKey}=${encodedValue}`);
89
+ }
90
+ return result;
91
+ }
92
+ function stringify(obj, options = {}) {
93
+ const {
94
+ arrayFormat = "indices",
95
+ skipNulls = true,
96
+ allowDots = true,
97
+ encodeValuesOnly = false,
98
+ encode = true,
99
+ addQueryPrefix = false
100
+ } = options;
101
+ if (!obj || typeof obj !== "object") {
102
+ return options.addQueryPrefix ? "?" : "";
103
+ }
104
+ const parts = [];
105
+ Object.keys(obj).forEach((key) => {
106
+ const value = obj[key];
107
+ const processedValues = processValue(value, {
108
+ arrayFormat,
109
+ skipNulls,
110
+ allowDots,
111
+ encodeValuesOnly,
112
+ encode,
113
+ addQueryPrefix
114
+ }, key);
115
+ parts.push(...processedValues);
116
+ });
117
+ const queryString = parts.join("&");
118
+ return addQueryPrefix ? queryString ? `?${queryString}` : "?" : queryString;
119
+ }
120
+
16
121
  // src/request-service.ts
17
122
  var _RequestService = class {
18
123
  /**
@@ -26,7 +131,7 @@ var _RequestService = class {
26
131
  }
27
132
  /**
28
133
  * 获取RequestAdatper
29
- * @returns
134
+ * @returns RequestAdapter
30
135
  */
31
136
  getRequestAdapter() {
32
137
  if (!_RequestService.config.adapter) {
@@ -58,17 +163,19 @@ var _RequestService = class {
58
163
  }
59
164
  /**
60
165
  * 执行前置插件逻辑
61
- * @param plugins
62
- * @param options
63
166
  */
64
167
  execResponsePlugin(leftcycle, plugins = [], options, response) {
65
168
  plugins.forEach((plugin) => {
66
169
  const leftcycleFn = plugin[leftcycle];
67
- leftcycleFn && leftcycleFn.bind(plugin)(response, options);
170
+ if (leftcycleFn) {
171
+ leftcycleFn.bind(plugin)(response, options);
172
+ }
68
173
  });
69
174
  _RequestService.config.plugins.forEach((plugin) => {
70
175
  const leftcycleFn = plugin[leftcycle];
71
- leftcycleFn && leftcycleFn.bind(plugin)(response, options);
176
+ if (leftcycleFn) {
177
+ leftcycleFn.bind(plugin)(response, options);
178
+ }
72
179
  });
73
180
  }
74
181
  /**
@@ -91,7 +198,7 @@ var _RequestService = class {
91
198
  * 开始请求
92
199
  * @param adapter
93
200
  * @param options
94
- * @returns
201
+ * @returns Promise
95
202
  */
96
203
  startRequest(adapter, options, extraParams) {
97
204
  return adapter.request({
@@ -111,7 +218,7 @@ var _RequestService = class {
111
218
  /**
112
219
  * 执行拦截器
113
220
  * @param response 请求响应对象
114
- * @returns
221
+ * @returns Promise
115
222
  */
116
223
  execInterceptors(response, hasException = false) {
117
224
  const interceptors = _RequestService.config?.interceptors;
@@ -131,8 +238,8 @@ var _RequestService = class {
131
238
  /**
132
239
  * 发送请求
133
240
  * @param {RequestSendOptions} options 请求选项
134
- * @param {RequestPlugin[]} plugins 请求插件
135
- * @returns
241
+ * @param {RequestPlugin[]} requestPlugins 请求插件
242
+ * @returns Promise
136
243
  */
137
244
  async send(options, requestPlugins = []) {
138
245
  if (!_RequestService.config) {
@@ -157,14 +264,17 @@ var _RequestService = class {
157
264
  * 生成请求路径
158
265
  * @param {RequestSendOptions} options 请求选项
159
266
  * @param {RequestPlugin[]} plugins 请求插件
160
- * @returns
267
+ * @returns string
161
268
  */
162
269
  toURL(options, plugins = []) {
163
270
  if (!_RequestService.config) {
164
271
  throw new Error("\u8BF7\u68C0\u67E5\u8BF7\u6C42\u914D\u7F6E\u662F\u5426\u5B8C\u6210");
165
272
  }
166
273
  if (plugins && plugins.length) {
167
- this.execRequestPlugin(plugins, options);
274
+ this.execRequestPlugin(
275
+ plugins.filter(Boolean),
276
+ options
277
+ );
168
278
  }
169
279
  const baseURL = _RequestService.config.gateway;
170
280
  const pathURL = this.parseRequestPath(
@@ -172,16 +282,14 @@ var _RequestService = class {
172
282
  options.paramsPath,
173
283
  options.service
174
284
  );
175
- const queryString = qs.stringify(options.paramsQuery, {
176
- ...{
177
- arrayFormat: "repeat",
178
- skipNulls: true,
179
- allowDots: true,
180
- encodeValuesOnly: true,
181
- encode: true
182
- },
183
- ..._RequestService.config.qs || {},
184
- addQueryPrefix: true
285
+ const queryString = stringify(options.paramsQuery || {}, {
286
+ arrayFormat: "repeat",
287
+ skipNulls: true,
288
+ allowDots: true,
289
+ encodeValuesOnly: true,
290
+ encode: true,
291
+ addQueryPrefix: true,
292
+ ..._RequestService.config.qs || {}
185
293
  });
186
294
  return `${baseURL}${pathURL}${queryString}`;
187
295
  }
@@ -195,25 +303,6 @@ function setup(config) {
195
303
  RequestService.config = config;
196
304
  RequestService.config?.adapter?.injectConfig?.(config);
197
305
  }
198
-
199
- // src/interfaces/request-send.interface.ts
200
- var RequestMethod = /* @__PURE__ */ ((RequestMethod2) => {
201
- RequestMethod2["Get"] = "GET";
202
- RequestMethod2["Post"] = "POST";
203
- RequestMethod2["Put"] = "PUT";
204
- RequestMethod2["Delete"] = "DELETE";
205
- RequestMethod2["Options"] = "OPTIONS";
206
- RequestMethod2["Head"] = "HEAD";
207
- RequestMethod2["Patch"] = "PATCH";
208
- return RequestMethod2;
209
- })(RequestMethod || {});
210
-
211
- // src/interfaces/request-generate.interface.ts
212
- var RequestGenerateType = /* @__PURE__ */ ((RequestGenerateType2) => {
213
- RequestGenerateType2["Request"] = "TO_REQUEST";
214
- RequestGenerateType2["URL"] = "TO_URL";
215
- return RequestGenerateType2;
216
- })(RequestGenerateType || {});
217
306
  export {
218
307
  PluginLifecycle,
219
308
  RequestGenerateType,
@@ -22,8 +22,24 @@ interface RequestSendOptions {
22
22
  paramsBody?: any;
23
23
  }
24
24
 
25
+ /**
26
+ * 请求插件
27
+ */
28
+ interface RequestPlugin {
29
+ before?: (options: RequestSendOptions, appendParams: (params: Record<string, any>) => void) => void | Promise<void>;
30
+ after?: (response: AdapterResponse, options: RequestSendOptions) => void;
31
+ catch?: (response: AdapterResponse, options: RequestSendOptions) => void;
32
+ }
33
+ declare enum PluginLifecycle {
34
+ before = "before",
35
+ after = "after",
36
+ catch = "catch"
37
+ }
38
+ type RequestLifecycle = PluginLifecycle.before;
39
+ type ResponseLifecycle = PluginLifecycle.after | PluginLifecycle.catch;
40
+
25
41
  interface ResponseInterceptor {
26
- exec(response: AdapterResponse): any;
42
+ exec: (response: AdapterResponse) => any;
27
43
  }
28
44
 
29
45
  interface RequestSetupConfig {
@@ -50,17 +66,17 @@ interface RequestAdapter {
50
66
  /**
51
67
  * 发送请求
52
68
  */
53
- request(options: RequestAdapterOptions): Promise<any>;
69
+ request: (options: RequestAdapterOptions) => Promise<any>;
54
70
  /**
55
71
  * 转换Response
56
72
  * @param any
57
73
  */
58
- transformResponse(response: any): AdapterResponse;
74
+ transformResponse: (response: any) => AdapterResponse;
59
75
  /**
60
76
  * 转换Exception
61
77
  * @param any
62
78
  */
63
- transformException(response: any): AdapterResponse;
79
+ transformException: (response: any) => AdapterResponse;
64
80
  }
65
81
  interface RequestAdapterOptions {
66
82
  baseURL: string;
@@ -79,20 +95,4 @@ interface AdapterResponse {
79
95
  }
80
96
  type AdapterResponseHeaders = Record<string, string | string[] | number | boolean | undefined>;
81
97
 
82
- /**
83
- * 请求插件
84
- */
85
- interface RequestPlugin {
86
- before?: (options: RequestSendOptions, appendParams: (params: Record<string, any>) => void) => void | Promise<void>;
87
- after?: (response: AdapterResponse, options: RequestSendOptions) => void;
88
- catch?: (response: AdapterResponse, options: RequestSendOptions) => void;
89
- }
90
- declare enum PluginLifecycle {
91
- before = "before",
92
- after = "after",
93
- catch = "catch"
94
- }
95
- type RequestLifecycle = PluginLifecycle.before;
96
- type ResponseLifecycle = PluginLifecycle.after | PluginLifecycle.catch;
97
-
98
98
  export { AdapterResponse as A, PluginLifecycle as P, RequestSetupConfig as R, RequestSendOptions as a, RequestPlugin as b, RequestAdapter as c, RequestAdapterOptions as d, AdapterResponseHeaders as e, RequestLifecycle as f, ResponseLifecycle as g, RequestMethod as h, ResponseInterceptor as i };
package/package.json CHANGED
@@ -1,32 +1,36 @@
1
1
  {
2
2
  "name": "@gopowerteam/request",
3
- "private": false,
4
- "version": "0.1.19",
5
3
  "type": "module",
6
- "files": [
7
- "dist",
8
- "adapters.d.ts",
9
- "README.md"
10
- ],
4
+ "version": "0.1.21",
5
+ "private": false,
11
6
  "keywords": [
12
7
  "gopowerteam",
13
8
  "request"
14
9
  ],
15
- "main": "./dist/index.cjs",
16
- "module": "./dist/index.js",
17
- "types": "./dist/index.d.ts",
18
10
  "exports": {
19
11
  ".": {
20
- "require": "./dist/index.cjs",
12
+ "types": "./dist/index.d.ts",
21
13
  "import": "./dist/index.js",
22
- "types": "./dist/index.d.ts"
14
+ "require": "./dist/index.cjs"
23
15
  },
24
16
  "./adapters": {
25
- "require": "./dist/adapters/index.cjs",
17
+ "types": "./dist/adapters/index.d.ts",
26
18
  "import": "./dist/adapters/index.js",
27
- "types": "./dist/adapters/index.d.ts"
19
+ "require": "./dist/adapters/index.cjs"
28
20
  }
29
21
  },
22
+ "main": "./dist/index.cjs",
23
+ "module": "./dist/index.js",
24
+ "types": "./dist/index.d.ts",
25
+ "files": [
26
+ "README.md",
27
+ "adapters.d.ts",
28
+ "dist"
29
+ ],
30
+ "dependencies": {
31
+ "axios": "^1.3.4",
32
+ "qs": "^6.11.0"
33
+ },
30
34
  "devDependencies": {
31
35
  "@types/jest": "^29.4.0",
32
36
  "@types/node": "18",
@@ -36,12 +40,14 @@
36
40
  "tsup": "^6.6.3",
37
41
  "typescript": "^4.9.5"
38
42
  },
39
- "dependencies": {
40
- "axios": "^1.3.4",
41
- "qs": "^6.11.0"
43
+ "publishConfig": {
44
+ "registry": "https://registry.npmjs.org",
45
+ "access": "public"
42
46
  },
43
47
  "scripts": {
48
+ "dev": "tsup --watch",
44
49
  "test": "jest --coverage",
45
- "build": "tsup"
50
+ "build": "tsup",
51
+ "lint": "eslint . --fix"
46
52
  }
47
53
  }