@varlet/axle 0.3.0 → 0.3.1

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
@@ -250,14 +250,14 @@ Each built-in interceptor supports `include` `exclude` `axiosInterceptorOptions
250
250
 
251
251
  #### include & exclude
252
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.
253
+ It is used to request filtering to determine what request should apply the interceptor and support specifying the `method`、 `glob` syntax or `status` code. The usage method is as follows.
254
254
 
255
255
  ```ts
256
256
  axle.useResponseInterceptor(
257
257
  responseRetryInterceptor({
258
258
  count: 3,
259
- include: ['method:put', 'method:post'],
260
- exclude: ['/system/**', '/user/addUser']
259
+ include: ['method:put', 'method:post', 'status:500'],
260
+ exclude: ['/system/**', '/user/addUser', 'status:400']
261
261
  }),
262
262
  )
263
263
  ```
package/README.zh-CN.md CHANGED
@@ -250,14 +250,14 @@ axle.useResponseInterceptor(responseRetryInterceptor({ count: 3 }))
250
250
 
251
251
  #### include & exclude
252
252
 
253
- 用于请求过滤,以确定什么请求应该应用该拦截器,支持指定 method 或是 glob 语法,使用方式如下。
253
+ 用于请求过滤,以确定什么请求应该应用该拦截器,支持指定 `method`、 `glob` 或者 `status`,使用方式如下。
254
254
 
255
255
  ```ts
256
256
  axle.useResponseInterceptor(
257
257
  responseRetryInterceptor({
258
258
  count: 3,
259
- include: ['method:put', 'method:post'],
260
- exclude: ['/system/**', '/user/addUser']
259
+ include: ['method:put', 'method:post', 'status:500'],
260
+ exclude: ['/system/**', '/user/addUser', 'status:400']
261
261
  }),
262
262
  )
263
263
  ```
package/es/index.d.ts CHANGED
@@ -2,8 +2,8 @@ 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, AxiosRequestConfig, 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;
5
+ declare function matchPattern(pattern: string, method: string, url: string, status?: number | null | undefined): boolean;
6
+ declare function createMatcher(include?: string[], exclude?: string[]): (method: string, url: string, status?: number | null | undefined) => boolean;
7
7
 
8
8
  interface RequestHeadersInterceptorOptions {
9
9
  headers?: Record<string, string> | (() => Record<string, string>);
package/es/index.js CHANGED
@@ -133,22 +133,25 @@ function createAxle(config = {}) {
133
133
 
134
134
  // src/matcher.ts
135
135
  import { minimatch } from "minimatch";
136
- function matchPattern(pattern, method, url) {
136
+ function matchPattern(pattern, method, url, status) {
137
+ if (pattern.startsWith("status:")) {
138
+ return pattern.replace("status:", "").trim() === String(status);
139
+ }
137
140
  return pattern.startsWith("method:") ? pattern.replace("method:", "").trim() === method : minimatch(url != null ? url : "", pattern);
138
141
  }
139
142
  function createMatcher(include, exclude) {
140
- function matcher(method, url) {
143
+ function matcher(method, url, status) {
141
144
  if (!include && !exclude) {
142
145
  return true;
143
146
  }
144
- const isExclude = (exclude != null ? exclude : []).some((pattern) => matchPattern(pattern, method, url));
147
+ const isExclude = (exclude != null ? exclude : []).some((pattern) => matchPattern(pattern, method, url, status));
145
148
  if (isExclude) {
146
149
  return false;
147
150
  }
148
151
  if (!include) {
149
152
  return true;
150
153
  }
151
- const isInclude = (include != null ? include : []).some((pattern) => matchPattern(pattern, method, url));
154
+ const isInclude = (include != null ? include : []).some((pattern) => matchPattern(pattern, method, url, status));
152
155
  return isInclude;
153
156
  }
154
157
  return matcher;
@@ -241,7 +244,7 @@ function responseBlobInterceptor(options = {}) {
241
244
  onFulfilled(response) {
242
245
  var _a, _b, _c, _d;
243
246
  const matcher = createMatcher(options.include, options.exclude);
244
- if (!matcher((_a = response.config.method) != null ? _a : "", (_b = response.config.url) != null ? _b : "")) {
247
+ if (!matcher((_a = response.config.method) != null ? _a : "", (_b = response.config.url) != null ? _b : "", response.status)) {
245
248
  return response;
246
249
  }
247
250
  if (response.request.responseType === "blob") {
@@ -259,13 +262,13 @@ function responseTimeoutInterceptor(options = {}) {
259
262
  return {
260
263
  onFulfilled: (response) => response,
261
264
  onRejected(error) {
262
- var _a, _b, _c;
265
+ var _a, _b, _c, _d;
263
266
  const matcher = createMatcher(options.include, options.exclude);
264
- if (!matcher((_a = error.config.method) != null ? _a : "", (_b = error.config.url) != null ? _b : "")) {
267
+ if (!matcher((_a = error.config.method) != null ? _a : "", (_b = error.config.url) != null ? _b : "", (_c = error == null ? void 0 : error.response) == null ? void 0 : _c.status)) {
265
268
  return Promise.reject(error);
266
269
  }
267
270
  if (error.code === "ECONNABORTED" && error.message.includes("timeout") || error.code === "ETIMEDOUT") {
268
- error.code = (_c = options == null ? void 0 : options.normalizeErrorCode) != null ? _c : "TIMEOUT";
271
+ error.code = (_d = options == null ? void 0 : options.normalizeErrorCode) != null ? _d : "TIMEOUT";
269
272
  }
270
273
  return Promise.reject(error);
271
274
  },
@@ -279,9 +282,9 @@ function responseRetryInterceptor(options) {
279
282
  return {
280
283
  onFulfilled: (response) => response,
281
284
  async onRejected(error) {
282
- var _a, _b;
285
+ var _a, _b, _c;
283
286
  const matcher = createMatcher(options.include, options.exclude);
284
- if (!matcher((_a = error.config.method) != null ? _a : "", (_b = error.config.url) != null ? _b : "") || isCancel(error)) {
287
+ if (!matcher((_a = error.config.method) != null ? _a : "", (_b = error.config.url) != null ? _b : "", (_c = error == null ? void 0 : error.response) == null ? void 0 : _c.status) || isCancel(error)) {
285
288
  return Promise.reject(error);
286
289
  }
287
290
  const { count = 1 } = options;
@@ -309,7 +312,7 @@ function responseStatusInterceptor(options) {
309
312
  onFulfilled: (response) => {
310
313
  var _a, _b, _c;
311
314
  const matcher = createMatcher(options.include, options.exclude);
312
- if (!matcher((_a = response.config.method) != null ? _a : "", (_b = response.config.url) != null ? _b : "")) {
315
+ if (!matcher((_a = response.config.method) != null ? _a : "", (_b = response.config.url) != null ? _b : "", response.status)) {
313
316
  return response;
314
317
  }
315
318
  const handler = ((_c = options.validStatusHandler) != null ? _c : {})[response.status];
@@ -320,12 +323,12 @@ function responseStatusInterceptor(options) {
320
323
  return handlerResponse != null ? handlerResponse : response;
321
324
  },
322
325
  onRejected: (error) => {
323
- var _a, _b, _c;
326
+ var _a, _b, _c, _d;
324
327
  const matcher = createMatcher(options.include, options.exclude);
325
- if (!matcher((_a = error.config.method) != null ? _a : "", (_b = error.config.url) != null ? _b : "")) {
328
+ if (!matcher((_a = error.config.method) != null ? _a : "", (_b = error.config.url) != null ? _b : "", (_c = error == null ? void 0 : error.response) == null ? void 0 : _c.status)) {
326
329
  return Promise.reject(error);
327
330
  }
328
- const handler = ((_c = options.invalidStatusHandler) != null ? _c : {})[error.response.status];
331
+ const handler = ((_d = options.invalidStatusHandler) != null ? _d : {})[error.response.status];
329
332
  if (!handler) {
330
333
  return Promise.reject(error);
331
334
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@varlet/axle",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "Progressive request tool based on axios",
5
5
  "type": "module",
6
6
  "main": "es/index.js",