axios-wrapper-pro 1.0.0 → 1.0.2
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/README.md +28 -23
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -15,7 +15,11 @@
|
|
|
15
15
|
## 安装
|
|
16
16
|
|
|
17
17
|
```bash
|
|
18
|
+
# npm安装
|
|
18
19
|
npm install axios-wrapper-pro
|
|
20
|
+
|
|
21
|
+
# yarn安装
|
|
22
|
+
yarn add axios-wrapper-pro
|
|
19
23
|
```
|
|
20
24
|
|
|
21
25
|
## 使用方法
|
|
@@ -23,21 +27,22 @@ npm install axios-wrapper-pro
|
|
|
23
27
|
### 基本使用
|
|
24
28
|
|
|
25
29
|
```typescript
|
|
26
|
-
import { AxiosWrapperPro } from
|
|
30
|
+
import { AxiosWrapperPro } from "axios-wrapper-pro";
|
|
27
31
|
|
|
28
32
|
const axiosWrapperPro = new AxiosWrapperPro({
|
|
29
|
-
baseURL:
|
|
33
|
+
baseURL: "https://api.example.com",
|
|
30
34
|
timeout: 10000,
|
|
31
35
|
headers: {
|
|
32
|
-
|
|
33
|
-
|
|
36
|
+
"Content-Type": "application/json",
|
|
37
|
+
colorTag: "dev",
|
|
38
|
+
},
|
|
34
39
|
});
|
|
35
40
|
|
|
36
41
|
// GET 请求
|
|
37
|
-
const response = await axiosWrapperPro.get(
|
|
42
|
+
const response = await axiosWrapperPro.get("/users");
|
|
38
43
|
|
|
39
44
|
// POST 请求
|
|
40
|
-
const response = await axiosWrapperPro.post(
|
|
45
|
+
const response = await axiosWrapperPro.post("/users", { name: "John" });
|
|
41
46
|
```
|
|
42
47
|
|
|
43
48
|
### 配置重试
|
|
@@ -52,16 +57,16 @@ const axiosWrapperPro = new AxiosWrapperPro({
|
|
|
52
57
|
// 网络错误或 5xx 错误时重试
|
|
53
58
|
if (!error?.response) return true;
|
|
54
59
|
return error?.response?.status >= 500;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
60
|
+
},
|
|
61
|
+
},
|
|
57
62
|
});
|
|
58
63
|
|
|
59
64
|
// 单次请求重试配置
|
|
60
|
-
await axiosWrapperPro.get(
|
|
65
|
+
await axiosWrapperPro.get("/users", {
|
|
61
66
|
retry: {
|
|
62
67
|
count: 2,
|
|
63
|
-
delay: 500
|
|
64
|
-
}
|
|
68
|
+
delay: 500,
|
|
69
|
+
},
|
|
65
70
|
});
|
|
66
71
|
```
|
|
67
72
|
|
|
@@ -69,20 +74,20 @@ await axiosWrapperPro.get('/users', {
|
|
|
69
74
|
|
|
70
75
|
```typescript
|
|
71
76
|
// 使用 requestKey 进行请求去重
|
|
72
|
-
const response1 = await axiosWrapperPro.get(
|
|
73
|
-
requestKey:
|
|
77
|
+
const response1 = await axiosWrapperPro.get("/users", {
|
|
78
|
+
requestKey: "get-users",
|
|
74
79
|
});
|
|
75
80
|
// 相同的 requestKey 会取消之前的请求
|
|
76
|
-
const response2 = await axiosWrapperPro.get(
|
|
77
|
-
requestKey:
|
|
81
|
+
const response2 = await axiosWrapperPro.get("/users", {
|
|
82
|
+
requestKey: "get-users",
|
|
78
83
|
});
|
|
79
84
|
|
|
80
85
|
// 使用 Symbol 作为 requestKey
|
|
81
|
-
const myKey = Symbol(
|
|
82
|
-
await axiosWrapperPro.get(
|
|
86
|
+
const myKey = Symbol("my-request");
|
|
87
|
+
await axiosWrapperPro.get("/data", { requestKey: myKey });
|
|
83
88
|
|
|
84
89
|
// 取消指定请求
|
|
85
|
-
axiosWrapperPro.cancelRequest(
|
|
90
|
+
axiosWrapperPro.cancelRequest("get-users");
|
|
86
91
|
// 取消所有请求
|
|
87
92
|
axiosWrapperPro.cancelAllRequests();
|
|
88
93
|
```
|
|
@@ -119,16 +124,16 @@ axiosWrapperPro.setResponseInterceptor(
|
|
|
119
124
|
|
|
120
125
|
```typescript
|
|
121
126
|
// 设置单个请求头
|
|
122
|
-
axiosWrapperPro.setHeader(
|
|
127
|
+
axiosWrapperPro.setHeader("Authorization", "Bearer token");
|
|
123
128
|
|
|
124
129
|
// 批量设置请求头
|
|
125
130
|
axiosWrapperPro.setHeaders({
|
|
126
|
-
|
|
127
|
-
|
|
131
|
+
"X-Custom-Header": "value",
|
|
132
|
+
"X-Another-Header": "value",
|
|
128
133
|
});
|
|
129
134
|
|
|
130
135
|
// 移除请求头
|
|
131
|
-
axiosWrapperPro.removeHeader(
|
|
136
|
+
axiosWrapperPro.removeHeader("X-Custom-Header");
|
|
132
137
|
```
|
|
133
138
|
|
|
134
139
|
## API
|
|
@@ -250,4 +255,4 @@ type RequestKey = string | symbol;
|
|
|
250
255
|
|
|
251
256
|
## 许可证
|
|
252
257
|
|
|
253
|
-
MIT
|
|
258
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "axios-wrapper-pro",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "基于 Axios 的增强型 HTTP 客户端,支持请求去重、自动取消、重试机制和可配置拦截器",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"build": "tsc",
|
|
@@ -42,4 +42,4 @@
|
|
|
42
42
|
"webpack": "^5.90.0",
|
|
43
43
|
"webpack-cli": "^5.1.0"
|
|
44
44
|
}
|
|
45
|
-
}
|
|
45
|
+
}
|