@varlet/axle 0.2.8 → 0.2.10
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 +74 -0
- package/README.zh-CN.md +74 -0
- package/es/index.d.ts +4 -1
- package/es/index.js +7 -10
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -197,6 +197,80 @@ axle.setHeader('TOKEN', TOKEN)
|
|
|
197
197
|
axle.removeHeader('TOKEN')
|
|
198
198
|
```
|
|
199
199
|
|
|
200
|
+
## Built-in interceptor
|
|
201
|
+
|
|
202
|
+
axle provides some practical requests/response interceptors, and is compatible with `axle` and `axios`.
|
|
203
|
+
|
|
204
|
+
### axios
|
|
205
|
+
|
|
206
|
+
```ts
|
|
207
|
+
import { requestHeadersInterceptor, responseTimeoutInterceptor } from '@varlet/axle'
|
|
208
|
+
|
|
209
|
+
const headersInterceptor = requestHeadersInterceptor({
|
|
210
|
+
headers: () => ({
|
|
211
|
+
token: localStorage.getItem('token'),
|
|
212
|
+
'Axle-Custom-Header': 'Axle-Custom-Header',
|
|
213
|
+
})
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
const retryInterceptor = responseRetryInterceptor({ count: 3 })
|
|
217
|
+
|
|
218
|
+
axios.interceptors.request.use(
|
|
219
|
+
headersInterceptor.onFulfilled,
|
|
220
|
+
headersInterceptor.onRejected,
|
|
221
|
+
headersInterceptor.options
|
|
222
|
+
)
|
|
223
|
+
axios.interceptors.response.use(
|
|
224
|
+
retryInterceptor.onFulfilled,
|
|
225
|
+
retryInterceptor.onRejected,
|
|
226
|
+
retryInterceptor.options
|
|
227
|
+
)
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
### axle
|
|
231
|
+
|
|
232
|
+
```ts
|
|
233
|
+
import { requestHeadersInterceptor, responseTimeoutInterceptor } from '@varlet/axle'
|
|
234
|
+
|
|
235
|
+
axle.useRequestInterceptor(
|
|
236
|
+
requestHeadersInterceptor({
|
|
237
|
+
headers: () => ({
|
|
238
|
+
token: localStorage.getItem('token'),
|
|
239
|
+
'Axle-Custom-Header': 'Axle-Custom-Header',
|
|
240
|
+
}),
|
|
241
|
+
}),
|
|
242
|
+
)
|
|
243
|
+
|
|
244
|
+
axle.useResponseInterceptor(responseRetryInterceptor({ count: 3 }))
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
### General parameter of the interceptor
|
|
248
|
+
|
|
249
|
+
Each built-in interceptor supports `include` `exclude` `axiosInterceptorOptions (same with Axios interceptor)`.
|
|
250
|
+
|
|
251
|
+
#### include & exclude
|
|
252
|
+
|
|
253
|
+
It is used to request filtering to determine what request should apply the interceptor and support specifying the `method` or `glob` syntax. The usage method is as follows.
|
|
254
|
+
|
|
255
|
+
```ts
|
|
256
|
+
axle.useResponseInterceptor(
|
|
257
|
+
responseRetryInterceptor({
|
|
258
|
+
count: 3,
|
|
259
|
+
include: ['method:put', 'method:post'],
|
|
260
|
+
exclude: ['/system/**', '/user/addUser']
|
|
261
|
+
}),
|
|
262
|
+
)
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
### List of built-in interceptor
|
|
266
|
+
|
|
267
|
+
| Name | Description |
|
|
268
|
+
| --- | --- |
|
|
269
|
+
| [requestHeadersInterceptor](https://github.com/varletjs/axle/blob/main/src/interceptors/requestHeadersInterceptor.ts) | Used to customize the request header |
|
|
270
|
+
| [responseRetryInterceptor](https://github.com/varletjs/axle/blob/main/src/interceptors/responseRetryInterceptor.ts) | Used to realize the request abnormal retry |
|
|
271
|
+
| [responseBlobInterceptor](https://github.com/varletjs/axle/blob/main/src/interceptors/responseBlobInterceptor.ts) | Used to intercept blob type |
|
|
272
|
+
| [responseTimeoutInterceptor](https://github.com/varletjs/axle/blob/main/src/interceptors/responseTimeoutInterceptor.ts) | Used to abnormal timeout |
|
|
273
|
+
|
|
200
274
|
## Vue Composition API
|
|
201
275
|
|
|
202
276
|
Axle provides the usage of Vue Composition API style, which encapsulates the `loading status`, `error status`, `upload and download progress` of the request, `return data`, `lifecycle`, etc., And inherit all the configuration of `axios`.
|
package/README.zh-CN.md
CHANGED
|
@@ -197,6 +197,80 @@ axle.setHeader('TOKEN', TOKEN)
|
|
|
197
197
|
axle.removeHeader('TOKEN')
|
|
198
198
|
```
|
|
199
199
|
|
|
200
|
+
## 内置拦截器
|
|
201
|
+
|
|
202
|
+
Axle 提供了一些实用的请求/响应拦截器,并且兼容 axle 和 axios。
|
|
203
|
+
|
|
204
|
+
### axios
|
|
205
|
+
|
|
206
|
+
```ts
|
|
207
|
+
import { requestHeadersInterceptor, responseTimeoutInterceptor } from '@varlet/axle'
|
|
208
|
+
|
|
209
|
+
const headersInterceptor = requestHeadersInterceptor({
|
|
210
|
+
headers: () => ({
|
|
211
|
+
token: localStorage.getItem('token'),
|
|
212
|
+
'Axle-Custom-Header': 'Axle-Custom-Header',
|
|
213
|
+
})
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
const retryInterceptor = responseRetryInterceptor({ count: 3 })
|
|
217
|
+
|
|
218
|
+
axios.interceptors.request.use(
|
|
219
|
+
headersInterceptor.onFulfilled,
|
|
220
|
+
headersInterceptor.onRejected,
|
|
221
|
+
headersInterceptor.options
|
|
222
|
+
)
|
|
223
|
+
axios.interceptors.response.use(
|
|
224
|
+
retryInterceptor.onFulfilled,
|
|
225
|
+
retryInterceptor.onRejected,
|
|
226
|
+
retryInterceptor.options
|
|
227
|
+
)
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
### axle
|
|
231
|
+
|
|
232
|
+
```ts
|
|
233
|
+
import { requestHeadersInterceptor, responseTimeoutInterceptor } from '@varlet/axle'
|
|
234
|
+
|
|
235
|
+
axle.useRequestInterceptor(
|
|
236
|
+
requestHeadersInterceptor({
|
|
237
|
+
headers: () => ({
|
|
238
|
+
token: localStorage.getItem('token'),
|
|
239
|
+
'Axle-Custom-Header': 'Axle-Custom-Header',
|
|
240
|
+
}),
|
|
241
|
+
}),
|
|
242
|
+
)
|
|
243
|
+
|
|
244
|
+
axle.useResponseInterceptor(responseRetryInterceptor({ count: 3 }))
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
### 拦截器通用参数
|
|
248
|
+
|
|
249
|
+
每个内置拦截器都支持 `include` `exclude` `axiosInterceptorOptions (与 axios 拦截器一致)`。
|
|
250
|
+
|
|
251
|
+
#### include & exclude
|
|
252
|
+
|
|
253
|
+
用于请求过滤,以确定什么请求应该应用该拦截器,支持指定 method 或是 glob 语法,使用方式如下。
|
|
254
|
+
|
|
255
|
+
```ts
|
|
256
|
+
axle.useResponseInterceptor(
|
|
257
|
+
responseRetryInterceptor({
|
|
258
|
+
count: 3,
|
|
259
|
+
include: ['method:put', 'method:post'],
|
|
260
|
+
exclude: ['/system/**', '/user/addUser']
|
|
261
|
+
}),
|
|
262
|
+
)
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
### 内置拦截器一览
|
|
266
|
+
|
|
267
|
+
| 名称 | 描述 |
|
|
268
|
+
| --- | --- |
|
|
269
|
+
| [requestHeadersInterceptor](https://github.com/varletjs/axle/blob/main/src/interceptors/requestHeadersInterceptor.ts) | 用于自定义请求头 |
|
|
270
|
+
| [responseRetryInterceptor](https://github.com/varletjs/axle/blob/main/src/interceptors/responseRetryInterceptor.ts) | 用于实现请求异常重试 |
|
|
271
|
+
| [responseBlobInterceptor](https://github.com/varletjs/axle/blob/main/src/interceptors/responseBlobInterceptor.ts) | 用于拦截 blob 类型 |
|
|
272
|
+
| [responseTimeoutInterceptor](https://github.com/varletjs/axle/blob/main/src/interceptors/responseTimeoutInterceptor.ts) | 用于归一化超时异常 |
|
|
273
|
+
|
|
200
274
|
## Vue 组合式 API
|
|
201
275
|
|
|
202
276
|
Axle 提供了 Vue Composition API 风格的用法,封装了请求的 `加载状态`, `错误状态`, `请求的上下行进度`,`返回数据`,`生命周期` 等等,并继承了 `axios` 的所有配置。
|
package/es/index.d.ts
CHANGED
|
@@ -2,6 +2,9 @@ import { R as RequestInterceptor, a as ResponseInterceptor } from './instance-8e
|
|
|
2
2
|
export { e as AxleInstance, A as AxleRequestConfig, b as FetchMethod, F as FetchRunner, I as Interceptor, c as ModifyMethod, M as ModifyRunner, d as RunnerMethod, i as createAxle, f as createFetchRunner, g as createModifyRunner, h as download } from './instance-8e68a761.js';
|
|
3
3
|
import { AxiosInterceptorOptions, AxiosResponse } from 'axios';
|
|
4
4
|
|
|
5
|
+
declare function matchPattern(pattern: string, method: string, url: string): boolean;
|
|
6
|
+
declare function createMatcher(include?: string[], exclude?: string[]): (method: string, url: string) => boolean;
|
|
7
|
+
|
|
5
8
|
interface RequestHeadersInterceptorOptions {
|
|
6
9
|
headers?: Record<string, string> | (() => Record<string, string>);
|
|
7
10
|
include?: string[];
|
|
@@ -34,4 +37,4 @@ interface ResponseRetryInterceptorOptions {
|
|
|
34
37
|
}
|
|
35
38
|
declare function responseRetryInterceptor(options: ResponseRetryInterceptorOptions): ResponseInterceptor;
|
|
36
39
|
|
|
37
|
-
export { RequestHeadersInterceptorOptions, RequestInterceptor, ResponseBlobInterceptorOptions, ResponseInterceptor, ResponseRetryInterceptorOptions, ResponseTimeoutInterceptorOptions, requestHeadersInterceptor, responseBlobInterceptor, responseRetryInterceptor, responseTimeoutInterceptor };
|
|
40
|
+
export { RequestHeadersInterceptorOptions, RequestInterceptor, ResponseBlobInterceptorOptions, ResponseInterceptor, ResponseRetryInterceptorOptions, ResponseTimeoutInterceptorOptions, createMatcher, matchPattern, requestHeadersInterceptor, responseBlobInterceptor, responseRetryInterceptor, responseTimeoutInterceptor };
|
package/es/index.js
CHANGED
|
@@ -113,9 +113,6 @@ function createAxle(config = {}) {
|
|
|
113
113
|
};
|
|
114
114
|
}
|
|
115
115
|
|
|
116
|
-
// src/interceptors/requestHeadersInterceptor.ts
|
|
117
|
-
import { isFunction } from "@varlet/shared";
|
|
118
|
-
|
|
119
116
|
// src/matcher.ts
|
|
120
117
|
import { minimatch } from "minimatch";
|
|
121
118
|
function matchPattern(pattern, method, url) {
|
|
@@ -140,6 +137,7 @@ function createMatcher(include, exclude) {
|
|
|
140
137
|
}
|
|
141
138
|
|
|
142
139
|
// src/interceptors/requestHeadersInterceptor.ts
|
|
140
|
+
import { isFunction } from "@varlet/shared";
|
|
143
141
|
function requestHeadersInterceptor(options = {}) {
|
|
144
142
|
const { headers: headersOrGetter } = options;
|
|
145
143
|
return {
|
|
@@ -206,15 +204,12 @@ function responseRetryInterceptor(options) {
|
|
|
206
204
|
async onRejected(error) {
|
|
207
205
|
var _a, _b;
|
|
208
206
|
const matcher = createMatcher(options.include, options.exclude);
|
|
209
|
-
if (!matcher((_a = error.config.method) != null ? _a : "", (_b = error.config.url) != null ? _b : "")) {
|
|
207
|
+
if (!matcher((_a = error.config.method) != null ? _a : "", (_b = error.config.url) != null ? _b : "") || isCancel(error)) {
|
|
210
208
|
return Promise.reject(error);
|
|
211
209
|
}
|
|
212
210
|
const { count = 1 } = options;
|
|
213
211
|
let retryCount = 0;
|
|
214
|
-
|
|
215
|
-
return Promise.reject(error);
|
|
216
|
-
}
|
|
217
|
-
async function loop() {
|
|
212
|
+
async function retry() {
|
|
218
213
|
try {
|
|
219
214
|
retryCount++;
|
|
220
215
|
return await axios2.create()(error.config);
|
|
@@ -222,10 +217,10 @@ function responseRetryInterceptor(options) {
|
|
|
222
217
|
if (retryCount === count) {
|
|
223
218
|
return Promise.reject(error2);
|
|
224
219
|
}
|
|
225
|
-
return
|
|
220
|
+
return retry();
|
|
226
221
|
}
|
|
227
222
|
}
|
|
228
|
-
return
|
|
223
|
+
return retry();
|
|
229
224
|
},
|
|
230
225
|
options: options.axiosInterceptorOptions
|
|
231
226
|
};
|
|
@@ -233,8 +228,10 @@ function responseRetryInterceptor(options) {
|
|
|
233
228
|
export {
|
|
234
229
|
createAxle,
|
|
235
230
|
createFetchRunner,
|
|
231
|
+
createMatcher,
|
|
236
232
|
createModifyRunner,
|
|
237
233
|
download,
|
|
234
|
+
matchPattern,
|
|
238
235
|
requestHeadersInterceptor,
|
|
239
236
|
responseBlobInterceptor,
|
|
240
237
|
responseRetryInterceptor,
|