@varlet/axle 0.2.10 → 0.2.12

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
@@ -266,10 +266,12 @@ axle.useResponseInterceptor(
266
266
 
267
267
  | Name | Description |
268
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 |
269
+ | [requestHeadersInterceptor](https://github.com/varletjs/axle/blob/main/src/interceptors/examples/requestHeadersInterceptor.md) | Used to customize the request header |
270
+ | [requestMockInterceptor](https://github.com/varletjs/axle/blob/main/src/interceptors/examples/requestMockInterceptor.md) | Used to mock data |
271
+ | [responseRetryInterceptor](https://github.com/varletjs/axle/blob/main/src/interceptors/examples/responseRetryInterceptor.md) | Used to realize the request abnormal retry |
272
+ | [responseStatusInterceptor](https://github.com/varletjs/axle/blob/main/src/interceptors/examples/responseStatusInterceptor.md) | Used to intercept status code |
273
+ | [responseBlobInterceptor](https://github.com/varletjs/axle/blob/main/src/interceptors/examples/responseBlobInterceptor.md) | Used to intercept blob type |
274
+ | [responseTimeoutInterceptor](https://github.com/varletjs/axle/blob/main/src/interceptors/examples/responseTimeoutInterceptor.md) | Used to abnormal timeout |
273
275
 
274
276
  ## Vue Composition API
275
277
 
package/README.zh-CN.md CHANGED
@@ -266,10 +266,12 @@ axle.useResponseInterceptor(
266
266
 
267
267
  | 名称 | 描述 |
268
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) | 用于归一化超时异常 |
269
+ | [requestHeadersInterceptor](https://github.com/varletjs/axle/blob/main/src/interceptors/examples/requestHeadersInterceptor.md) | 用于自定义请求头 |
270
+ | [requestMockInterceptor](https://github.com/varletjs/axle/blob/main/src/interceptors/examples/requestMockInterceptor.md) | 用于模拟数据 |
271
+ | [responseRetryInterceptor](https://github.com/varletjs/axle/blob/main/src/interceptors/examples/responseRetryInterceptor.md) | 用于实现请求异常重试 |
272
+ | [responseStatusInterceptor](https://github.com/varletjs/axle/blob/main/src/interceptors/examples/responseStatusInterceptor.md) | 用于拦截状态码 |
273
+ | [responseBlobInterceptor](https://github.com/varletjs/axle/blob/main/src/interceptors/examples/responseBlobInterceptor.md) | 用于拦截 blob 类型 |
274
+ | [responseTimeoutInterceptor](https://github.com/varletjs/axle/blob/main/src/interceptors/examples/responseTimeoutInterceptor.md) | 用于归一化超时异常 |
273
275
 
274
276
  ## Vue 组合式 API
275
277
 
@@ -1,4 +1,6 @@
1
1
  var __defProp = Object.defineProperty;
2
+ var __defProps = Object.defineProperties;
3
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
2
4
  var __getOwnPropSymbols = Object.getOwnPropertySymbols;
3
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
4
6
  var __propIsEnum = Object.prototype.propertyIsEnumerable;
@@ -14,7 +16,9 @@ var __spreadValues = (a, b) => {
14
16
  }
15
17
  return a;
16
18
  };
19
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
17
20
 
18
21
  export {
19
- __spreadValues
22
+ __spreadValues,
23
+ __spreadProps
20
24
  };
package/es/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { R as RequestInterceptor, a as ResponseInterceptor } from './instance-8e68a761.js';
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
- import { AxiosInterceptorOptions, AxiosResponse } from 'axios';
3
+ import { AxiosInterceptorOptions, AxiosRequestConfig, AxiosResponse } from 'axios';
4
4
 
5
5
  declare function matchPattern(pattern: string, method: string, url: string): boolean;
6
6
  declare function createMatcher(include?: string[], exclude?: string[]): (method: string, url: string) => boolean;
@@ -13,6 +13,23 @@ interface RequestHeadersInterceptorOptions {
13
13
  }
14
14
  declare function requestHeadersInterceptor(options?: RequestHeadersInterceptorOptions): RequestInterceptor;
15
15
 
16
+ type RequestMockInterceptorMappingValue = {
17
+ handler: (config: AxiosRequestConfig) => {
18
+ data: any;
19
+ status?: number;
20
+ statusText?: string;
21
+ };
22
+ method?: string;
23
+ delay?: number;
24
+ };
25
+ interface RequestMockInterceptorOptions {
26
+ mapping?: Record<string, RequestMockInterceptorMappingValue>;
27
+ include?: string[];
28
+ exclude?: string[];
29
+ axiosInterceptorOptions?: AxiosInterceptorOptions;
30
+ }
31
+ declare function requestMockInterceptor(options?: RequestMockInterceptorOptions): RequestInterceptor;
32
+
16
33
  interface ResponseBlobInterceptorOptions {
17
34
  onResponse?: (response: AxiosResponse<any, any>) => any;
18
35
  include?: string[];
@@ -37,4 +54,13 @@ interface ResponseRetryInterceptorOptions {
37
54
  }
38
55
  declare function responseRetryInterceptor(options: ResponseRetryInterceptorOptions): ResponseInterceptor;
39
56
 
40
- export { RequestHeadersInterceptorOptions, RequestInterceptor, ResponseBlobInterceptorOptions, ResponseInterceptor, ResponseRetryInterceptorOptions, ResponseTimeoutInterceptorOptions, createMatcher, matchPattern, requestHeadersInterceptor, responseBlobInterceptor, responseRetryInterceptor, responseTimeoutInterceptor };
57
+ interface ResponseStatusInterceptorOptions {
58
+ validateHandler?: Record<number | string, (response: AxiosResponse<any, any>) => any>;
59
+ invalidateHandler?: Record<number | string, (error: any) => any>;
60
+ include?: string[];
61
+ exclude?: string[];
62
+ axiosInterceptorOptions?: AxiosInterceptorOptions;
63
+ }
64
+ declare function responseStatusInterceptor(options: ResponseStatusInterceptorOptions): ResponseInterceptor;
65
+
66
+ export { RequestHeadersInterceptorOptions, RequestInterceptor, RequestMockInterceptorMappingValue, RequestMockInterceptorOptions, ResponseBlobInterceptorOptions, ResponseInterceptor, ResponseRetryInterceptorOptions, ResponseStatusInterceptorOptions, ResponseTimeoutInterceptorOptions, createMatcher, matchPattern, requestHeadersInterceptor, requestMockInterceptor, responseBlobInterceptor, responseRetryInterceptor, responseStatusInterceptor, responseTimeoutInterceptor };
package/es/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import {
2
+ __spreadProps,
2
3
  __spreadValues
3
- } from "./chunk-ZSUG26OQ.js";
4
+ } from "./chunk-KLNSLHAC.js";
4
5
 
5
6
  // src/instance.ts
6
7
  import axios from "axios";
@@ -158,6 +159,66 @@ function requestHeadersInterceptor(options = {}) {
158
159
  };
159
160
  }
160
161
 
162
+ // src/interceptors/requestMockInterceptor.ts
163
+ import { AxiosError } from "axios";
164
+ import { minimatch as minimatch2 } from "minimatch";
165
+ function settle(response, resolve, reject, delay = 0) {
166
+ if (delay > 0) {
167
+ setTimeout(() => {
168
+ settle(response, resolve, reject);
169
+ }, delay);
170
+ return;
171
+ }
172
+ if (!response.config.validateStatus || response.config.validateStatus(response.status)) {
173
+ resolve(response);
174
+ } else {
175
+ reject(new AxiosError(`Request failed with status code ${response.status}`, void 0, response.config, response));
176
+ }
177
+ }
178
+ function requestMockInterceptor(options = {}) {
179
+ return {
180
+ onFulfilled(config) {
181
+ var _a, _b;
182
+ const matcher = createMatcher(options.include, options.exclude);
183
+ if (!matcher((_a = config.method) != null ? _a : "", (_b = config.url) != null ? _b : "")) {
184
+ return config;
185
+ }
186
+ const findMappingRecord = () => {
187
+ var _a2;
188
+ return Object.entries((_a2 = options.mapping) != null ? _a2 : {}).find(
189
+ ([key, value]) => {
190
+ var _a3, _b2;
191
+ return minimatch2((_a3 = config.url) != null ? _a3 : "", key) && config.method === ((_b2 = value.method) != null ? _b2 : "get");
192
+ }
193
+ );
194
+ };
195
+ const mappingRecord = findMappingRecord();
196
+ if (!mappingRecord) {
197
+ return config;
198
+ }
199
+ const [, mappingValue] = mappingRecord;
200
+ config.adapter = () => {
201
+ var _a2, _b2;
202
+ const partialResponse = mappingValue.handler(config);
203
+ const response = __spreadProps(__spreadValues({}, partialResponse), {
204
+ headers: config.headers,
205
+ config,
206
+ request: {},
207
+ data: partialResponse.data,
208
+ status: (_a2 = partialResponse.status) != null ? _a2 : 200,
209
+ statusText: (_b2 = partialResponse.statusText) != null ? _b2 : "OK"
210
+ });
211
+ return new Promise((resolve, reject) => {
212
+ settle(response, resolve, reject, mappingValue.delay);
213
+ });
214
+ };
215
+ return config;
216
+ },
217
+ onRejected: (error) => Promise.reject(error),
218
+ options: options.axiosInterceptorOptions
219
+ };
220
+ }
221
+
161
222
  // src/interceptors/responseBlobInterceptor.ts
162
223
  function responseBlobInterceptor(options = {}) {
163
224
  return {
@@ -225,6 +286,39 @@ function responseRetryInterceptor(options) {
225
286
  options: options.axiosInterceptorOptions
226
287
  };
227
288
  }
289
+
290
+ // src/interceptors/responseStatusInterceptor.ts
291
+ function responseStatusInterceptor(options) {
292
+ return {
293
+ onFulfilled: (response) => {
294
+ var _a, _b, _c;
295
+ const matcher = createMatcher(options.include, options.exclude);
296
+ if (!matcher((_a = response.config.method) != null ? _a : "", (_b = response.config.url) != null ? _b : "")) {
297
+ return response;
298
+ }
299
+ const handler = ((_c = options.validateHandler) != null ? _c : {})[response.status];
300
+ if (!handler) {
301
+ return response;
302
+ }
303
+ const handlerResponse = handler(response);
304
+ return handlerResponse != null ? handlerResponse : response;
305
+ },
306
+ onRejected: (error) => {
307
+ var _a, _b, _c;
308
+ const matcher = createMatcher(options.include, options.exclude);
309
+ if (!matcher((_a = error.config.method) != null ? _a : "", (_b = error.config.url) != null ? _b : "")) {
310
+ return Promise.reject(error);
311
+ }
312
+ const handler = ((_c = options.invalidateHandler) != null ? _c : {})[error.response.status];
313
+ if (!handler) {
314
+ return Promise.reject(error);
315
+ }
316
+ const handlerError = handler(error);
317
+ return handlerError != null ? handlerError : Promise.reject(error);
318
+ },
319
+ options: options.axiosInterceptorOptions
320
+ };
321
+ }
228
322
  export {
229
323
  createAxle,
230
324
  createFetchRunner,
@@ -233,7 +327,9 @@ export {
233
327
  download,
234
328
  matchPattern,
235
329
  requestHeadersInterceptor,
330
+ requestMockInterceptor,
236
331
  responseBlobInterceptor,
237
332
  responseRetryInterceptor,
333
+ responseStatusInterceptor,
238
334
  responseTimeoutInterceptor
239
335
  };
package/es/use.js CHANGED
@@ -1,12 +1,12 @@
1
1
  import {
2
2
  __spreadValues
3
- } from "./chunk-ZSUG26OQ.js";
3
+ } from "./chunk-KLNSLHAC.js";
4
4
 
5
5
  // src/use.ts
6
6
  import { ref as ref3 } from "vue";
7
7
  import { isFunction } from "@varlet/shared";
8
8
 
9
- // src/useValues.ts
9
+ // src/composables/useValues.ts
10
10
  import { computed } from "vue";
11
11
  function useValues(...valueCollection) {
12
12
  return computed({
@@ -19,7 +19,7 @@ function useValues(...valueCollection) {
19
19
  });
20
20
  }
21
21
 
22
- // src/useHasLoading.ts
22
+ // src/composables/useHasLoading.ts
23
23
  import { ref, watch } from "vue";
24
24
  function useHasLoading(...loadings) {
25
25
  const hasLoading = ref(false);
@@ -33,7 +33,7 @@ function useHasLoading(...loadings) {
33
33
  return hasLoading;
34
34
  }
35
35
 
36
- // src/useAverageProgress.ts
36
+ // src/composables/useAverageProgress.ts
37
37
  import { ref as ref2, watch as watch2 } from "vue";
38
38
  function useAverageProgress(...progressCollection) {
39
39
  const averageProgress = ref2(0);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@varlet/axle",
3
- "version": "0.2.10",
3
+ "version": "0.2.12",
4
4
  "description": "Progressive request tool based on axios",
5
5
  "type": "module",
6
6
  "main": "es/index.js",