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.js CHANGED
@@ -1,325 +1,374 @@
1
- import axios from "axios";
2
- import { isFunction, sleep } from "./utils";
3
- import { DEFAULT_CONFIG, DEFAULT_RETRY_CONFIG, DEFAULT_INTERCEPTOR } from "./config";
4
- class AxiosWrapperPro {
5
- constructor(options = {}) {
6
- this._abortControllers = new Map();
7
- // 接口重试配置
8
- this._retryConfig = {
9
- ...DEFAULT_RETRY_CONFIG,
10
- ...((options === null || options === void 0 ? void 0 : options.retry) || {}),
11
- };
12
- // 保存参数序列化函数
13
- this._paramsSerializer = isFunction(options.paramsSerializer) ? options.paramsSerializer : null;
14
- // 创建axios实例
15
- const axiosConfig = {
16
- ...DEFAULT_CONFIG,
17
- ...(options.baseURL && { baseURL: options.baseURL }),
18
- ...(options.timeout && { timeout: options.timeout }),
19
- headers: { ...DEFAULT_CONFIG.headers, ...options.headers },
20
- };
21
- if (this._paramsSerializer) {
22
- axiosConfig.paramsSerializer = this._paramsSerializer;
23
- }
24
- this._instance = axios.create(axiosConfig);
25
- // 保存拦截器配置
26
- this._interceptorConfig = options.interceptors || {};
27
- // 初始化拦截器
28
- this._initRequestInterceptors();
29
- this._initResponseInterceptors();
30
- }
31
- /**
32
- * 获取拦截器配置,未配置的使用默认值
33
- * @returns {InterceptorConfig} 完整的拦截器配置
34
- */
35
- _getInterceptors() {
36
- const { request, requestError, response, responseError } = this._interceptorConfig;
37
- return {
38
- request: isFunction(request) ? request : DEFAULT_INTERCEPTOR.request,
39
- requestError: isFunction(requestError) ? requestError : DEFAULT_INTERCEPTOR.requestError,
40
- response: isFunction(response) ? response : DEFAULT_INTERCEPTOR.response,
41
- responseError: isFunction(responseError) ? responseError : DEFAULT_INTERCEPTOR.responseError,
42
- };
43
- }
44
- /**
45
- * 初始化请求拦截器
46
- */
47
- _initRequestInterceptors() {
48
- const { request, requestError } = this._getInterceptors();
49
- this._requestInterceptorId = this._instance.interceptors.request.use(request, requestError);
50
- }
51
- /**
52
- * 初始化响应拦截器
53
- */
54
- _initResponseInterceptors() {
55
- const { response, responseError } = this._getInterceptors();
56
- // 响应拦截器
57
- this._responseInterceptorId = this._instance.interceptors.response.use((res) => {
58
- // 清理AbortController,使用请求时保存的 key 确保一致性
59
- const key = res.config._requestKey;
60
- key && this._abortControllers.delete(key);
61
- // 执行自定义响应拦截器
62
- return response(res);
63
- }, (error) => {
64
- var _a;
65
- // 清理AbortController,使用请求时保存的 key 确保一致性
66
- const key = (_a = error.config) === null || _a === void 0 ? void 0 : _a._requestKey;
67
- key && this._abortControllers.delete(key);
68
- // 执行自定义响应错误拦截器
69
- return responseError(error);
70
- });
71
- }
72
- /**
73
- * 设置请求拦截器,移除旧拦截器,更新拦截器配置,重新初始化拦截器
74
- * @param {InterceptorConfig["request"]} interceptor 拦截器
75
- * @param {InterceptorConfig["requestError"]} errorInterceptor 错误拦截器
76
- */
77
- setRequestInterceptor(interceptor, errorInterceptor) {
78
- if (this._requestInterceptorId !== undefined) {
79
- this._instance.interceptors.request.eject(this._requestInterceptorId);
80
- }
81
- this._interceptorConfig.request = interceptor;
82
- this._interceptorConfig.requestError = errorInterceptor;
83
- this._initRequestInterceptors();
84
- }
85
- /**
86
- * 设置响应拦截器,移除旧拦截器,更新拦截器配置,重新初始化拦截器
87
- * @param {InterceptorConfig["response"]} interceptor 拦截器
88
- * @param {InterceptorConfig["responseError"]} errorInterceptor 错误拦截器
89
- */
90
- setResponseInterceptor(interceptor, errorInterceptor) {
91
- if (this._responseInterceptorId !== undefined) {
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
- * 稳定的JSON序列化,确保相同内容生成相同字符串
144
- * @param {any} obj 待序列化对象
145
- * @returns {string} 序列化后的字符串
146
- */
147
- _stableStringify(obj) {
148
- if (obj === null || obj === undefined) {
149
- return "";
150
- }
151
- if (Array.isArray(obj)) {
152
- return "[" + obj.map((item) => this._stableStringify(item)).join(",") + "]";
153
- }
154
- if (typeof obj === "object") {
155
- const sortedKeys = Object.keys(obj).sort();
156
- const pairs = sortedKeys.map((key) => `"${key}":${this._stableStringify(obj[key])}`);
157
- return "{" + pairs.join(",") + "}";
158
- }
159
- return JSON.stringify(obj);
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
- * @param {RequestKey} key 请求标识
164
- * @param {string} [reason] 取消原因
165
- * @returns {boolean} 是否成功取消
166
- */
167
- cancelRequest(key, reason) {
168
- const controller = this._abortControllers.get(key);
169
- if (controller) {
170
- controller.abort(reason || "请求已取消");
171
- this._abortControllers.delete(key);
172
- return true;
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
- * @param {string} [reason] 取消原因
179
- * @returns {number} 被取消的请求数量
180
- */
181
- cancelAllRequests(reason) {
182
- const count = this._abortControllers.size;
183
- this._abortControllers.forEach((controller) => {
184
- controller.abort(reason || "请求已取消");
185
- });
186
- this._abortControllers.clear();
187
- return count;
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
- * @param {string} key 请求头名称
192
- * @param {string} value 请求头值
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
- * @param {Record<string, string>} headers 请求头对象
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
- * @param {string} key 请求头名称
209
- */
210
- removeHeader(key) {
211
- delete this._instance.defaults.headers.common[key];
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
- /** 获取基础URL */
214
- getBaseURL() {
215
- return this._instance.defaults.baseURL || "";
208
+ if (Array.isArray(obj)) {
209
+ return "[" + obj.map((item) => this._stableStringify(item)).join(",") + "]";
216
210
  }
217
- /** 设置基础URL */
218
- setBaseURL(url) {
219
- this._instance.defaults.baseURL = url;
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
- /** 获取 Axios 实例 */
222
- getInstance() {
223
- return this._instance;
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
- * @param {string} method 请求方法
228
- * @param {string} url 请求地址
229
- * @param {RequestConfig} [config={}] 请求配置
230
- * @returns {Promise<AxiosResponse>} 响应数据
231
- */
232
- async request(method, url, config = {}) {
233
- const { requestKey, retry, ...restConfig } = config;
234
- // 合并重试配置
235
- const retryConfig = {
236
- ...this._retryConfig,
237
- ...(retry || {}),
238
- };
239
- // 构建请求配置,避免重复的 method 和 url
240
- const requestConfig = { method, url, ...restConfig };
241
- let lastError = null;
242
- const configKey = requestKey !== null && requestKey !== void 0 ? requestKey : this._generateRequestKey(requestConfig);
243
- // 重试循环
244
- for (let attempt = 0; attempt <= retryConfig.count; attempt++) {
245
- try {
246
- // 重试时清理旧的AbortController
247
- if (attempt > 0) {
248
- this._abortControllers.delete(configKey);
249
- }
250
- // 设置AbortController
251
- const finalConfig = this._setupAbortController(requestConfig, configKey);
252
- // 调用接口
253
- const result = await this._instance.request(finalConfig);
254
- return result;
255
- }
256
- catch (error) {
257
- lastError = error;
258
- // 清理旧的AbortController
259
- this._abortControllers.delete(configKey);
260
- // 判断是否继续重试
261
- const isLast = attempt === retryConfig.count;
262
- const shouldRetry = retryConfig.condition(error);
263
- const isCancelled = axios.isCancel(error);
264
- if (isLast || !shouldRetry || isCancelled) {
265
- break;
266
- }
267
- // 延迟重试
268
- if (retryConfig.delay > 0) {
269
- await sleep(retryConfig.delay);
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
- return Promise.reject(lastError);
274
- }
275
- /**
276
- * GET请求
277
- * @param {string} url 请求地址
278
- * @param {RequestConfig} [options] 请求选项
279
- * @returns {Promise<AxiosResponse>} 响应数据
280
- */
281
- get(url, options = {}) {
282
- return this.request("GET", url, options);
283
- }
284
- /**
285
- * POST请求
286
- * @param {string} url 请求地址
287
- * @param {any} [data] 请求数据
288
- * @param {RequestConfig} [options] 其他选项
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
- export { AxiosWrapperPro };
325
- //# sourceMappingURL=index.js.map
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
+ });