@gopowerteam/request 0.1.20 → 0.2.0
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.
- package/dist/adapters/index.cjs +96 -124
- package/dist/adapters/index.d.cts +39 -0
- package/dist/adapters/index.d.mts +39 -0
- package/dist/adapters/index.mjs +77 -0
- package/dist/index-hNav9Ikc.d.mts +112 -0
- package/dist/index.cjs +241 -259
- package/dist/index.d.cts +71 -0
- package/dist/index.d.mts +62 -0
- package/dist/index.mjs +244 -0
- package/dist/request-adapter.interface-Cr5s4kIp.d.cts +103 -0
- package/package.json +34 -28
- package/dist/adapters/index.d.ts +0 -33
- package/dist/adapters/index.js +0 -97
- package/dist/chunk-XXPGZHWZ.js +0 -10
- package/dist/index.d.ts +0 -72
- package/dist/index.js +0 -226
- package/dist/request-plugin.interface-3df174bc.d.ts +0 -98
package/dist/index.js
DELETED
|
@@ -1,226 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
__publicField
|
|
3
|
-
} from "./chunk-XXPGZHWZ.js";
|
|
4
|
-
|
|
5
|
-
// src/request-service.ts
|
|
6
|
-
import qs from "qs";
|
|
7
|
-
|
|
8
|
-
// src/interfaces/request-plugin.interface.ts
|
|
9
|
-
var PluginLifecycle = /* @__PURE__ */ ((PluginLifecycle2) => {
|
|
10
|
-
PluginLifecycle2["before"] = "before";
|
|
11
|
-
PluginLifecycle2["after"] = "after";
|
|
12
|
-
PluginLifecycle2["catch"] = "catch";
|
|
13
|
-
return PluginLifecycle2;
|
|
14
|
-
})(PluginLifecycle || {});
|
|
15
|
-
|
|
16
|
-
// src/request-service.ts
|
|
17
|
-
var _RequestService = class {
|
|
18
|
-
/**
|
|
19
|
-
* 获取服务请求单例
|
|
20
|
-
*/
|
|
21
|
-
static getInstance() {
|
|
22
|
-
if (this.instance) {
|
|
23
|
-
return this.instance;
|
|
24
|
-
}
|
|
25
|
-
return new _RequestService();
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* 获取RequestAdatper
|
|
29
|
-
* @returns
|
|
30
|
-
*/
|
|
31
|
-
getRequestAdapter() {
|
|
32
|
-
if (!_RequestService.config.adapter) {
|
|
33
|
-
throw new Error("\u8BF7\u68C0\u67E5\u662F\u5426\u914D\u7F6E\u8BF7\u6C42Adatper");
|
|
34
|
-
}
|
|
35
|
-
return _RequestService.config.adapter;
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* 执行前置插件逻辑
|
|
39
|
-
* @param plugins
|
|
40
|
-
* @param options
|
|
41
|
-
*/
|
|
42
|
-
async execRequestPlugin(plugins = [], options) {
|
|
43
|
-
const extraParams = {};
|
|
44
|
-
const appendParams = (params) => {
|
|
45
|
-
Object.assign(extraParams, params || {});
|
|
46
|
-
};
|
|
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
|
-
}
|
|
57
|
-
return extraParams;
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* 执行前置插件逻辑
|
|
61
|
-
* @param plugins
|
|
62
|
-
* @param options
|
|
63
|
-
*/
|
|
64
|
-
execResponsePlugin(leftcycle, plugins = [], options, response) {
|
|
65
|
-
plugins.forEach((plugin) => {
|
|
66
|
-
const leftcycleFn = plugin[leftcycle];
|
|
67
|
-
leftcycleFn && leftcycleFn.bind(plugin)(response, options);
|
|
68
|
-
});
|
|
69
|
-
_RequestService.config.plugins.forEach((plugin) => {
|
|
70
|
-
const leftcycleFn = plugin[leftcycle];
|
|
71
|
-
leftcycleFn && leftcycleFn.bind(plugin)(response, options);
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* 转换请求路径
|
|
76
|
-
*/
|
|
77
|
-
parseRequestPath(path, paramsPath, service) {
|
|
78
|
-
if (service) {
|
|
79
|
-
path = `/${service}/${path}`.replace(/\/{2,3}/g, "/");
|
|
80
|
-
}
|
|
81
|
-
if (paramsPath) {
|
|
82
|
-
return Object.entries(paramsPath).reduce(
|
|
83
|
-
(r, [key, value]) => r.replace(`{${key}}`, value.toString()),
|
|
84
|
-
path
|
|
85
|
-
);
|
|
86
|
-
} else {
|
|
87
|
-
return path;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* 开始请求
|
|
92
|
-
* @param adapter
|
|
93
|
-
* @param options
|
|
94
|
-
* @returns
|
|
95
|
-
*/
|
|
96
|
-
startRequest(adapter, options, extraParams) {
|
|
97
|
-
return adapter.request({
|
|
98
|
-
baseURL: _RequestService.config.gateway,
|
|
99
|
-
pathURL: this.parseRequestPath(
|
|
100
|
-
options.path,
|
|
101
|
-
options.paramsPath,
|
|
102
|
-
options.service
|
|
103
|
-
),
|
|
104
|
-
method: options.method,
|
|
105
|
-
headers: options.headers || {},
|
|
106
|
-
paramsQuery: options.paramsQuery,
|
|
107
|
-
paramsBody: options.paramsBody,
|
|
108
|
-
extraParams
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* 执行拦截器
|
|
113
|
-
* @param response 请求响应对象
|
|
114
|
-
* @returns
|
|
115
|
-
*/
|
|
116
|
-
execInterceptors(response, hasException = false) {
|
|
117
|
-
const interceptors = _RequestService.config?.interceptors;
|
|
118
|
-
if (!interceptors?.status || !interceptors?.error || !interceptors?.success || !interceptors?.exception) {
|
|
119
|
-
throw new Error("\u8BF7\u68C0\u67E5\u62E6\u622A\u5668\u914D\u7F6E");
|
|
120
|
-
}
|
|
121
|
-
const status = interceptors.status.exec(response) && !hasException;
|
|
122
|
-
if (hasException) {
|
|
123
|
-
interceptors.exception.exec(response);
|
|
124
|
-
}
|
|
125
|
-
if (status) {
|
|
126
|
-
return Promise.resolve(interceptors.success.exec(response));
|
|
127
|
-
} else {
|
|
128
|
-
return Promise.reject(interceptors.error.exec(response));
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
/**
|
|
132
|
-
* 发送请求
|
|
133
|
-
* @param {RequestSendOptions} options 请求选项
|
|
134
|
-
* @param {RequestPlugin[]} plugins 请求插件
|
|
135
|
-
* @returns
|
|
136
|
-
*/
|
|
137
|
-
async send(options, requestPlugins = []) {
|
|
138
|
-
if (!_RequestService.config) {
|
|
139
|
-
throw new Error("\u8BF7\u68C0\u67E5\u8BF7\u6C42\u914D\u7F6E\u662F\u5426\u5B8C\u6210");
|
|
140
|
-
}
|
|
141
|
-
let hasException = false;
|
|
142
|
-
const adapter = this.getRequestAdapter();
|
|
143
|
-
const plugins = requestPlugins.filter(Boolean);
|
|
144
|
-
const extraParams = await this.execRequestPlugin(plugins, options);
|
|
145
|
-
const response = await this.startRequest(adapter, options, extraParams).then((response2) => adapter.transformResponse(response2)).catch((exception) => {
|
|
146
|
-
hasException = true;
|
|
147
|
-
return adapter.transformException(exception);
|
|
148
|
-
});
|
|
149
|
-
if (!hasException) {
|
|
150
|
-
this.execResponsePlugin("after" /* after */, plugins, options, response);
|
|
151
|
-
} else {
|
|
152
|
-
this.execResponsePlugin("catch" /* catch */, plugins, options, response);
|
|
153
|
-
}
|
|
154
|
-
return this.execInterceptors(response, hasException);
|
|
155
|
-
}
|
|
156
|
-
/**
|
|
157
|
-
* 生成请求路径
|
|
158
|
-
* @param {RequestSendOptions} options 请求选项
|
|
159
|
-
* @param {RequestPlugin[]} plugins 请求插件
|
|
160
|
-
* @returns
|
|
161
|
-
*/
|
|
162
|
-
toURL(options, plugins = []) {
|
|
163
|
-
if (!_RequestService.config) {
|
|
164
|
-
throw new Error("\u8BF7\u68C0\u67E5\u8BF7\u6C42\u914D\u7F6E\u662F\u5426\u5B8C\u6210");
|
|
165
|
-
}
|
|
166
|
-
if (plugins && plugins.length) {
|
|
167
|
-
this.execRequestPlugin(
|
|
168
|
-
plugins.filter(Boolean),
|
|
169
|
-
options
|
|
170
|
-
);
|
|
171
|
-
}
|
|
172
|
-
const baseURL = _RequestService.config.gateway;
|
|
173
|
-
const pathURL = this.parseRequestPath(
|
|
174
|
-
options.path,
|
|
175
|
-
options.paramsPath,
|
|
176
|
-
options.service
|
|
177
|
-
);
|
|
178
|
-
const queryString = qs.stringify(options.paramsQuery, {
|
|
179
|
-
...{
|
|
180
|
-
arrayFormat: "repeat",
|
|
181
|
-
skipNulls: true,
|
|
182
|
-
allowDots: true,
|
|
183
|
-
encodeValuesOnly: true,
|
|
184
|
-
encode: true
|
|
185
|
-
},
|
|
186
|
-
..._RequestService.config.qs || {},
|
|
187
|
-
addQueryPrefix: true
|
|
188
|
-
});
|
|
189
|
-
return `${baseURL}${pathURL}${queryString}`;
|
|
190
|
-
}
|
|
191
|
-
};
|
|
192
|
-
var RequestService = _RequestService;
|
|
193
|
-
__publicField(RequestService, "config");
|
|
194
|
-
__publicField(RequestService, "instance");
|
|
195
|
-
|
|
196
|
-
// src/request-setup.ts
|
|
197
|
-
function setup(config) {
|
|
198
|
-
RequestService.config = config;
|
|
199
|
-
RequestService.config?.adapter?.injectConfig?.(config);
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
// src/interfaces/request-send.interface.ts
|
|
203
|
-
var RequestMethod = /* @__PURE__ */ ((RequestMethod2) => {
|
|
204
|
-
RequestMethod2["Get"] = "GET";
|
|
205
|
-
RequestMethod2["Post"] = "POST";
|
|
206
|
-
RequestMethod2["Put"] = "PUT";
|
|
207
|
-
RequestMethod2["Delete"] = "DELETE";
|
|
208
|
-
RequestMethod2["Options"] = "OPTIONS";
|
|
209
|
-
RequestMethod2["Head"] = "HEAD";
|
|
210
|
-
RequestMethod2["Patch"] = "PATCH";
|
|
211
|
-
return RequestMethod2;
|
|
212
|
-
})(RequestMethod || {});
|
|
213
|
-
|
|
214
|
-
// src/interfaces/request-generate.interface.ts
|
|
215
|
-
var RequestGenerateType = /* @__PURE__ */ ((RequestGenerateType2) => {
|
|
216
|
-
RequestGenerateType2["Request"] = "TO_REQUEST";
|
|
217
|
-
RequestGenerateType2["URL"] = "TO_URL";
|
|
218
|
-
return RequestGenerateType2;
|
|
219
|
-
})(RequestGenerateType || {});
|
|
220
|
-
export {
|
|
221
|
-
PluginLifecycle,
|
|
222
|
-
RequestGenerateType,
|
|
223
|
-
RequestMethod,
|
|
224
|
-
RequestService,
|
|
225
|
-
setup
|
|
226
|
-
};
|
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
import { IStringifyOptions } from 'qs';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* 请求方法类型
|
|
5
|
-
*/
|
|
6
|
-
declare enum RequestMethod {
|
|
7
|
-
Get = "GET",
|
|
8
|
-
Post = "POST",
|
|
9
|
-
Put = "PUT",
|
|
10
|
-
Delete = "DELETE",
|
|
11
|
-
Options = "OPTIONS",
|
|
12
|
-
Head = "HEAD",
|
|
13
|
-
Patch = "PATCH"
|
|
14
|
-
}
|
|
15
|
-
interface RequestSendOptions {
|
|
16
|
-
service?: string;
|
|
17
|
-
path: string;
|
|
18
|
-
method: RequestMethod | string;
|
|
19
|
-
headers?: Record<string, string>;
|
|
20
|
-
paramsPath?: Record<string, string | number>;
|
|
21
|
-
paramsQuery?: Record<string, any>;
|
|
22
|
-
paramsBody?: any;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
interface ResponseInterceptor {
|
|
26
|
-
exec(response: AdapterResponse): any;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
interface RequestSetupConfig {
|
|
30
|
-
gateway: string;
|
|
31
|
-
timeout?: number;
|
|
32
|
-
qs?: IStringifyOptions;
|
|
33
|
-
adapter?: RequestAdapter;
|
|
34
|
-
interceptors: {
|
|
35
|
-
status: ResponseInterceptor;
|
|
36
|
-
success: ResponseInterceptor;
|
|
37
|
-
error: ResponseInterceptor;
|
|
38
|
-
exception: ResponseInterceptor;
|
|
39
|
-
};
|
|
40
|
-
plugins: RequestPlugin[];
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
interface RequestAdapter {
|
|
44
|
-
/**
|
|
45
|
-
* 注入全局配置文件
|
|
46
|
-
* @param config
|
|
47
|
-
* @returns
|
|
48
|
-
*/
|
|
49
|
-
injectConfig?: (config: RequestSetupConfig) => void;
|
|
50
|
-
/**
|
|
51
|
-
* 发送请求
|
|
52
|
-
*/
|
|
53
|
-
request(options: RequestAdapterOptions): Promise<any>;
|
|
54
|
-
/**
|
|
55
|
-
* 转换Response
|
|
56
|
-
* @param any
|
|
57
|
-
*/
|
|
58
|
-
transformResponse(response: any): AdapterResponse;
|
|
59
|
-
/**
|
|
60
|
-
* 转换Exception
|
|
61
|
-
* @param any
|
|
62
|
-
*/
|
|
63
|
-
transformException(response: any): AdapterResponse;
|
|
64
|
-
}
|
|
65
|
-
interface RequestAdapterOptions {
|
|
66
|
-
baseURL: string;
|
|
67
|
-
pathURL: string;
|
|
68
|
-
headers: Record<string, string>;
|
|
69
|
-
method: RequestMethod | string;
|
|
70
|
-
paramsQuery?: Record<string, any>;
|
|
71
|
-
paramsBody?: any;
|
|
72
|
-
extraParams?: any;
|
|
73
|
-
}
|
|
74
|
-
interface AdapterResponse {
|
|
75
|
-
data: Record<string, any>;
|
|
76
|
-
status: number;
|
|
77
|
-
statusText: string;
|
|
78
|
-
headers: AdapterResponseHeaders;
|
|
79
|
-
}
|
|
80
|
-
type AdapterResponseHeaders = Record<string, string | string[] | number | boolean | undefined>;
|
|
81
|
-
|
|
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
|
-
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 };
|