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.
@@ -0,0 +1,209 @@
1
+ import * as axios from 'axios';
2
+ import { InternalAxiosRequestConfig, AxiosError, AxiosResponse, AxiosRequestConfig, AxiosInstance } from 'axios';
3
+
4
+ /**
5
+ * 扩展 Axios 类型,添加自定义字段
6
+ */
7
+ declare module "axios" {
8
+ interface InternalAxiosRequestConfig {
9
+ /** 内部使用的请求键(用于在响应拦截器中清理 AbortController) */
10
+ _requestKey?: RequestKey;
11
+ }
12
+ }
13
+ /** 请求键类型(字符串或符号) */
14
+ type RequestKey = string | symbol;
15
+ /**
16
+ * 重试配置接口(完整配置)
17
+ */
18
+ interface RetryConfig {
19
+ /** 重试次数,默认 0 */
20
+ count: number;
21
+ /** 重试延迟(毫秒),默认 1000 */
22
+ delay: number;
23
+ /** 重试条件判断函数 */
24
+ condition: (error: AxiosError) => boolean;
25
+ }
26
+ /**
27
+ * 拦截器配置接口
28
+ */
29
+ interface InterceptorConfig {
30
+ /** 请求拦截器 */
31
+ request: (config: InternalAxiosRequestConfig) => InternalAxiosRequestConfig | Promise<InternalAxiosRequestConfig>;
32
+ /** 请求错误拦截器 */
33
+ requestError: (error: AxiosError) => Promise<any>;
34
+ /** 响应拦截器 */
35
+ response: (response: AxiosResponse) => AxiosResponse | Promise<AxiosResponse>;
36
+ /** 响应错误拦截器 */
37
+ responseError: (error: AxiosError) => Promise<any>;
38
+ }
39
+ /**
40
+ * 请求配置扩展接口
41
+ */
42
+ interface RequestConfig extends AxiosRequestConfig {
43
+ /** 自定义请求标识(用于去重和取消请求) */
44
+ requestKey?: RequestKey;
45
+ /** 单次请求的重试配置 */
46
+ retry?: Partial<RetryConfig>;
47
+ }
48
+ /**
49
+ * AxiosWrapperPro 构造函数配置选项
50
+ */
51
+ interface AxiosWrapperProOptions {
52
+ /** 基础 URL */
53
+ baseURL?: string;
54
+ /** 超时时间(毫秒) */
55
+ timeout?: number;
56
+ /** 默认请求头 */
57
+ headers?: Record<string, string>;
58
+ /** 参数序列化函数 */
59
+ paramsSerializer?: (params: any) => string;
60
+ /** 拦截器配置 */
61
+ interceptors?: Partial<InterceptorConfig>;
62
+ /** 重试配置 */
63
+ retry?: Partial<RetryConfig>;
64
+ }
65
+
66
+ declare class AxiosWrapperPro {
67
+ /** 中断控制器 */
68
+ private _abortControllers;
69
+ /** 请求拦截器ID */
70
+ private _requestInterceptorId;
71
+ /** 响应拦截器ID */
72
+ private _responseInterceptorId;
73
+ /** 拦截器配置 */
74
+ private _interceptorConfig;
75
+ /** 重试配置(完整配置) */
76
+ private _retryConfig;
77
+ /** 参数序列化函数 */
78
+ private _paramsSerializer;
79
+ /** Axios实例 */
80
+ private _instance;
81
+ constructor(options?: AxiosWrapperProOptions);
82
+ /**
83
+ * 获取拦截器配置,未配置的使用默认值
84
+ * @returns {InterceptorConfig} 完整的拦截器配置
85
+ */
86
+ _getInterceptors(): InterceptorConfig;
87
+ /**
88
+ * 初始化请求拦截器
89
+ */
90
+ _initRequestInterceptors(): void;
91
+ /**
92
+ * 初始化响应拦截器
93
+ */
94
+ _initResponseInterceptors(): void;
95
+ /**
96
+ * 设置请求拦截器,移除旧拦截器,更新拦截器配置,重新初始化拦截器
97
+ * @param {InterceptorConfig["request"]} interceptor 拦截器
98
+ * @param {InterceptorConfig["requestError"]} errorInterceptor 错误拦截器
99
+ */
100
+ setRequestInterceptor(interceptor: InterceptorConfig["request"], errorInterceptor: InterceptorConfig["requestError"]): void;
101
+ /**
102
+ * 设置响应拦截器,移除旧拦截器,更新拦截器配置,重新初始化拦截器
103
+ * @param {InterceptorConfig["response"]} interceptor 拦截器
104
+ * @param {InterceptorConfig["responseError"]} errorInterceptor 错误拦截器
105
+ */
106
+ setResponseInterceptor(interceptor: InterceptorConfig["response"], errorInterceptor: InterceptorConfig["responseError"]): void;
107
+ /**
108
+ * 设置取消控制器
109
+ * @param {AxiosRequestConfig} config 请求配置
110
+ * @param {RequestKey} customKey 自定义key
111
+ * @returns {InternalAxiosRequestConfig} 处理后的配置
112
+ */
113
+ _setupAbortController(config: AxiosRequestConfig, customKey: RequestKey): InternalAxiosRequestConfig;
114
+ /**
115
+ * 生成请求唯一标识
116
+ * @param {AxiosRequestConfig} config 请求配置
117
+ * @returns {string} 请求唯一标识
118
+ */
119
+ _generateRequestKey(config: AxiosRequestConfig): string;
120
+ /**
121
+ * 稳定的JSON序列化,确保相同内容生成相同字符串
122
+ * @param {any} obj 待序列化对象
123
+ * @returns {string} 序列化后的字符串
124
+ */
125
+ _stableStringify(obj: any): string;
126
+ /**
127
+ * 取消指定请求
128
+ * @param {RequestKey} key 请求标识
129
+ * @param {string} [reason] 取消原因
130
+ * @returns {boolean} 是否成功取消
131
+ */
132
+ cancelRequest(key: RequestKey, reason?: string): boolean;
133
+ /**
134
+ * 取消所有请求
135
+ * @param {string} [reason] 取消原因
136
+ * @returns {number} 被取消的请求数量
137
+ */
138
+ cancelAllRequests(reason?: string): number;
139
+ /**
140
+ * 设置默认请求头
141
+ * @param {string} key 请求头名称
142
+ * @param {string} value 请求头值
143
+ */
144
+ setHeader(key: string, value: string): void;
145
+ /**
146
+ * 批量设置请求头
147
+ * @param {Record<string, string>} headers 请求头对象
148
+ */
149
+ setHeaders(headers: Record<string, string>): void;
150
+ /**
151
+ * 移除请求头
152
+ * @param {string} key 请求头名称
153
+ */
154
+ removeHeader(key: string): void;
155
+ /** 获取基础URL */
156
+ getBaseURL(): string;
157
+ /** 设置基础URL */
158
+ setBaseURL(url: string): void;
159
+ /** 获取 Axios 实例 */
160
+ getInstance(): AxiosInstance;
161
+ /**
162
+ * 通用请求方法
163
+ * @param {string} method 请求方法
164
+ * @param {string} url 请求地址
165
+ * @param {RequestConfig} [config={}] 请求配置
166
+ * @returns {Promise<AxiosResponse>} 响应数据
167
+ */
168
+ request(method: string, url: string, config?: RequestConfig): Promise<axios.AxiosResponse<any, any, {}>>;
169
+ /**
170
+ * GET请求
171
+ * @param {string} url 请求地址
172
+ * @param {RequestConfig} [options] 请求选项
173
+ * @returns {Promise<AxiosResponse>} 响应数据
174
+ */
175
+ get(url: string, options?: RequestConfig): Promise<axios.AxiosResponse<any, any, {}>>;
176
+ /**
177
+ * POST请求
178
+ * @param {string} url 请求地址
179
+ * @param {any} [data] 请求数据
180
+ * @param {RequestConfig} [options] 其他选项
181
+ * @returns {Promise<AxiosResponse>} 响应数据
182
+ */
183
+ post(url: string, data?: any, options?: RequestConfig): Promise<axios.AxiosResponse<any, any, {}>>;
184
+ /**
185
+ * PUT请求
186
+ * @param {string} url 请求地址
187
+ * @param {any} [data] 请求数据
188
+ * @param {RequestConfig} [options] 其他选项
189
+ * @returns {Promise<AxiosResponse>} 响应数据
190
+ */
191
+ put(url: string, data?: any, options?: RequestConfig): Promise<axios.AxiosResponse<any, any, {}>>;
192
+ /**
193
+ * DELETE请求
194
+ * @param {string} url 请求地址
195
+ * @param {RequestConfig} [options] 请求选项
196
+ * @returns {Promise<AxiosResponse>} 响应数据
197
+ */
198
+ delete(url: string, options?: RequestConfig): Promise<axios.AxiosResponse<any, any, {}>>;
199
+ /**
200
+ * PATCH请求
201
+ * @param {string} url 请求地址
202
+ * @param {any} [data] 请求数据
203
+ * @param {RequestConfig} [options] 其他选项
204
+ * @returns {Promise<AxiosResponse>} 响应数据
205
+ */
206
+ patch(url: string, data?: any, options?: RequestConfig): Promise<axios.AxiosResponse<any, any, {}>>;
207
+ }
208
+
209
+ export { AxiosWrapperPro, type AxiosWrapperProOptions, type InterceptorConfig, type RequestConfig, type RequestKey, type RetryConfig };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,68 @@
1
- import { AxiosInstance, AxiosRequestConfig, InternalAxiosRequestConfig } from "axios";
2
- import type { AxiosWrapperProOptions, InterceptorConfig, RequestConfig, RequestKey } from "./types";
1
+ import * as axios from 'axios';
2
+ import { InternalAxiosRequestConfig, AxiosError, AxiosResponse, AxiosRequestConfig, AxiosInstance } from 'axios';
3
+
4
+ /**
5
+ * 扩展 Axios 类型,添加自定义字段
6
+ */
7
+ declare module "axios" {
8
+ interface InternalAxiosRequestConfig {
9
+ /** 内部使用的请求键(用于在响应拦截器中清理 AbortController) */
10
+ _requestKey?: RequestKey;
11
+ }
12
+ }
13
+ /** 请求键类型(字符串或符号) */
14
+ type RequestKey = string | symbol;
15
+ /**
16
+ * 重试配置接口(完整配置)
17
+ */
18
+ interface RetryConfig {
19
+ /** 重试次数,默认 0 */
20
+ count: number;
21
+ /** 重试延迟(毫秒),默认 1000 */
22
+ delay: number;
23
+ /** 重试条件判断函数 */
24
+ condition: (error: AxiosError) => boolean;
25
+ }
26
+ /**
27
+ * 拦截器配置接口
28
+ */
29
+ interface InterceptorConfig {
30
+ /** 请求拦截器 */
31
+ request: (config: InternalAxiosRequestConfig) => InternalAxiosRequestConfig | Promise<InternalAxiosRequestConfig>;
32
+ /** 请求错误拦截器 */
33
+ requestError: (error: AxiosError) => Promise<any>;
34
+ /** 响应拦截器 */
35
+ response: (response: AxiosResponse) => AxiosResponse | Promise<AxiosResponse>;
36
+ /** 响应错误拦截器 */
37
+ responseError: (error: AxiosError) => Promise<any>;
38
+ }
39
+ /**
40
+ * 请求配置扩展接口
41
+ */
42
+ interface RequestConfig extends AxiosRequestConfig {
43
+ /** 自定义请求标识(用于去重和取消请求) */
44
+ requestKey?: RequestKey;
45
+ /** 单次请求的重试配置 */
46
+ retry?: Partial<RetryConfig>;
47
+ }
48
+ /**
49
+ * AxiosWrapperPro 构造函数配置选项
50
+ */
51
+ interface AxiosWrapperProOptions {
52
+ /** 基础 URL */
53
+ baseURL?: string;
54
+ /** 超时时间(毫秒) */
55
+ timeout?: number;
56
+ /** 默认请求头 */
57
+ headers?: Record<string, string>;
58
+ /** 参数序列化函数 */
59
+ paramsSerializer?: (params: any) => string;
60
+ /** 拦截器配置 */
61
+ interceptors?: Partial<InterceptorConfig>;
62
+ /** 重试配置 */
63
+ retry?: Partial<RetryConfig>;
64
+ }
65
+
3
66
  declare class AxiosWrapperPro {
4
67
  /** 中断控制器 */
5
68
  private _abortControllers;
@@ -102,14 +165,14 @@ declare class AxiosWrapperPro {
102
165
  * @param {RequestConfig} [config={}] 请求配置
103
166
  * @returns {Promise<AxiosResponse>} 响应数据
104
167
  */
105
- request(method: string, url: string, config?: RequestConfig): Promise<import("axios").AxiosResponse<any, any, {}>>;
168
+ request(method: string, url: string, config?: RequestConfig): Promise<axios.AxiosResponse<any, any, {}>>;
106
169
  /**
107
170
  * GET请求
108
171
  * @param {string} url 请求地址
109
172
  * @param {RequestConfig} [options] 请求选项
110
173
  * @returns {Promise<AxiosResponse>} 响应数据
111
174
  */
112
- get(url: string, options?: RequestConfig): Promise<import("axios").AxiosResponse<any, any, {}>>;
175
+ get(url: string, options?: RequestConfig): Promise<axios.AxiosResponse<any, any, {}>>;
113
176
  /**
114
177
  * POST请求
115
178
  * @param {string} url 请求地址
@@ -117,7 +180,7 @@ declare class AxiosWrapperPro {
117
180
  * @param {RequestConfig} [options] 其他选项
118
181
  * @returns {Promise<AxiosResponse>} 响应数据
119
182
  */
120
- post(url: string, data?: any, options?: RequestConfig): Promise<import("axios").AxiosResponse<any, any, {}>>;
183
+ post(url: string, data?: any, options?: RequestConfig): Promise<axios.AxiosResponse<any, any, {}>>;
121
184
  /**
122
185
  * PUT请求
123
186
  * @param {string} url 请求地址
@@ -125,14 +188,14 @@ declare class AxiosWrapperPro {
125
188
  * @param {RequestConfig} [options] 其他选项
126
189
  * @returns {Promise<AxiosResponse>} 响应数据
127
190
  */
128
- put(url: string, data?: any, options?: RequestConfig): Promise<import("axios").AxiosResponse<any, any, {}>>;
191
+ put(url: string, data?: any, options?: RequestConfig): Promise<axios.AxiosResponse<any, any, {}>>;
129
192
  /**
130
193
  * DELETE请求
131
194
  * @param {string} url 请求地址
132
195
  * @param {RequestConfig} [options] 请求选项
133
196
  * @returns {Promise<AxiosResponse>} 响应数据
134
197
  */
135
- delete(url: string, options?: RequestConfig): Promise<import("axios").AxiosResponse<any, any, {}>>;
198
+ delete(url: string, options?: RequestConfig): Promise<axios.AxiosResponse<any, any, {}>>;
136
199
  /**
137
200
  * PATCH请求
138
201
  * @param {string} url 请求地址
@@ -140,8 +203,7 @@ declare class AxiosWrapperPro {
140
203
  * @param {RequestConfig} [options] 其他选项
141
204
  * @returns {Promise<AxiosResponse>} 响应数据
142
205
  */
143
- patch(url: string, data?: any, options?: RequestConfig): Promise<import("axios").AxiosResponse<any, any, {}>>;
206
+ patch(url: string, data?: any, options?: RequestConfig): Promise<axios.AxiosResponse<any, any, {}>>;
144
207
  }
145
- export { AxiosWrapperPro };
146
- export type { AxiosWrapperProOptions, RetryConfig, InterceptorConfig, RequestConfig, RequestKey } from "./types";
147
- //# sourceMappingURL=index.d.ts.map
208
+
209
+ export { AxiosWrapperPro, type AxiosWrapperProOptions, type InterceptorConfig, type RequestConfig, type RequestKey, type RetryConfig };