@gopowerteam/request 0.1.21 → 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 -122
- 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 +233 -327
- 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 +19 -19
- package/dist/adapters/index.d.ts +0 -31
- package/dist/adapters/index.js +0 -95
- package/dist/chunk-XXPGZHWZ.js +0 -10
- package/dist/index.d.ts +0 -70
- package/dist/index.js +0 -312
- package/dist/request-adapter.interface-fef4fe40.d.ts +0 -98
package/dist/index.cjs
CHANGED
|
@@ -1,344 +1,250 @@
|
|
|
1
|
-
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
7
|
-
var __export = (target, all) => {
|
|
8
|
-
for (var name in all)
|
|
9
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
-
};
|
|
11
|
-
var __copyProps = (to, from, except, desc) => {
|
|
12
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
-
for (let key of __getOwnPropNames(from))
|
|
14
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
-
}
|
|
17
|
-
return to;
|
|
18
|
-
};
|
|
19
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
-
var __publicField = (obj, key, value) => {
|
|
21
|
-
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
22
|
-
return value;
|
|
23
|
-
};
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
24
2
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
RequestService: () => RequestService,
|
|
32
|
-
setup: () => setup
|
|
33
|
-
});
|
|
34
|
-
module.exports = __toCommonJS(src_exports);
|
|
3
|
+
//#region src/interfaces/request-generate.interface.ts
|
|
4
|
+
let RequestGenerateType = /* @__PURE__ */ function(RequestGenerateType) {
|
|
5
|
+
RequestGenerateType["Request"] = "TO_REQUEST";
|
|
6
|
+
RequestGenerateType["URL"] = "TO_URL";
|
|
7
|
+
return RequestGenerateType;
|
|
8
|
+
}({});
|
|
35
9
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region src/interfaces/request-plugin.interface.ts
|
|
12
|
+
let PluginLifecycle = /* @__PURE__ */ function(PluginLifecycle) {
|
|
13
|
+
PluginLifecycle["before"] = "before";
|
|
14
|
+
PluginLifecycle["after"] = "after";
|
|
15
|
+
PluginLifecycle["catch"] = "catch";
|
|
16
|
+
return PluginLifecycle;
|
|
17
|
+
}({});
|
|
42
18
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
19
|
+
//#endregion
|
|
20
|
+
//#region src/interfaces/request-send.interface.ts
|
|
21
|
+
/**
|
|
22
|
+
* 请求方法类型
|
|
23
|
+
*/
|
|
24
|
+
let RequestMethod = /* @__PURE__ */ function(RequestMethod) {
|
|
25
|
+
RequestMethod["Get"] = "GET";
|
|
26
|
+
RequestMethod["Post"] = "POST";
|
|
27
|
+
RequestMethod["Put"] = "PUT";
|
|
28
|
+
RequestMethod["Delete"] = "DELETE";
|
|
29
|
+
RequestMethod["Options"] = "OPTIONS";
|
|
30
|
+
RequestMethod["Head"] = "HEAD";
|
|
31
|
+
RequestMethod["Patch"] = "PATCH";
|
|
32
|
+
return RequestMethod;
|
|
33
|
+
}({});
|
|
50
34
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
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
|
|
35
|
+
//#endregion
|
|
36
|
+
//#region src/utils/query-string.ts
|
|
37
|
+
/**
|
|
38
|
+
* 编码字符串
|
|
39
|
+
*/
|
|
64
40
|
function encodeValue(value, encode) {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
return encodeURIComponent(value);
|
|
41
|
+
if (!encode) return value;
|
|
42
|
+
return encodeURIComponent(value);
|
|
68
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* 处理嵌套对象的键名
|
|
46
|
+
*/
|
|
69
47
|
function getKeyWithDot(key, childKey, allowDots) {
|
|
70
|
-
|
|
48
|
+
return allowDots ? `${key}.${childKey}` : `${key}[${childKey}]`;
|
|
71
49
|
}
|
|
50
|
+
/**
|
|
51
|
+
* 递归处理对象值
|
|
52
|
+
*/
|
|
72
53
|
function processValue(value, options, parentKey = "") {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
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;
|
|
54
|
+
const result = [];
|
|
55
|
+
if (value === null || value === void 0) {
|
|
56
|
+
if (!options.skipNulls) result.push(`${parentKey}=`);
|
|
57
|
+
return result;
|
|
58
|
+
}
|
|
59
|
+
if (Array.isArray(value)) if (options.arrayFormat === "comma") {
|
|
60
|
+
const filteredValues = value.filter((item) => item !== null && item !== void 0);
|
|
61
|
+
if (filteredValues.length > 0) {
|
|
62
|
+
const encodedValue = encodeValue(filteredValues.map((item) => typeof item === "object" ? JSON.stringify(item) : item.toString()).join(","), options.encode !== false);
|
|
63
|
+
result.push(`${parentKey}=${encodedValue}`);
|
|
64
|
+
}
|
|
65
|
+
} else value.forEach((item, index) => {
|
|
66
|
+
let itemKey;
|
|
67
|
+
const shouldEncode = options.encode !== false;
|
|
68
|
+
if (options.arrayFormat === "indices") itemKey = parentKey ? `${parentKey}[${index}]` : `[${index}]`;
|
|
69
|
+
else if (options.arrayFormat === "brackets") itemKey = parentKey ? `${parentKey}[]` : "[]";
|
|
70
|
+
else itemKey = parentKey;
|
|
71
|
+
if (typeof item === "object" && item !== null) result.push(...processValue(item, options, itemKey));
|
|
72
|
+
else {
|
|
73
|
+
const encodedValue = encodeValue(item.toString(), shouldEncode);
|
|
74
|
+
result.push(`${itemKey}=${encodedValue}`);
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
else if (typeof value === "object") Object.keys(value).forEach((childKey) => {
|
|
78
|
+
const fullKey = parentKey ? getKeyWithDot(parentKey, childKey, options.allowDots || false) : childKey;
|
|
79
|
+
result.push(...processValue(value[childKey], options, fullKey));
|
|
80
|
+
});
|
|
81
|
+
else {
|
|
82
|
+
const shouldEncode = options.encode !== false;
|
|
83
|
+
const encodedValue = encodeValue(value.toString(), shouldEncode);
|
|
84
|
+
const encodedKey = options.encodeValuesOnly ? parentKey : encodeValue(parentKey, shouldEncode);
|
|
85
|
+
result.push(`${encodedKey}=${encodedValue}`);
|
|
86
|
+
}
|
|
87
|
+
return result;
|
|
122
88
|
}
|
|
89
|
+
/**
|
|
90
|
+
* 将对象序列化为查询字符串
|
|
91
|
+
*/
|
|
123
92
|
function stringify(obj, options = {}) {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
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;
|
|
93
|
+
const { arrayFormat = "indices", skipNulls = true, allowDots = true, encodeValuesOnly = false, encode = true, addQueryPrefix = false } = options;
|
|
94
|
+
if (!obj || typeof obj !== "object") return options.addQueryPrefix ? "?" : "";
|
|
95
|
+
const parts = [];
|
|
96
|
+
Object.keys(obj).forEach((key) => {
|
|
97
|
+
const value = obj[key];
|
|
98
|
+
const processedValues = processValue(value, {
|
|
99
|
+
arrayFormat,
|
|
100
|
+
skipNulls,
|
|
101
|
+
allowDots,
|
|
102
|
+
encodeValuesOnly,
|
|
103
|
+
encode,
|
|
104
|
+
addQueryPrefix
|
|
105
|
+
}, key);
|
|
106
|
+
parts.push(...processedValues);
|
|
107
|
+
});
|
|
108
|
+
const queryString = parts.join("&");
|
|
109
|
+
return addQueryPrefix ? queryString ? `?${queryString}` : "?" : queryString;
|
|
150
110
|
}
|
|
151
111
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
if (!_RequestService.config) {
|
|
277
|
-
throw new Error("\u8BF7\u68C0\u67E5\u8BF7\u6C42\u914D\u7F6E\u662F\u5426\u5B8C\u6210");
|
|
278
|
-
}
|
|
279
|
-
let hasException = false;
|
|
280
|
-
const adapter = this.getRequestAdapter();
|
|
281
|
-
const plugins = requestPlugins.filter(Boolean);
|
|
282
|
-
const extraParams = await this.execRequestPlugin(plugins, options);
|
|
283
|
-
const response = await this.startRequest(adapter, options, extraParams).then((response2) => adapter.transformResponse(response2)).catch((exception) => {
|
|
284
|
-
hasException = true;
|
|
285
|
-
return adapter.transformException(exception);
|
|
286
|
-
});
|
|
287
|
-
if (!hasException) {
|
|
288
|
-
this.execResponsePlugin("after" /* after */, plugins, options, response);
|
|
289
|
-
} else {
|
|
290
|
-
this.execResponsePlugin("catch" /* catch */, plugins, options, response);
|
|
291
|
-
}
|
|
292
|
-
return this.execInterceptors(response, hasException);
|
|
293
|
-
}
|
|
294
|
-
/**
|
|
295
|
-
* 生成请求路径
|
|
296
|
-
* @param {RequestSendOptions} options 请求选项
|
|
297
|
-
* @param {RequestPlugin[]} plugins 请求插件
|
|
298
|
-
* @returns string
|
|
299
|
-
*/
|
|
300
|
-
toURL(options, plugins = []) {
|
|
301
|
-
if (!_RequestService.config) {
|
|
302
|
-
throw new Error("\u8BF7\u68C0\u67E5\u8BF7\u6C42\u914D\u7F6E\u662F\u5426\u5B8C\u6210");
|
|
303
|
-
}
|
|
304
|
-
if (plugins && plugins.length) {
|
|
305
|
-
this.execRequestPlugin(
|
|
306
|
-
plugins.filter(Boolean),
|
|
307
|
-
options
|
|
308
|
-
);
|
|
309
|
-
}
|
|
310
|
-
const baseURL = _RequestService.config.gateway;
|
|
311
|
-
const pathURL = this.parseRequestPath(
|
|
312
|
-
options.path,
|
|
313
|
-
options.paramsPath,
|
|
314
|
-
options.service
|
|
315
|
-
);
|
|
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 || {}
|
|
324
|
-
});
|
|
325
|
-
return `${baseURL}${pathURL}${queryString}`;
|
|
326
|
-
}
|
|
112
|
+
//#endregion
|
|
113
|
+
//#region src/request-service.ts
|
|
114
|
+
var RequestService = class RequestService {
|
|
115
|
+
static config;
|
|
116
|
+
static instance;
|
|
117
|
+
/**
|
|
118
|
+
* 获取服务请求单例
|
|
119
|
+
*/
|
|
120
|
+
static getInstance() {
|
|
121
|
+
if (this.instance) return this.instance;
|
|
122
|
+
return new RequestService();
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* 获取RequestAdatper
|
|
126
|
+
* @returns RequestAdapter
|
|
127
|
+
*/
|
|
128
|
+
getRequestAdapter() {
|
|
129
|
+
if (!RequestService.config.adapter) throw new Error("请检查是否配置请求Adatper");
|
|
130
|
+
return RequestService.config.adapter;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* 执行前置插件逻辑
|
|
134
|
+
* @param plugins
|
|
135
|
+
* @param options
|
|
136
|
+
*/
|
|
137
|
+
async execRequestPlugin(plugins = [], options) {
|
|
138
|
+
const extraParams = {};
|
|
139
|
+
const appendParams = (params) => {
|
|
140
|
+
Object.assign(extraParams, params || {});
|
|
141
|
+
};
|
|
142
|
+
for (const plugin of RequestService.config.plugins) if (plugin.before) await plugin.before(options, appendParams);
|
|
143
|
+
for (const plugin of plugins) if (plugin.before) await plugin.before(options, appendParams);
|
|
144
|
+
return extraParams;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* 执行前置插件逻辑
|
|
148
|
+
*/
|
|
149
|
+
execResponsePlugin(leftcycle, plugins = [], options, response) {
|
|
150
|
+
plugins.forEach((plugin) => {
|
|
151
|
+
const leftcycleFn = plugin[leftcycle];
|
|
152
|
+
if (leftcycleFn) leftcycleFn.bind(plugin)(response, options);
|
|
153
|
+
});
|
|
154
|
+
RequestService.config.plugins.forEach((plugin) => {
|
|
155
|
+
const leftcycleFn = plugin[leftcycle];
|
|
156
|
+
if (leftcycleFn) leftcycleFn.bind(plugin)(response, options);
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* 转换请求路径
|
|
161
|
+
*/
|
|
162
|
+
parseRequestPath(path, paramsPath, service) {
|
|
163
|
+
if (service) path = `/${service}/${path}`.replace(/\/{2,3}/g, "/");
|
|
164
|
+
if (paramsPath) return Object.entries(paramsPath).reduce((r, [key, value]) => r.replace(`{${key}}`, value.toString()), path);
|
|
165
|
+
else return path;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* 开始请求
|
|
169
|
+
* @param adapter
|
|
170
|
+
* @param options
|
|
171
|
+
* @returns Promise
|
|
172
|
+
*/
|
|
173
|
+
startRequest(adapter, options, extraParams) {
|
|
174
|
+
return adapter.request({
|
|
175
|
+
baseURL: RequestService.config.gateway,
|
|
176
|
+
pathURL: this.parseRequestPath(options.path, options.paramsPath, options.service),
|
|
177
|
+
method: options.method,
|
|
178
|
+
headers: options.headers || {},
|
|
179
|
+
paramsQuery: options.paramsQuery,
|
|
180
|
+
paramsBody: options.paramsBody,
|
|
181
|
+
extraParams
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* 执行拦截器
|
|
186
|
+
* @param response 请求响应对象
|
|
187
|
+
* @returns Promise
|
|
188
|
+
*/
|
|
189
|
+
execInterceptors(response, hasException = false) {
|
|
190
|
+
const interceptors = RequestService.config?.interceptors;
|
|
191
|
+
if (!interceptors?.status || !interceptors?.error || !interceptors?.success || !interceptors?.exception) throw new Error("请检查拦截器配置");
|
|
192
|
+
const status = interceptors.status.exec(response) && !hasException;
|
|
193
|
+
if (hasException) interceptors.exception.exec(response);
|
|
194
|
+
if (status) return Promise.resolve(interceptors.success.exec(response));
|
|
195
|
+
else return Promise.reject(interceptors.error.exec(response));
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* 发送请求
|
|
199
|
+
* @param {RequestSendOptions} options 请求选项
|
|
200
|
+
* @param {RequestPlugin[]} requestPlugins 请求插件
|
|
201
|
+
* @returns Promise
|
|
202
|
+
*/
|
|
203
|
+
async send(options, requestPlugins = []) {
|
|
204
|
+
if (!RequestService.config) throw new Error("请检查请求配置是否完成");
|
|
205
|
+
let hasException = false;
|
|
206
|
+
const adapter = this.getRequestAdapter();
|
|
207
|
+
const plugins = requestPlugins.filter(Boolean);
|
|
208
|
+
const extraParams = await this.execRequestPlugin(plugins, options);
|
|
209
|
+
const response = await this.startRequest(adapter, options, extraParams).then((response) => adapter.transformResponse(response)).catch((exception) => {
|
|
210
|
+
hasException = true;
|
|
211
|
+
return adapter.transformException(exception);
|
|
212
|
+
});
|
|
213
|
+
if (!hasException) this.execResponsePlugin(PluginLifecycle.after, plugins, options, response);
|
|
214
|
+
else this.execResponsePlugin(PluginLifecycle.catch, plugins, options, response);
|
|
215
|
+
return this.execInterceptors(response, hasException);
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* 生成请求路径
|
|
219
|
+
* @param {RequestSendOptions} options 请求选项
|
|
220
|
+
* @param {RequestPlugin[]} plugins 请求插件
|
|
221
|
+
* @returns string
|
|
222
|
+
*/
|
|
223
|
+
toURL(options, plugins = []) {
|
|
224
|
+
if (!RequestService.config) throw new Error("请检查请求配置是否完成");
|
|
225
|
+
if (plugins && plugins.length) this.execRequestPlugin(plugins.filter(Boolean), options);
|
|
226
|
+
return `${RequestService.config.gateway}${this.parseRequestPath(options.path, options.paramsPath, options.service)}${stringify(options.paramsQuery || {}, {
|
|
227
|
+
arrayFormat: "repeat",
|
|
228
|
+
skipNulls: true,
|
|
229
|
+
allowDots: true,
|
|
230
|
+
encodeValuesOnly: true,
|
|
231
|
+
encode: true,
|
|
232
|
+
addQueryPrefix: true,
|
|
233
|
+
...RequestService.config.qs || {}
|
|
234
|
+
})}`;
|
|
235
|
+
}
|
|
327
236
|
};
|
|
328
|
-
var RequestService = _RequestService;
|
|
329
|
-
__publicField(RequestService, "config");
|
|
330
|
-
__publicField(RequestService, "instance");
|
|
331
237
|
|
|
332
|
-
|
|
238
|
+
//#endregion
|
|
239
|
+
//#region src/request-setup.ts
|
|
333
240
|
function setup(config) {
|
|
334
|
-
|
|
335
|
-
|
|
241
|
+
RequestService.config = config;
|
|
242
|
+
RequestService.config?.adapter?.injectConfig?.(config);
|
|
336
243
|
}
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
});
|
|
244
|
+
|
|
245
|
+
//#endregion
|
|
246
|
+
exports.PluginLifecycle = PluginLifecycle;
|
|
247
|
+
exports.RequestGenerateType = RequestGenerateType;
|
|
248
|
+
exports.RequestMethod = RequestMethod;
|
|
249
|
+
exports.RequestService = RequestService;
|
|
250
|
+
exports.setup = setup;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { a as RequestSetupConfig, c as RequestLifecycle, d as RequestMethod, f as RequestSendOptions, i as RequestAdapterOptions, l as RequestPlugin, n as AdapterResponseHeaders, o as ResponseInterceptor, r as RequestAdapter, s as PluginLifecycle, t as AdapterResponse, u as ResponseLifecycle } from "./request-adapter.interface-Cr5s4kIp.cjs";
|
|
2
|
+
|
|
3
|
+
//#region src/interfaces/request-generate.interface.d.ts
|
|
4
|
+
declare enum RequestGenerateType {
|
|
5
|
+
Request = "TO_REQUEST",
|
|
6
|
+
URL = "TO_URL"
|
|
7
|
+
}
|
|
8
|
+
interface RequestGenerateOptions {
|
|
9
|
+
type?: RequestGenerateType;
|
|
10
|
+
}
|
|
11
|
+
//#endregion
|
|
12
|
+
//#region src/request-service.d.ts
|
|
13
|
+
declare class RequestService {
|
|
14
|
+
static config: RequestSetupConfig;
|
|
15
|
+
static instance: RequestService;
|
|
16
|
+
/**
|
|
17
|
+
* 获取服务请求单例
|
|
18
|
+
*/
|
|
19
|
+
static getInstance(): RequestService;
|
|
20
|
+
/**
|
|
21
|
+
* 获取RequestAdatper
|
|
22
|
+
* @returns RequestAdapter
|
|
23
|
+
*/
|
|
24
|
+
private getRequestAdapter;
|
|
25
|
+
/**
|
|
26
|
+
* 执行前置插件逻辑
|
|
27
|
+
* @param plugins
|
|
28
|
+
* @param options
|
|
29
|
+
*/
|
|
30
|
+
private execRequestPlugin;
|
|
31
|
+
/**
|
|
32
|
+
* 执行前置插件逻辑
|
|
33
|
+
*/
|
|
34
|
+
private execResponsePlugin;
|
|
35
|
+
/**
|
|
36
|
+
* 转换请求路径
|
|
37
|
+
*/
|
|
38
|
+
private parseRequestPath;
|
|
39
|
+
/**
|
|
40
|
+
* 开始请求
|
|
41
|
+
* @param adapter
|
|
42
|
+
* @param options
|
|
43
|
+
* @returns Promise
|
|
44
|
+
*/
|
|
45
|
+
private startRequest;
|
|
46
|
+
/**
|
|
47
|
+
* 执行拦截器
|
|
48
|
+
* @param response 请求响应对象
|
|
49
|
+
* @returns Promise
|
|
50
|
+
*/
|
|
51
|
+
private execInterceptors;
|
|
52
|
+
/**
|
|
53
|
+
* 发送请求
|
|
54
|
+
* @param {RequestSendOptions} options 请求选项
|
|
55
|
+
* @param {RequestPlugin[]} requestPlugins 请求插件
|
|
56
|
+
* @returns Promise
|
|
57
|
+
*/
|
|
58
|
+
send(options: RequestSendOptions, requestPlugins?: (RequestPlugin | undefined)[]): Promise<any>;
|
|
59
|
+
/**
|
|
60
|
+
* 生成请求路径
|
|
61
|
+
* @param {RequestSendOptions} options 请求选项
|
|
62
|
+
* @param {RequestPlugin[]} plugins 请求插件
|
|
63
|
+
* @returns string
|
|
64
|
+
*/
|
|
65
|
+
toURL(options: RequestSendOptions, plugins?: (RequestPlugin | undefined)[]): string;
|
|
66
|
+
}
|
|
67
|
+
//#endregion
|
|
68
|
+
//#region src/request-setup.d.ts
|
|
69
|
+
declare function setup(config: RequestSetupConfig): void;
|
|
70
|
+
//#endregion
|
|
71
|
+
export { AdapterResponse, AdapterResponseHeaders, PluginLifecycle, RequestAdapter, RequestAdapterOptions, RequestGenerateOptions, RequestGenerateType, RequestLifecycle, RequestMethod, RequestPlugin, RequestSendOptions, RequestService, RequestSetupConfig, ResponseInterceptor, ResponseLifecycle, setup };
|