@varlet/axle 0.2.18 → 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
  ```
@@ -266,12 +266,13 @@ axle.useResponseInterceptor(
266
266
 
267
267
  | Name | Description |
268
268
  | --- | --- |
269
- | [requestHeadersInterceptor](https://github.com/varletjs/axle/packages/axle/blob/main/src/interceptors/examples/requestHeadersInterceptor.md) | Used to customize the request header |
270
- | [requestMockInterceptor](https://github.com/varletjs/axle/packages/axle/blob/main/src/interceptors/examples/requestMockInterceptor.md) | Used to mock data |
271
- | [responseRetryInterceptor](https://github.com/varletjs/axle/packages/axle/blob/main/src/interceptors/examples/responseRetryInterceptor.md) | Used to realize the request abnormal retry |
272
- | [responseStatusInterceptor](https://github.com/varletjs/axle/packages/axle/blob/main/src/interceptors/examples/responseStatusInterceptor.md) | Used to intercept status code |
273
- | [responseBlobInterceptor](https://github.com/varletjs/axle/packages/axle/blob/main/src/interceptors/examples/responseBlobInterceptor.md) | Used to intercept blob type |
274
- | [responseTimeoutInterceptor](https://github.com/varletjs/axle/packages/axle/blob/main/src/interceptors/examples/responseTimeoutInterceptor.md) | Used to abnormal timeout |
269
+ | [requestHeadersInterceptor](https://github.com/varletjs/axle/blob/packages/axle/main/src/interceptors/examples/requestHeadersInterceptor.md) | Used to customize the request header |
270
+ | [requestMockInterceptor](https://github.com/varletjs/axle/blob/packages/axle/main/src/interceptors/examples/requestMockInterceptor.md) | Used to mock data |
271
+ | [requestMd5Interceptor](https://github.com/varletjs/axle/blob/packages/axle/main/src/interceptors/examples/requestMd5Interceptor.md) | Used for md encryption of parameters and headers |
272
+ | [responseRetryInterceptor](https://github.com/varletjs/axle/blob/packages/axle/main/src/interceptors/examples/responseRetryInterceptor.md) | Used to realize the request abnormal retry |
273
+ | [responseStatusInterceptor](https://github.com/varletjs/axle/blob/packages/axle/main/src/interceptors/examples/responseStatusInterceptor.md) | Used to intercept status code |
274
+ | [responseBlobInterceptor](https://github.com/varletjs/axle/blob/packages/axle/main/src/interceptors/examples/responseBlobInterceptor.md) | Used to intercept blob type |
275
+ | [responseTimeoutInterceptor](https://github.com/varletjs/axle/blob/packages/axle/main/src/interceptors/examples/responseTimeoutInterceptor.md) | Used to abnormal timeout |
275
276
 
276
277
  ## Vue Composition API
277
278
 
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
  ```
@@ -266,12 +266,13 @@ axle.useResponseInterceptor(
266
266
 
267
267
  | 名称 | 描述 |
268
268
  | --- | --- |
269
- | [requestHeadersInterceptor](https://github.com/varletjs/axle/packages/axle/blob/main/src/interceptors/examples/requestHeadersInterceptor.md) | 用于自定义请求头 |
270
- | [requestMockInterceptor](https://github.com/varletjs/axle/packages/axle/blob/main/src/interceptors/examples/requestMockInterceptor.md) | 用于模拟数据 |
271
- | [responseRetryInterceptor](https://github.com/varletjs/axle/packages/axle/blob/main/src/interceptors/examples/responseRetryInterceptor.md) | 用于实现请求异常重试 |
272
- | [responseStatusInterceptor](https://github.com/varletjs/axle/packages/axle/blob/main/src/interceptors/examples/responseStatusInterceptor.md) | 用于拦截状态码 |
273
- | [responseBlobInterceptor](https://github.com/varletjs/axle/packages/axle/blob/main/src/interceptors/examples/responseBlobInterceptor.md) | 用于拦截 blob 类型 |
274
- | [responseTimeoutInterceptor](https://github.com/varletjs/axle/packages/axle/blob/main/src/interceptors/examples/responseTimeoutInterceptor.md) | 用于归一化超时异常 |
269
+ | [requestHeadersInterceptor](https://github.com/varletjs/axle/blob/packages/axle/main/src/interceptors/examples/requestHeadersInterceptor.md) | 用于自定义请求头 |
270
+ | [requestMockInterceptor](https://github.com/varletjs/axle/blob/packages/axle/main/src/interceptors/examples/requestMockInterceptor.md) | 用于模拟数据 |
271
+ | [requestMd5Interceptor](https://github.com/varletjs/axle/blob/packages/axle/main/src/interceptors/examples/requestMd5Interceptor.md) | 用于对请求参数进行 md5 处理 |
272
+ | [responseRetryInterceptor](https://github.com/varletjs/axle/blob/packages/axle/main/src/interceptors/examples/responseRetryInterceptor.md) | 用于实现请求异常重试 |
273
+ | [responseStatusInterceptor](https://github.com/varletjs/axle/blob/packages/axle/main/src/interceptors/examples/responseStatusInterceptor.md) | 用于拦截状态码 |
274
+ | [responseBlobInterceptor](https://github.com/varletjs/axle/blob/packages/axle/main/src/interceptors/examples/responseBlobInterceptor.md) | 用于拦截 blob 类型 |
275
+ | [responseTimeoutInterceptor](https://github.com/varletjs/axle/blob/packages/axle/main/src/interceptors/examples/responseTimeoutInterceptor.md) | 用于归一化超时异常 |
275
276
 
276
277
  ## Vue 组合式 API
277
278
 
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>);
@@ -13,7 +13,8 @@ interface RequestHeadersInterceptorOptions {
13
13
  }
14
14
  declare function requestHeadersInterceptor(options?: RequestHeadersInterceptorOptions): RequestInterceptor;
15
15
 
16
- type RequestMockInterceptorMappingValue = {
16
+ type RequestMockInterceptorMapping = {
17
+ url: string;
17
18
  handler: (config: AxiosRequestConfig) => {
18
19
  data: any;
19
20
  status?: number;
@@ -23,7 +24,7 @@ type RequestMockInterceptorMappingValue = {
23
24
  delay?: number;
24
25
  };
25
26
  interface RequestMockInterceptorOptions {
26
- mapping?: Record<string, RequestMockInterceptorMappingValue>;
27
+ mappings?: RequestMockInterceptorMapping[];
27
28
  include?: string[];
28
29
  exclude?: string[];
29
30
  axiosInterceptorOptions?: AxiosInterceptorOptions;
@@ -63,16 +64,17 @@ interface ResponseStatusInterceptorOptions {
63
64
  }
64
65
  declare function responseStatusInterceptor(options: ResponseStatusInterceptorOptions): ResponseInterceptor;
65
66
 
66
- type RequestMd5InterceptorMappingValue = {
67
+ type RequestMd5InterceptorMapping = {
68
+ url: string;
67
69
  path: string[];
68
70
  method?: string;
69
71
  };
70
72
  interface RequestMd5InterceptorOptions {
71
- mapping?: Record<string, RequestMd5InterceptorMappingValue>;
73
+ mappings?: RequestMd5InterceptorMapping[];
72
74
  include?: string[];
73
75
  exclude?: string[];
74
76
  axiosInterceptorOptions?: AxiosInterceptorOptions;
75
77
  }
76
78
  declare function requestMd5Interceptor(options?: RequestMd5InterceptorOptions): RequestInterceptor;
77
79
 
78
- export { RequestHeadersInterceptorOptions, RequestInterceptor, RequestMd5InterceptorMappingValue, RequestMd5InterceptorOptions, RequestMockInterceptorMappingValue, RequestMockInterceptorOptions, ResponseBlobInterceptorOptions, ResponseInterceptor, ResponseRetryInterceptorOptions, ResponseStatusInterceptorOptions, ResponseTimeoutInterceptorOptions, createMatcher, matchPattern, requestHeadersInterceptor, requestMd5Interceptor, requestMockInterceptor, responseBlobInterceptor, responseRetryInterceptor, responseStatusInterceptor, responseTimeoutInterceptor };
80
+ export { RequestHeadersInterceptorOptions, RequestInterceptor, RequestMd5InterceptorMapping, RequestMd5InterceptorOptions, RequestMockInterceptorMapping, RequestMockInterceptorOptions, ResponseBlobInterceptorOptions, ResponseInterceptor, ResponseRetryInterceptorOptions, ResponseStatusInterceptorOptions, ResponseTimeoutInterceptorOptions, createMatcher, matchPattern, requestHeadersInterceptor, requestMd5Interceptor, requestMockInterceptor, responseBlobInterceptor, responseRetryInterceptor, responseStatusInterceptor, responseTimeoutInterceptor };
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;
@@ -200,23 +203,22 @@ function requestMockInterceptor(options = {}) {
200
203
  if (!matcher((_a = config.method) != null ? _a : "", (_b = config.url) != null ? _b : "")) {
201
204
  return config;
202
205
  }
203
- const findMappingRecord = () => {
206
+ const findMapping = () => {
204
207
  var _a2;
205
- return Object.entries((_a2 = options.mapping) != null ? _a2 : {}).find(
206
- ([key, value]) => {
207
- var _a3, _b2;
208
- return minimatch2((_a3 = config.url) != null ? _a3 : "", key) && config.method === ((_b2 = value.method) != null ? _b2 : "get");
209
- }
210
- );
208
+ return ((_a2 = options.mappings) != null ? _a2 : []).find((mapping2) => {
209
+ var _a3;
210
+ const isMatchUrl = minimatch2((_a3 = config.url) != null ? _a3 : "", mapping2.url);
211
+ const isMatchMethod = mapping2.method != null ? config.method === mapping2.method : true;
212
+ return isMatchUrl && isMatchMethod;
213
+ });
211
214
  };
212
- const mappingRecord = findMappingRecord();
213
- if (!mappingRecord) {
215
+ const mapping = findMapping();
216
+ if (!mapping) {
214
217
  return config;
215
218
  }
216
- const [, mappingValue] = mappingRecord;
217
219
  config.adapter = () => {
218
220
  var _a2, _b2;
219
- const partialResponse = mappingValue.handler(config);
221
+ const partialResponse = mapping.handler(config);
220
222
  const response = __spreadProps(__spreadValues({}, partialResponse), {
221
223
  headers: config.headers,
222
224
  config,
@@ -226,7 +228,7 @@ function requestMockInterceptor(options = {}) {
226
228
  statusText: (_b2 = partialResponse.statusText) != null ? _b2 : "OK"
227
229
  });
228
230
  return new Promise((resolve, reject) => {
229
- settle(response, resolve, reject, mappingValue.delay);
231
+ settle(response, resolve, reject, mapping.delay);
230
232
  });
231
233
  };
232
234
  return config;
@@ -242,7 +244,7 @@ function responseBlobInterceptor(options = {}) {
242
244
  onFulfilled(response) {
243
245
  var _a, _b, _c, _d;
244
246
  const matcher = createMatcher(options.include, options.exclude);
245
- 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)) {
246
248
  return response;
247
249
  }
248
250
  if (response.request.responseType === "blob") {
@@ -260,13 +262,13 @@ function responseTimeoutInterceptor(options = {}) {
260
262
  return {
261
263
  onFulfilled: (response) => response,
262
264
  onRejected(error) {
263
- var _a, _b, _c;
265
+ var _a, _b, _c, _d;
264
266
  const matcher = createMatcher(options.include, options.exclude);
265
- 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)) {
266
268
  return Promise.reject(error);
267
269
  }
268
270
  if (error.code === "ECONNABORTED" && error.message.includes("timeout") || error.code === "ETIMEDOUT") {
269
- 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";
270
272
  }
271
273
  return Promise.reject(error);
272
274
  },
@@ -280,9 +282,9 @@ function responseRetryInterceptor(options) {
280
282
  return {
281
283
  onFulfilled: (response) => response,
282
284
  async onRejected(error) {
283
- var _a, _b;
285
+ var _a, _b, _c;
284
286
  const matcher = createMatcher(options.include, options.exclude);
285
- 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)) {
286
288
  return Promise.reject(error);
287
289
  }
288
290
  const { count = 1 } = options;
@@ -310,7 +312,7 @@ function responseStatusInterceptor(options) {
310
312
  onFulfilled: (response) => {
311
313
  var _a, _b, _c;
312
314
  const matcher = createMatcher(options.include, options.exclude);
313
- 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)) {
314
316
  return response;
315
317
  }
316
318
  const handler = ((_c = options.validStatusHandler) != null ? _c : {})[response.status];
@@ -321,12 +323,12 @@ function responseStatusInterceptor(options) {
321
323
  return handlerResponse != null ? handlerResponse : response;
322
324
  },
323
325
  onRejected: (error) => {
324
- var _a, _b, _c;
326
+ var _a, _b, _c, _d;
325
327
  const matcher = createMatcher(options.include, options.exclude);
326
- 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)) {
327
329
  return Promise.reject(error);
328
330
  }
329
- const handler = ((_c = options.invalidStatusHandler) != null ? _c : {})[error.response.status];
331
+ const handler = ((_d = options.invalidStatusHandler) != null ? _d : {})[error.response.status];
330
332
  if (!handler) {
331
333
  return Promise.reject(error);
332
334
  }
@@ -343,15 +345,15 @@ import { minimatch as minimatch3 } from "minimatch";
343
345
  import { set, get } from "lodash-es";
344
346
  import { MD5 } from "crypto-js";
345
347
  import { isString } from "@varlet/shared";
346
- function withCtxMd5(ctx, mappingValue) {
347
- mappingValue.path.forEach((path) => {
348
+ function withCtxMd5(ctx, mapping) {
349
+ mapping.path.forEach((path) => {
348
350
  const targetValue = get(ctx, path);
349
351
  if (targetValue != null) {
350
352
  set(ctx, path, MD5(String(targetValue)).toString());
351
353
  }
352
354
  });
353
355
  }
354
- function withMd5(config, mappingValue) {
356
+ function withMd5(config, mapping) {
355
357
  const { data = {}, params = {}, headers = {} } = config;
356
358
  const ctx = {
357
359
  data: isFormData(data) ? data : JSON.parse(JSON.stringify(data)),
@@ -360,14 +362,14 @@ function withMd5(config, mappingValue) {
360
362
  };
361
363
  if (isString(ctx.data) && ctx.data.length > 0 && ctx.headers["Content-Type"] === "application/x-www-form-urlencoded") {
362
364
  ctx.data = qs2.parse(ctx.data);
363
- withCtxMd5(ctx, mappingValue);
365
+ withCtxMd5(ctx, mapping);
364
366
  ctx.data = qs2.stringify(ctx.data);
365
367
  } else if (isFormData(ctx.data)) {
366
368
  ctx.data = formDataToObject(ctx.data);
367
- withCtxMd5(ctx, mappingValue);
369
+ withCtxMd5(ctx, mapping);
368
370
  ctx.data = objectToFormData(ctx.data);
369
371
  } else {
370
- withCtxMd5(ctx, mappingValue);
372
+ withCtxMd5(ctx, mapping);
371
373
  }
372
374
  return __spreadValues(__spreadValues({}, config), ctx);
373
375
  }
@@ -379,21 +381,20 @@ function requestMd5Interceptor(options = {}) {
379
381
  if (!matcher((_a = config.method) != null ? _a : "", (_b = config.url) != null ? _b : "")) {
380
382
  return config;
381
383
  }
382
- const findMappingRecord = () => {
384
+ const findMapping = () => {
383
385
  var _a2;
384
- return Object.entries((_a2 = options.mapping) != null ? _a2 : {}).find(
385
- ([key, value]) => {
386
- var _a3, _b2;
387
- return minimatch3((_a3 = config.url) != null ? _a3 : "", key) && config.method === ((_b2 = value.method) != null ? _b2 : "get");
388
- }
389
- );
386
+ return ((_a2 = options.mappings) != null ? _a2 : []).find((mapping2) => {
387
+ var _a3;
388
+ const isMatchUrl = minimatch3((_a3 = config.url) != null ? _a3 : "", mapping2.url);
389
+ const isMatchMethod = mapping2.method != null ? config.method === mapping2.method : true;
390
+ return isMatchUrl && isMatchMethod;
391
+ });
390
392
  };
391
- const mappingRecord = findMappingRecord();
392
- if (!mappingRecord) {
393
+ const mapping = findMapping();
394
+ if (!mapping) {
393
395
  return config;
394
396
  }
395
- const [, mappingValue] = mappingRecord;
396
- return withMd5(config, mappingValue);
397
+ return withMd5(config, mapping);
397
398
  },
398
399
  onRejected: (error) => Promise.reject(error),
399
400
  options: options.axiosInterceptorOptions
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@varlet/axle",
3
- "version": "0.2.18",
3
+ "version": "0.3.1",
4
4
  "description": "Progressive request tool based on axios",
5
5
  "type": "module",
6
6
  "main": "es/index.js",