axios-wrapper-pro 1.0.4 → 1.0.5
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/index.d.mts +209 -0
- package/dist/index.d.ts +73 -11
- package/dist/index.js +362 -313
- package/dist/index.mjs +339 -0
- package/dist/utils.d.ts +1 -1
- package/dist/utils.d.ts.map +1 -1
- package/package.json +5 -4
package/dist/index.js
CHANGED
|
@@ -1,325 +1,374 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
this._instance.interceptors.response.eject(this._responseInterceptorId);
|
|
93
|
-
}
|
|
94
|
-
this._interceptorConfig.response = interceptor;
|
|
95
|
-
this._interceptorConfig.responseError = errorInterceptor;
|
|
96
|
-
this._initResponseInterceptors();
|
|
97
|
-
}
|
|
98
|
-
/**
|
|
99
|
-
* 设置取消控制器
|
|
100
|
-
* @param {AxiosRequestConfig} config 请求配置
|
|
101
|
-
* @param {RequestKey} customKey 自定义key
|
|
102
|
-
* @returns {InternalAxiosRequestConfig} 处理后的配置
|
|
103
|
-
*/
|
|
104
|
-
_setupAbortController(config, customKey) {
|
|
105
|
-
const key = customKey !== null && customKey !== void 0 ? customKey : this._generateRequestKey(config);
|
|
106
|
-
// 取消已存在的相同请求
|
|
107
|
-
this.cancelRequest(key);
|
|
108
|
-
// 设置当前请求的AbortController
|
|
109
|
-
const controller = new AbortController();
|
|
110
|
-
this._abortControllers.set(key, controller);
|
|
111
|
-
// 将 key 保存到 config 中,确保响应拦截器使用相同的 key
|
|
112
|
-
return { ...config, signal: controller.signal, _requestKey: key };
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* 生成请求唯一标识
|
|
116
|
-
* @param {AxiosRequestConfig} config 请求配置
|
|
117
|
-
* @returns {string} 请求唯一标识
|
|
118
|
-
*/
|
|
119
|
-
_generateRequestKey(config) {
|
|
120
|
-
const method = (config.method || "GET").toUpperCase();
|
|
121
|
-
const url = config.url || "";
|
|
122
|
-
// 复制params对象,避免修改原始配置
|
|
123
|
-
const params = config.params ? { ...config.params } : null;
|
|
124
|
-
// 删除_t参数,避免影响生成请求唯一标识
|
|
125
|
-
if (params === null || params === void 0 ? void 0 : params._t) {
|
|
126
|
-
delete params._t;
|
|
127
|
-
}
|
|
128
|
-
// 获取序列化函数:优先使用请求级配置,其次使用实例级配置
|
|
129
|
-
const serializer = isFunction(config.paramsSerializer) ? config.paramsSerializer : this._paramsSerializer;
|
|
130
|
-
let paramsStr = "";
|
|
131
|
-
// 所有HTTP方法都可能携带params参数
|
|
132
|
-
if (params) {
|
|
133
|
-
paramsStr = serializer ? serializer(params) : this._stableStringify(params);
|
|
134
|
-
}
|
|
135
|
-
// POST/PUT/PATCH/DELETE 可能同时携带 data
|
|
136
|
-
let dataStr = "";
|
|
137
|
-
if (["POST", "PUT", "PATCH", "DELETE"].includes(method) && config.data) {
|
|
138
|
-
dataStr = typeof config.data === "string" ? config.data : this._stableStringify(config.data);
|
|
139
|
-
}
|
|
140
|
-
return `${method}|${url}|${paramsStr}|${dataStr}`;
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
AxiosWrapperPro: () => AxiosWrapperPro
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(index_exports);
|
|
36
|
+
var import_axios2 = __toESM(require("axios"));
|
|
37
|
+
|
|
38
|
+
// src/utils.ts
|
|
39
|
+
var import_axios = __toESM(require("axios"));
|
|
40
|
+
function isFunction(variable) {
|
|
41
|
+
return typeof variable === "function";
|
|
42
|
+
}
|
|
43
|
+
function sleep(ms) {
|
|
44
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
45
|
+
}
|
|
46
|
+
function isCancel(error) {
|
|
47
|
+
return import_axios.default.isCancel(error);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// src/config.ts
|
|
51
|
+
var DEFAULT_CONFIG = {
|
|
52
|
+
baseURL: "",
|
|
53
|
+
timeout: 1e4,
|
|
54
|
+
headers: {
|
|
55
|
+
"Content-Type": "application/json;charset=utf-8"
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
var DEFAULT_RETRY_CONFIG = {
|
|
59
|
+
count: 0,
|
|
60
|
+
delay: 1e3,
|
|
61
|
+
condition: (error) => {
|
|
62
|
+
var _a;
|
|
63
|
+
if (isCancel(error)) return false;
|
|
64
|
+
if (!(error == null ? void 0 : error.response)) return true;
|
|
65
|
+
return ((_a = error == null ? void 0 : error.response) == null ? void 0 : _a.status) >= 500;
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
var DEFAULT_INTERCEPTOR = {
|
|
69
|
+
request: (config) => config,
|
|
70
|
+
requestError: (error) => Promise.reject(error),
|
|
71
|
+
response: (response) => response,
|
|
72
|
+
responseError: (error) => Promise.reject(error)
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
// src/index.ts
|
|
76
|
+
var AxiosWrapperPro = class {
|
|
77
|
+
constructor(options = {}) {
|
|
78
|
+
this._abortControllers = /* @__PURE__ */ new Map();
|
|
79
|
+
this._retryConfig = {
|
|
80
|
+
...DEFAULT_RETRY_CONFIG,
|
|
81
|
+
...(options == null ? void 0 : options.retry) || {}
|
|
82
|
+
};
|
|
83
|
+
this._paramsSerializer = isFunction(options.paramsSerializer) ? options.paramsSerializer : null;
|
|
84
|
+
const axiosConfig = {
|
|
85
|
+
...DEFAULT_CONFIG,
|
|
86
|
+
...options.baseURL && { baseURL: options.baseURL },
|
|
87
|
+
...options.timeout && { timeout: options.timeout },
|
|
88
|
+
headers: { ...DEFAULT_CONFIG.headers, ...options.headers }
|
|
89
|
+
};
|
|
90
|
+
if (this._paramsSerializer) {
|
|
91
|
+
axiosConfig.paramsSerializer = this._paramsSerializer;
|
|
141
92
|
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
93
|
+
this._instance = import_axios2.default.create(axiosConfig);
|
|
94
|
+
this._interceptorConfig = options.interceptors || {};
|
|
95
|
+
this._initRequestInterceptors();
|
|
96
|
+
this._initResponseInterceptors();
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* 获取拦截器配置,未配置的使用默认值
|
|
100
|
+
* @returns {InterceptorConfig} 完整的拦截器配置
|
|
101
|
+
*/
|
|
102
|
+
_getInterceptors() {
|
|
103
|
+
const { request, requestError, response, responseError } = this._interceptorConfig;
|
|
104
|
+
return {
|
|
105
|
+
request: isFunction(request) ? request : DEFAULT_INTERCEPTOR.request,
|
|
106
|
+
requestError: isFunction(requestError) ? requestError : DEFAULT_INTERCEPTOR.requestError,
|
|
107
|
+
response: isFunction(response) ? response : DEFAULT_INTERCEPTOR.response,
|
|
108
|
+
responseError: isFunction(responseError) ? responseError : DEFAULT_INTERCEPTOR.responseError
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* 初始化请求拦截器
|
|
113
|
+
*/
|
|
114
|
+
_initRequestInterceptors() {
|
|
115
|
+
const { request, requestError } = this._getInterceptors();
|
|
116
|
+
this._requestInterceptorId = this._instance.interceptors.request.use(request, requestError);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* 初始化响应拦截器
|
|
120
|
+
*/
|
|
121
|
+
_initResponseInterceptors() {
|
|
122
|
+
const { response, responseError } = this._getInterceptors();
|
|
123
|
+
this._responseInterceptorId = this._instance.interceptors.response.use(
|
|
124
|
+
(res) => {
|
|
125
|
+
const key = res.config._requestKey;
|
|
126
|
+
key && this._abortControllers.delete(key);
|
|
127
|
+
return response(res);
|
|
128
|
+
},
|
|
129
|
+
(error) => {
|
|
130
|
+
var _a;
|
|
131
|
+
const key = (_a = error.config) == null ? void 0 : _a._requestKey;
|
|
132
|
+
key && this._abortControllers.delete(key);
|
|
133
|
+
return responseError(error);
|
|
134
|
+
}
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* 设置请求拦截器,移除旧拦截器,更新拦截器配置,重新初始化拦截器
|
|
139
|
+
* @param {InterceptorConfig["request"]} interceptor 拦截器
|
|
140
|
+
* @param {InterceptorConfig["requestError"]} errorInterceptor 错误拦截器
|
|
141
|
+
*/
|
|
142
|
+
setRequestInterceptor(interceptor, errorInterceptor) {
|
|
143
|
+
if (this._requestInterceptorId !== void 0) {
|
|
144
|
+
this._instance.interceptors.request.eject(this._requestInterceptorId);
|
|
160
145
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
}
|
|
174
|
-
return false;
|
|
146
|
+
this._interceptorConfig.request = interceptor;
|
|
147
|
+
this._interceptorConfig.requestError = errorInterceptor;
|
|
148
|
+
this._initRequestInterceptors();
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* 设置响应拦截器,移除旧拦截器,更新拦截器配置,重新初始化拦截器
|
|
152
|
+
* @param {InterceptorConfig["response"]} interceptor 拦截器
|
|
153
|
+
* @param {InterceptorConfig["responseError"]} errorInterceptor 错误拦截器
|
|
154
|
+
*/
|
|
155
|
+
setResponseInterceptor(interceptor, errorInterceptor) {
|
|
156
|
+
if (this._responseInterceptorId !== void 0) {
|
|
157
|
+
this._instance.interceptors.response.eject(this._responseInterceptorId);
|
|
175
158
|
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
159
|
+
this._interceptorConfig.response = interceptor;
|
|
160
|
+
this._interceptorConfig.responseError = errorInterceptor;
|
|
161
|
+
this._initResponseInterceptors();
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* 设置取消控制器
|
|
165
|
+
* @param {AxiosRequestConfig} config 请求配置
|
|
166
|
+
* @param {RequestKey} customKey 自定义key
|
|
167
|
+
* @returns {InternalAxiosRequestConfig} 处理后的配置
|
|
168
|
+
*/
|
|
169
|
+
_setupAbortController(config, customKey) {
|
|
170
|
+
const key = customKey != null ? customKey : this._generateRequestKey(config);
|
|
171
|
+
this.cancelRequest(key);
|
|
172
|
+
const controller = new AbortController();
|
|
173
|
+
this._abortControllers.set(key, controller);
|
|
174
|
+
return { ...config, signal: controller.signal, _requestKey: key };
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* 生成请求唯一标识
|
|
178
|
+
* @param {AxiosRequestConfig} config 请求配置
|
|
179
|
+
* @returns {string} 请求唯一标识
|
|
180
|
+
*/
|
|
181
|
+
_generateRequestKey(config) {
|
|
182
|
+
const method = (config.method || "GET").toUpperCase();
|
|
183
|
+
const url = config.url || "";
|
|
184
|
+
const params = config.params ? { ...config.params } : null;
|
|
185
|
+
if (params == null ? void 0 : params._t) {
|
|
186
|
+
delete params._t;
|
|
188
187
|
}
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
*/
|
|
194
|
-
setHeader(key, value) {
|
|
195
|
-
this._instance.defaults.headers.common[key] = value;
|
|
188
|
+
const serializer = isFunction(config.paramsSerializer) ? config.paramsSerializer : this._paramsSerializer;
|
|
189
|
+
let paramsStr = "";
|
|
190
|
+
if (params) {
|
|
191
|
+
paramsStr = serializer ? serializer(params) : this._stableStringify(params);
|
|
196
192
|
}
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
*/
|
|
201
|
-
setHeaders(headers) {
|
|
202
|
-
Object.entries(headers).forEach(([key, value]) => {
|
|
203
|
-
this._instance.defaults.headers.common[key] = value;
|
|
204
|
-
});
|
|
193
|
+
let dataStr = "";
|
|
194
|
+
if (["POST", "PUT", "PATCH", "DELETE"].includes(method) && config.data) {
|
|
195
|
+
dataStr = typeof config.data === "string" ? config.data : this._stableStringify(config.data);
|
|
205
196
|
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
197
|
+
return `${method}|${url}|${paramsStr}|${dataStr}`;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* 稳定的JSON序列化,确保相同内容生成相同字符串
|
|
201
|
+
* @param {any} obj 待序列化对象
|
|
202
|
+
* @returns {string} 序列化后的字符串
|
|
203
|
+
*/
|
|
204
|
+
_stableStringify(obj) {
|
|
205
|
+
if (obj === null || obj === void 0) {
|
|
206
|
+
return "";
|
|
212
207
|
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
return this._instance.defaults.baseURL || "";
|
|
208
|
+
if (Array.isArray(obj)) {
|
|
209
|
+
return "[" + obj.map((item) => this._stableStringify(item)).join(",") + "]";
|
|
216
210
|
}
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
211
|
+
if (typeof obj === "object") {
|
|
212
|
+
const sortedKeys = Object.keys(obj).sort();
|
|
213
|
+
const pairs = sortedKeys.map((key) => `"${key}":${this._stableStringify(obj[key])}`);
|
|
214
|
+
return "{" + pairs.join(",") + "}";
|
|
220
215
|
}
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
216
|
+
return JSON.stringify(obj);
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* 取消指定请求
|
|
220
|
+
* @param {RequestKey} key 请求标识
|
|
221
|
+
* @param {string} [reason] 取消原因
|
|
222
|
+
* @returns {boolean} 是否成功取消
|
|
223
|
+
*/
|
|
224
|
+
cancelRequest(key, reason) {
|
|
225
|
+
const controller = this._abortControllers.get(key);
|
|
226
|
+
if (controller) {
|
|
227
|
+
controller.abort(reason || "\u8BF7\u6C42\u5DF2\u53D6\u6D88");
|
|
228
|
+
this._abortControllers.delete(key);
|
|
229
|
+
return true;
|
|
224
230
|
}
|
|
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
|
-
|
|
231
|
+
return false;
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* 取消所有请求
|
|
235
|
+
* @param {string} [reason] 取消原因
|
|
236
|
+
* @returns {number} 被取消的请求数量
|
|
237
|
+
*/
|
|
238
|
+
cancelAllRequests(reason) {
|
|
239
|
+
const count = this._abortControllers.size;
|
|
240
|
+
this._abortControllers.forEach((controller) => {
|
|
241
|
+
controller.abort(reason || "\u8BF7\u6C42\u5DF2\u53D6\u6D88");
|
|
242
|
+
});
|
|
243
|
+
this._abortControllers.clear();
|
|
244
|
+
return count;
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* 设置默认请求头
|
|
248
|
+
* @param {string} key 请求头名称
|
|
249
|
+
* @param {string} value 请求头值
|
|
250
|
+
*/
|
|
251
|
+
setHeader(key, value) {
|
|
252
|
+
this._instance.defaults.headers.common[key] = value;
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* 批量设置请求头
|
|
256
|
+
* @param {Record<string, string>} headers 请求头对象
|
|
257
|
+
*/
|
|
258
|
+
setHeaders(headers) {
|
|
259
|
+
Object.entries(headers).forEach(([key, value]) => {
|
|
260
|
+
this._instance.defaults.headers.common[key] = value;
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* 移除请求头
|
|
265
|
+
* @param {string} key 请求头名称
|
|
266
|
+
*/
|
|
267
|
+
removeHeader(key) {
|
|
268
|
+
delete this._instance.defaults.headers.common[key];
|
|
269
|
+
}
|
|
270
|
+
/** 获取基础URL */
|
|
271
|
+
getBaseURL() {
|
|
272
|
+
return this._instance.defaults.baseURL || "";
|
|
273
|
+
}
|
|
274
|
+
/** 设置基础URL */
|
|
275
|
+
setBaseURL(url) {
|
|
276
|
+
this._instance.defaults.baseURL = url;
|
|
277
|
+
}
|
|
278
|
+
/** 获取 Axios 实例 */
|
|
279
|
+
getInstance() {
|
|
280
|
+
return this._instance;
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* 通用请求方法
|
|
284
|
+
* @param {string} method 请求方法
|
|
285
|
+
* @param {string} url 请求地址
|
|
286
|
+
* @param {RequestConfig} [config={}] 请求配置
|
|
287
|
+
* @returns {Promise<AxiosResponse>} 响应数据
|
|
288
|
+
*/
|
|
289
|
+
async request(method, url, config = {}) {
|
|
290
|
+
const { requestKey, retry, ...restConfig } = config;
|
|
291
|
+
const retryConfig = {
|
|
292
|
+
...this._retryConfig,
|
|
293
|
+
...retry || {}
|
|
294
|
+
};
|
|
295
|
+
const requestConfig = { method, url, ...restConfig };
|
|
296
|
+
let lastError = null;
|
|
297
|
+
const configKey = requestKey != null ? requestKey : this._generateRequestKey(requestConfig);
|
|
298
|
+
for (let attempt = 0; attempt <= retryConfig.count; attempt++) {
|
|
299
|
+
try {
|
|
300
|
+
if (attempt > 0) {
|
|
301
|
+
this._abortControllers.delete(configKey);
|
|
272
302
|
}
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
* @returns {Promise<AxiosResponse>} 响应数据
|
|
290
|
-
*/
|
|
291
|
-
post(url, data = {}, options = {}) {
|
|
292
|
-
return this.request("POST", url, { ...options, data });
|
|
293
|
-
}
|
|
294
|
-
/**
|
|
295
|
-
* PUT请求
|
|
296
|
-
* @param {string} url 请求地址
|
|
297
|
-
* @param {any} [data] 请求数据
|
|
298
|
-
* @param {RequestConfig} [options] 其他选项
|
|
299
|
-
* @returns {Promise<AxiosResponse>} 响应数据
|
|
300
|
-
*/
|
|
301
|
-
put(url, data = {}, options = {}) {
|
|
302
|
-
return this.request("PUT", url, { ...options, data });
|
|
303
|
-
}
|
|
304
|
-
/**
|
|
305
|
-
* DELETE请求
|
|
306
|
-
* @param {string} url 请求地址
|
|
307
|
-
* @param {RequestConfig} [options] 请求选项
|
|
308
|
-
* @returns {Promise<AxiosResponse>} 响应数据
|
|
309
|
-
*/
|
|
310
|
-
delete(url, options = {}) {
|
|
311
|
-
return this.request("DELETE", url, options);
|
|
312
|
-
}
|
|
313
|
-
/**
|
|
314
|
-
* PATCH请求
|
|
315
|
-
* @param {string} url 请求地址
|
|
316
|
-
* @param {any} [data] 请求数据
|
|
317
|
-
* @param {RequestConfig} [options] 其他选项
|
|
318
|
-
* @returns {Promise<AxiosResponse>} 响应数据
|
|
319
|
-
*/
|
|
320
|
-
patch(url, data = {}, options = {}) {
|
|
321
|
-
return this.request("PATCH", url, { ...options, data });
|
|
303
|
+
const finalConfig = this._setupAbortController(requestConfig, configKey);
|
|
304
|
+
const result = await this._instance.request(finalConfig);
|
|
305
|
+
return result;
|
|
306
|
+
} catch (error) {
|
|
307
|
+
lastError = error;
|
|
308
|
+
this._abortControllers.delete(configKey);
|
|
309
|
+
const isLast = attempt === retryConfig.count;
|
|
310
|
+
const shouldRetry = retryConfig.condition(error);
|
|
311
|
+
const isCancelled = import_axios2.default.isCancel(error);
|
|
312
|
+
if (isLast || !shouldRetry || isCancelled) {
|
|
313
|
+
break;
|
|
314
|
+
}
|
|
315
|
+
if (retryConfig.delay > 0) {
|
|
316
|
+
await sleep(retryConfig.delay);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
322
319
|
}
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
320
|
+
return Promise.reject(lastError);
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* GET请求
|
|
324
|
+
* @param {string} url 请求地址
|
|
325
|
+
* @param {RequestConfig} [options] 请求选项
|
|
326
|
+
* @returns {Promise<AxiosResponse>} 响应数据
|
|
327
|
+
*/
|
|
328
|
+
get(url, options = {}) {
|
|
329
|
+
return this.request("GET", url, options);
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* POST请求
|
|
333
|
+
* @param {string} url 请求地址
|
|
334
|
+
* @param {any} [data] 请求数据
|
|
335
|
+
* @param {RequestConfig} [options] 其他选项
|
|
336
|
+
* @returns {Promise<AxiosResponse>} 响应数据
|
|
337
|
+
*/
|
|
338
|
+
post(url, data = {}, options = {}) {
|
|
339
|
+
return this.request("POST", url, { ...options, data });
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* PUT请求
|
|
343
|
+
* @param {string} url 请求地址
|
|
344
|
+
* @param {any} [data] 请求数据
|
|
345
|
+
* @param {RequestConfig} [options] 其他选项
|
|
346
|
+
* @returns {Promise<AxiosResponse>} 响应数据
|
|
347
|
+
*/
|
|
348
|
+
put(url, data = {}, options = {}) {
|
|
349
|
+
return this.request("PUT", url, { ...options, data });
|
|
350
|
+
}
|
|
351
|
+
/**
|
|
352
|
+
* DELETE请求
|
|
353
|
+
* @param {string} url 请求地址
|
|
354
|
+
* @param {RequestConfig} [options] 请求选项
|
|
355
|
+
* @returns {Promise<AxiosResponse>} 响应数据
|
|
356
|
+
*/
|
|
357
|
+
delete(url, options = {}) {
|
|
358
|
+
return this.request("DELETE", url, options);
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* PATCH请求
|
|
362
|
+
* @param {string} url 请求地址
|
|
363
|
+
* @param {any} [data] 请求数据
|
|
364
|
+
* @param {RequestConfig} [options] 其他选项
|
|
365
|
+
* @returns {Promise<AxiosResponse>} 响应数据
|
|
366
|
+
*/
|
|
367
|
+
patch(url, data = {}, options = {}) {
|
|
368
|
+
return this.request("PATCH", url, { ...options, data });
|
|
369
|
+
}
|
|
370
|
+
};
|
|
371
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
372
|
+
0 && (module.exports = {
|
|
373
|
+
AxiosWrapperPro
|
|
374
|
+
});
|