@varlet/axle 0.2.8 → 0.2.9

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 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 {
@@ -233,8 +231,10 @@ function responseRetryInterceptor(options) {
233
231
  export {
234
232
  createAxle,
235
233
  createFetchRunner,
234
+ createMatcher,
236
235
  createModifyRunner,
237
236
  download,
237
+ matchPattern,
238
238
  requestHeadersInterceptor,
239
239
  responseBlobInterceptor,
240
240
  responseRetryInterceptor,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@varlet/axle",
3
- "version": "0.2.8",
3
+ "version": "0.2.9",
4
4
  "description": "Progressive request tool based on axios",
5
5
  "type": "module",
6
6
  "main": "es/index.js",