@varlet/axle 0.2.18-alpha.1702624592933 → 0.3.0
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 +7 -6
- package/README.zh-CN.md +7 -6
- package/es/index.d.ts +7 -5
- package/es/index.js +42 -39
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -266,12 +266,13 @@ axle.useResponseInterceptor(
|
|
|
266
266
|
|
|
267
267
|
| Name | Description |
|
|
268
268
|
| --- | --- |
|
|
269
|
-
| [requestHeadersInterceptor](https://github.com/varletjs/axle/packages/axle/
|
|
270
|
-
| [requestMockInterceptor](https://github.com/varletjs/axle/packages/axle/
|
|
271
|
-
| [
|
|
272
|
-
| [
|
|
273
|
-
| [
|
|
274
|
-
| [
|
|
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
|
@@ -266,12 +266,13 @@ axle.useResponseInterceptor(
|
|
|
266
266
|
|
|
267
267
|
| 名称 | 描述 |
|
|
268
268
|
| --- | --- |
|
|
269
|
-
| [requestHeadersInterceptor](https://github.com/varletjs/axle/packages/axle/
|
|
270
|
-
| [requestMockInterceptor](https://github.com/varletjs/axle/packages/axle/
|
|
271
|
-
| [
|
|
272
|
-
| [
|
|
273
|
-
| [
|
|
274
|
-
| [
|
|
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
|
@@ -13,7 +13,8 @@ interface RequestHeadersInterceptorOptions {
|
|
|
13
13
|
}
|
|
14
14
|
declare function requestHeadersInterceptor(options?: RequestHeadersInterceptorOptions): RequestInterceptor;
|
|
15
15
|
|
|
16
|
-
type
|
|
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
|
-
|
|
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
|
|
67
|
+
type RequestMd5InterceptorMapping = {
|
|
68
|
+
url: string;
|
|
67
69
|
path: string[];
|
|
68
70
|
method?: string;
|
|
69
71
|
};
|
|
70
72
|
interface RequestMd5InterceptorOptions {
|
|
71
|
-
|
|
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,
|
|
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
|
@@ -200,23 +200,22 @@ function requestMockInterceptor(options = {}) {
|
|
|
200
200
|
if (!matcher((_a = config.method) != null ? _a : "", (_b = config.url) != null ? _b : "")) {
|
|
201
201
|
return config;
|
|
202
202
|
}
|
|
203
|
-
const
|
|
203
|
+
const findMapping = () => {
|
|
204
204
|
var _a2;
|
|
205
|
-
return
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
);
|
|
205
|
+
return ((_a2 = options.mappings) != null ? _a2 : []).find((mapping2) => {
|
|
206
|
+
var _a3;
|
|
207
|
+
const isMatchUrl = minimatch2((_a3 = config.url) != null ? _a3 : "", mapping2.url);
|
|
208
|
+
const isMatchMethod = mapping2.method != null ? config.method === mapping2.method : true;
|
|
209
|
+
return isMatchUrl && isMatchMethod;
|
|
210
|
+
});
|
|
211
211
|
};
|
|
212
|
-
const
|
|
213
|
-
if (!
|
|
212
|
+
const mapping = findMapping();
|
|
213
|
+
if (!mapping) {
|
|
214
214
|
return config;
|
|
215
215
|
}
|
|
216
|
-
const [, mappingValue] = mappingRecord;
|
|
217
216
|
config.adapter = () => {
|
|
218
217
|
var _a2, _b2;
|
|
219
|
-
const partialResponse =
|
|
218
|
+
const partialResponse = mapping.handler(config);
|
|
220
219
|
const response = __spreadProps(__spreadValues({}, partialResponse), {
|
|
221
220
|
headers: config.headers,
|
|
222
221
|
config,
|
|
@@ -226,7 +225,7 @@ function requestMockInterceptor(options = {}) {
|
|
|
226
225
|
statusText: (_b2 = partialResponse.statusText) != null ? _b2 : "OK"
|
|
227
226
|
});
|
|
228
227
|
return new Promise((resolve, reject) => {
|
|
229
|
-
settle(response, resolve, reject,
|
|
228
|
+
settle(response, resolve, reject, mapping.delay);
|
|
230
229
|
});
|
|
231
230
|
};
|
|
232
231
|
return config;
|
|
@@ -343,28 +342,33 @@ import { minimatch as minimatch3 } from "minimatch";
|
|
|
343
342
|
import { set, get } from "lodash-es";
|
|
344
343
|
import { MD5 } from "crypto-js";
|
|
345
344
|
import { isString } from "@varlet/shared";
|
|
346
|
-
function
|
|
347
|
-
|
|
348
|
-
const targetValue = get(
|
|
345
|
+
function withCtxMd5(ctx, mapping) {
|
|
346
|
+
mapping.path.forEach((path) => {
|
|
347
|
+
const targetValue = get(ctx, path);
|
|
349
348
|
if (targetValue != null) {
|
|
350
|
-
set(
|
|
349
|
+
set(ctx, path, MD5(String(targetValue)).toString());
|
|
351
350
|
}
|
|
352
351
|
});
|
|
353
352
|
}
|
|
354
|
-
function withMd5(config,
|
|
355
|
-
const { data, headers = {} } = config;
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
353
|
+
function withMd5(config, mapping) {
|
|
354
|
+
const { data = {}, params = {}, headers = {} } = config;
|
|
355
|
+
const ctx = {
|
|
356
|
+
data: isFormData(data) ? data : JSON.parse(JSON.stringify(data)),
|
|
357
|
+
params: JSON.parse(JSON.stringify(params)),
|
|
358
|
+
headers: JSON.parse(JSON.stringify(headers))
|
|
359
|
+
};
|
|
360
|
+
if (isString(ctx.data) && ctx.data.length > 0 && ctx.headers["Content-Type"] === "application/x-www-form-urlencoded") {
|
|
361
|
+
ctx.data = qs2.parse(ctx.data);
|
|
362
|
+
withCtxMd5(ctx, mapping);
|
|
363
|
+
ctx.data = qs2.stringify(ctx.data);
|
|
364
|
+
} else if (isFormData(ctx.data)) {
|
|
365
|
+
ctx.data = formDataToObject(ctx.data);
|
|
366
|
+
withCtxMd5(ctx, mapping);
|
|
367
|
+
ctx.data = objectToFormData(ctx.data);
|
|
364
368
|
} else {
|
|
365
|
-
|
|
369
|
+
withCtxMd5(ctx, mapping);
|
|
366
370
|
}
|
|
367
|
-
return config;
|
|
371
|
+
return __spreadValues(__spreadValues({}, config), ctx);
|
|
368
372
|
}
|
|
369
373
|
function requestMd5Interceptor(options = {}) {
|
|
370
374
|
return {
|
|
@@ -374,21 +378,20 @@ function requestMd5Interceptor(options = {}) {
|
|
|
374
378
|
if (!matcher((_a = config.method) != null ? _a : "", (_b = config.url) != null ? _b : "")) {
|
|
375
379
|
return config;
|
|
376
380
|
}
|
|
377
|
-
const
|
|
381
|
+
const findMapping = () => {
|
|
378
382
|
var _a2;
|
|
379
|
-
return
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
);
|
|
383
|
+
return ((_a2 = options.mappings) != null ? _a2 : []).find((mapping2) => {
|
|
384
|
+
var _a3;
|
|
385
|
+
const isMatchUrl = minimatch3((_a3 = config.url) != null ? _a3 : "", mapping2.url);
|
|
386
|
+
const isMatchMethod = mapping2.method != null ? config.method === mapping2.method : true;
|
|
387
|
+
return isMatchUrl && isMatchMethod;
|
|
388
|
+
});
|
|
385
389
|
};
|
|
386
|
-
const
|
|
387
|
-
if (!
|
|
390
|
+
const mapping = findMapping();
|
|
391
|
+
if (!mapping) {
|
|
388
392
|
return config;
|
|
389
393
|
}
|
|
390
|
-
|
|
391
|
-
return withMd5(config, mappingValue);
|
|
394
|
+
return withMd5(config, mapping);
|
|
392
395
|
},
|
|
393
396
|
onRejected: (error) => Promise.reject(error),
|
|
394
397
|
options: options.axiosInterceptorOptions
|