@gopowerteam/request 0.1.16 → 0.1.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.
@@ -1,4 +1,4 @@
1
- import { c as RequestAdapter, R as RequestSetupConfig, d as RequestAdapterOptions, A as AdapterResponse } from '../request-plugin.interface-8152139f.js';
1
+ import { c as RequestAdapter, R as RequestSetupConfig, d as RequestAdapterOptions, A as AdapterResponse } from '../request-plugin.interface-3df174bc.js';
2
2
  import { AxiosResponse, AxiosError } from 'axios';
3
3
  import 'qs';
4
4
 
package/dist/index.cjs CHANGED
@@ -80,17 +80,21 @@ var _RequestService = class {
80
80
  * @param plugins
81
81
  * @param options
82
82
  */
83
- execRequestPlugin(plugins = [], options) {
83
+ async execRequestPlugin(plugins = [], options) {
84
84
  const extraParams = {};
85
85
  const appendParams = (params) => {
86
86
  Object.assign(extraParams, params || {});
87
87
  };
88
- _RequestService.config.plugins.forEach(
89
- (service) => service.before && service.before(options, appendParams)
90
- );
91
- plugins.forEach(
92
- (service) => service.before && service.before(options, appendParams)
93
- );
88
+ for (const plugin of _RequestService.config.plugins) {
89
+ if (plugin.before) {
90
+ await plugin.before(options, appendParams);
91
+ }
92
+ }
93
+ for (const plugin of plugins) {
94
+ if (plugin.before) {
95
+ await plugin.before(options, appendParams);
96
+ }
97
+ }
94
98
  return extraParams;
95
99
  }
96
100
  /**
@@ -171,13 +175,14 @@ var _RequestService = class {
171
175
  * @param {RequestPlugin[]} plugins 请求插件
172
176
  * @returns
173
177
  */
174
- async send(options, plugins = []) {
178
+ async send(options, requestPlugins = []) {
175
179
  if (!_RequestService.config) {
176
180
  throw new Error("\u8BF7\u68C0\u67E5\u8BF7\u6C42\u914D\u7F6E\u662F\u5426\u5B8C\u6210");
177
181
  }
178
182
  let hasException = false;
179
183
  const adapter = this.getRequestAdapter();
180
- const extraParams = this.execRequestPlugin(plugins, options);
184
+ const plugins = requestPlugins.filter(Boolean);
185
+ const extraParams = await this.execRequestPlugin(plugins, options);
181
186
  const response = await this.startRequest(adapter, options, extraParams).then((response2) => adapter.transformResponse(response2)).catch((exception) => {
182
187
  hasException = true;
183
188
  return adapter.transformException(exception);
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { R as RequestSetupConfig, a as RequestSendOptions, b as RequestPlugin } from './request-plugin.interface-8152139f.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-8152139f.js';
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';
3
3
  import 'qs';
4
4
 
5
5
  declare class RequestService {
@@ -49,7 +49,7 @@ declare class RequestService {
49
49
  * @param {RequestPlugin[]} plugins 请求插件
50
50
  * @returns
51
51
  */
52
- send(options: RequestSendOptions, plugins?: RequestPlugin[]): Promise<any>;
52
+ send(options: RequestSendOptions, requestPlugins?: (RequestPlugin | undefined)[]): Promise<any>;
53
53
  /**
54
54
  * 生成请求路径
55
55
  * @param {RequestSendOptions} options 请求选项
package/dist/index.js CHANGED
@@ -39,17 +39,21 @@ var _RequestService = class {
39
39
  * @param plugins
40
40
  * @param options
41
41
  */
42
- execRequestPlugin(plugins = [], options) {
42
+ async execRequestPlugin(plugins = [], options) {
43
43
  const extraParams = {};
44
44
  const appendParams = (params) => {
45
45
  Object.assign(extraParams, params || {});
46
46
  };
47
- _RequestService.config.plugins.forEach(
48
- (service) => service.before && service.before(options, appendParams)
49
- );
50
- plugins.forEach(
51
- (service) => service.before && service.before(options, appendParams)
52
- );
47
+ for (const plugin of _RequestService.config.plugins) {
48
+ if (plugin.before) {
49
+ await plugin.before(options, appendParams);
50
+ }
51
+ }
52
+ for (const plugin of plugins) {
53
+ if (plugin.before) {
54
+ await plugin.before(options, appendParams);
55
+ }
56
+ }
53
57
  return extraParams;
54
58
  }
55
59
  /**
@@ -130,13 +134,14 @@ var _RequestService = class {
130
134
  * @param {RequestPlugin[]} plugins 请求插件
131
135
  * @returns
132
136
  */
133
- async send(options, plugins = []) {
137
+ async send(options, requestPlugins = []) {
134
138
  if (!_RequestService.config) {
135
139
  throw new Error("\u8BF7\u68C0\u67E5\u8BF7\u6C42\u914D\u7F6E\u662F\u5426\u5B8C\u6210");
136
140
  }
137
141
  let hasException = false;
138
142
  const adapter = this.getRequestAdapter();
139
- const extraParams = this.execRequestPlugin(plugins, options);
143
+ const plugins = requestPlugins.filter(Boolean);
144
+ const extraParams = await this.execRequestPlugin(plugins, options);
140
145
  const response = await this.startRequest(adapter, options, extraParams).then((response2) => adapter.transformResponse(response2)).catch((exception) => {
141
146
  hasException = true;
142
147
  return adapter.transformException(exception);
@@ -83,7 +83,7 @@ type AdapterResponseHeaders = Record<string, string | string[] | number | boolea
83
83
  * 请求插件
84
84
  */
85
85
  interface RequestPlugin {
86
- before?: (options: RequestSendOptions, appendParams: (params: Record<string, any>) => void) => void;
86
+ before?: (options: RequestSendOptions, appendParams: (params: Record<string, any>) => void) => void | Promise<void>;
87
87
  after?: (response: AdapterResponse, options: RequestSendOptions) => void;
88
88
  catch?: (response: AdapterResponse, options: RequestSendOptions) => void;
89
89
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@gopowerteam/request",
3
3
  "private": false,
4
- "version": "0.1.16",
4
+ "version": "0.1.18",
5
5
  "type": "module",
6
6
  "files": [
7
7
  "dist",