@varlet/axle 0.11.2 → 0.13.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 +9 -0
- package/README.zh-CN.md +9 -0
- package/lib/api.cjs +8 -2
- package/lib/api.d.cts +15 -4
- package/lib/api.d.ts +15 -4
- package/lib/api.js +6 -2
- package/lib/index.cjs +16 -14
- package/lib/index.d.cts +24 -19
- package/lib/index.d.ts +24 -19
- package/lib/index.js +13 -11
- package/lib/use.cjs +253 -80
- package/lib/use.d.cts +31 -6
- package/lib/use.d.ts +31 -6
- package/lib/use.js +258 -86
- package/package.json +1 -3
package/README.md
CHANGED
|
@@ -297,6 +297,8 @@ Axle provides the usage of Vue Composition API style, which encapsulates the `lo
|
|
|
297
297
|
onTransform: (response) => response,
|
|
298
298
|
})
|
|
299
299
|
|
|
300
|
+
const id = ref('1')
|
|
301
|
+
|
|
300
302
|
const [
|
|
301
303
|
users,
|
|
302
304
|
// request runner/invoker
|
|
@@ -312,6 +314,13 @@ Axle provides the usage of Vue Composition API style, which encapsulates the `lo
|
|
|
312
314
|
url: '/user',
|
|
313
315
|
// Whether to send the request immediately, defaults true
|
|
314
316
|
immediate: false,
|
|
317
|
+
// Automatically reload when reactive parameters change
|
|
318
|
+
// When set to true, watch all parameters (params, config, pathParams)
|
|
319
|
+
// When set to an object, watch specific parameters:
|
|
320
|
+
// - params: true - watch params changes
|
|
321
|
+
// - pathParams: true - watch pathParams changes (for API definitions)
|
|
322
|
+
// - config: true - watch config changes
|
|
323
|
+
watch: true,
|
|
315
324
|
// Whether the value needs to be reset before requesting, defaults false
|
|
316
325
|
resetValue: true,
|
|
317
326
|
// Whether to clone when resetting value, defaults false
|
package/README.zh-CN.md
CHANGED
|
@@ -297,6 +297,8 @@ Axle 提供了 Vue Composition API 风格的用法,封装了请求的 `加载
|
|
|
297
297
|
onTransform: (response) => response,
|
|
298
298
|
})
|
|
299
299
|
|
|
300
|
+
const id = ref('1')
|
|
301
|
+
|
|
300
302
|
const [
|
|
301
303
|
users,
|
|
302
304
|
// 请求触发器
|
|
@@ -312,6 +314,13 @@ Axle 提供了 Vue Composition API 风格的用法,封装了请求的 `加载
|
|
|
312
314
|
url: '/user',
|
|
313
315
|
// 是否立即发送请求, 默认值: true
|
|
314
316
|
immediate: false,
|
|
317
|
+
// 监听响应式参数变化时自动重新加载
|
|
318
|
+
// 设置为 true 时,监听所有参数变化 (params, config, pathParams)
|
|
319
|
+
// 设置为对象时,监听指定参数变化:
|
|
320
|
+
// - params: true - 监听 params 参数变化
|
|
321
|
+
// - pathParams: true - 监听 pathParams 变化(用于 API 定义)
|
|
322
|
+
// - config: true - 监听 config 配置变化
|
|
323
|
+
watch: true,
|
|
315
324
|
// 请求前是否需要重置 value, 默认值: false
|
|
316
325
|
resetValue: true,
|
|
317
326
|
// 重置 value 是否对 value 进行拷贝
|
package/lib/api.cjs
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __defProp = Object.defineProperty;
|
|
3
|
+
var __defProps = Object.defineProperties;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
6
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
7
|
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
6
8
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
@@ -17,6 +19,7 @@ var __spreadValues = (a, b) => {
|
|
|
17
19
|
}
|
|
18
20
|
return a;
|
|
19
21
|
};
|
|
22
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
23
|
var __objRest = (source, exclude) => {
|
|
21
24
|
var target = {};
|
|
22
25
|
for (var prop in source)
|
|
@@ -56,10 +59,13 @@ function createApi(axle, useAxle) {
|
|
|
56
59
|
return axle[method](patchUrl(url, pathParams != null ? pathParams : {}), params, config);
|
|
57
60
|
}
|
|
58
61
|
function use(options = {}) {
|
|
59
|
-
const _a = options, { pathParams = {} } = _a, rest = __objRest(_a, ["pathParams"]);
|
|
62
|
+
const _a = options, { pathParams = {}, watch: watchOptions } = _a, rest = __objRest(_a, ["pathParams", "watch"]);
|
|
63
|
+
const enableWatchPathParams = (0, import_rattail.isFunction)(pathParams) && ((watchOptions == null ? void 0 : watchOptions.pathParams) || watchOptions === true);
|
|
64
|
+
const normalizedWatchOptions = watchOptions === true ? { params: true, config: true, _custom: enableWatchPathParams ? pathParams : void 0 } : watchOptions === false ? { params: false, config: false } : __spreadProps(__spreadValues({ params: false, config: false }, watchOptions), { _custom: enableWatchPathParams ? pathParams : void 0 });
|
|
60
65
|
return useAxle(__spreadValues({
|
|
61
66
|
url: () => patchUrl(url, pathParams),
|
|
62
|
-
method
|
|
67
|
+
method,
|
|
68
|
+
watch: normalizedWatchOptions
|
|
63
69
|
}, rest));
|
|
64
70
|
}
|
|
65
71
|
function patchUrl(url2, pathParams) {
|
package/lib/api.d.cts
CHANGED
|
@@ -1,17 +1,28 @@
|
|
|
1
|
-
import { UseAxleOptions, UseAxle, UseAxleInstance } from './use.cjs';
|
|
2
1
|
import { e as AxleInstance, d as RunnerMethod, A as AxleRequestConfig } from './instance-857fe07c.js';
|
|
3
|
-
import '
|
|
2
|
+
import { WatchOptions, UseAxleOptions, UseAxleOptionsWithRunnable, UseAxle, UseAxleInstance } from './use.cjs';
|
|
4
3
|
import 'axios';
|
|
4
|
+
import 'vue';
|
|
5
5
|
|
|
6
6
|
type ApiPathParams = Record<string, any> | (() => Record<string, any>);
|
|
7
|
+
type ApiWatchOptions = WatchOptions & {
|
|
8
|
+
pathParams?: boolean;
|
|
9
|
+
};
|
|
7
10
|
type ApiUseOptions<V, R, P, D> = Partial<UseAxleOptions<V, R, P, D>> & {
|
|
8
11
|
pathParams?: ApiPathParams;
|
|
12
|
+
watch?: ApiWatchOptions;
|
|
13
|
+
};
|
|
14
|
+
type ApiUseOptionsWithRunnable<V, R, P, D> = Partial<Omit<UseAxleOptionsWithRunnable<V, R, P, D>, 'runnable'>> & Pick<UseAxleOptionsWithRunnable<V, R, P, D>, 'runnable'> & {
|
|
15
|
+
pathParams?: ApiPathParams;
|
|
16
|
+
watch?: ApiWatchOptions;
|
|
9
17
|
};
|
|
10
18
|
declare function createApi(axle: AxleInstance, useAxle: UseAxle): <R = any, P = Record<string, any>, D = Record<string, any>>(url: string, method: RunnerMethod) => {
|
|
11
19
|
url: string;
|
|
12
20
|
load: (params?: P | undefined, pathParams?: ApiPathParams, config?: AxleRequestConfig<D, Record<string, any>> | undefined) => Promise<R>;
|
|
13
|
-
use:
|
|
21
|
+
use: {
|
|
22
|
+
<V = R>(options?: ApiUseOptionsWithRunnable<V, R, P, D> | undefined): UseAxleInstance<V, R | undefined, P, D>;
|
|
23
|
+
<V_1 = R>(options?: ApiUseOptions<V_1, R, P, D> | undefined): UseAxleInstance<V_1, R, P, D>;
|
|
24
|
+
};
|
|
14
25
|
patchUrl: (url: string, pathParams: ApiPathParams) => string;
|
|
15
26
|
};
|
|
16
27
|
|
|
17
|
-
export { ApiPathParams, ApiUseOptions, createApi };
|
|
28
|
+
export { ApiPathParams, ApiUseOptions, ApiUseOptionsWithRunnable, ApiWatchOptions, createApi };
|
package/lib/api.d.ts
CHANGED
|
@@ -1,17 +1,28 @@
|
|
|
1
|
-
import { UseAxleOptions, UseAxle, UseAxleInstance } from './use.js';
|
|
2
1
|
import { e as AxleInstance, d as RunnerMethod, A as AxleRequestConfig } from './instance-857fe07c.js';
|
|
3
|
-
import '
|
|
2
|
+
import { WatchOptions, UseAxleOptions, UseAxleOptionsWithRunnable, UseAxle, UseAxleInstance } from './use.js';
|
|
4
3
|
import 'axios';
|
|
4
|
+
import 'vue';
|
|
5
5
|
|
|
6
6
|
type ApiPathParams = Record<string, any> | (() => Record<string, any>);
|
|
7
|
+
type ApiWatchOptions = WatchOptions & {
|
|
8
|
+
pathParams?: boolean;
|
|
9
|
+
};
|
|
7
10
|
type ApiUseOptions<V, R, P, D> = Partial<UseAxleOptions<V, R, P, D>> & {
|
|
8
11
|
pathParams?: ApiPathParams;
|
|
12
|
+
watch?: ApiWatchOptions;
|
|
13
|
+
};
|
|
14
|
+
type ApiUseOptionsWithRunnable<V, R, P, D> = Partial<Omit<UseAxleOptionsWithRunnable<V, R, P, D>, 'runnable'>> & Pick<UseAxleOptionsWithRunnable<V, R, P, D>, 'runnable'> & {
|
|
15
|
+
pathParams?: ApiPathParams;
|
|
16
|
+
watch?: ApiWatchOptions;
|
|
9
17
|
};
|
|
10
18
|
declare function createApi(axle: AxleInstance, useAxle: UseAxle): <R = any, P = Record<string, any>, D = Record<string, any>>(url: string, method: RunnerMethod) => {
|
|
11
19
|
url: string;
|
|
12
20
|
load: (params?: P | undefined, pathParams?: ApiPathParams, config?: AxleRequestConfig<D, Record<string, any>> | undefined) => Promise<R>;
|
|
13
|
-
use:
|
|
21
|
+
use: {
|
|
22
|
+
<V = R>(options?: ApiUseOptionsWithRunnable<V, R, P, D> | undefined): UseAxleInstance<V, R | undefined, P, D>;
|
|
23
|
+
<V_1 = R>(options?: ApiUseOptions<V_1, R, P, D> | undefined): UseAxleInstance<V_1, R, P, D>;
|
|
24
|
+
};
|
|
14
25
|
patchUrl: (url: string, pathParams: ApiPathParams) => string;
|
|
15
26
|
};
|
|
16
27
|
|
|
17
|
-
export { ApiPathParams, ApiUseOptions, createApi };
|
|
28
|
+
export { ApiPathParams, ApiUseOptions, ApiUseOptionsWithRunnable, ApiWatchOptions, createApi };
|
package/lib/api.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
__objRest,
|
|
3
|
+
__spreadProps,
|
|
3
4
|
__spreadValues
|
|
4
5
|
} from "./chunk-G2ADBYYC.js";
|
|
5
6
|
|
|
@@ -11,10 +12,13 @@ function createApi(axle, useAxle) {
|
|
|
11
12
|
return axle[method](patchUrl(url, pathParams != null ? pathParams : {}), params, config);
|
|
12
13
|
}
|
|
13
14
|
function use(options = {}) {
|
|
14
|
-
const _a = options, { pathParams = {} } = _a, rest = __objRest(_a, ["pathParams"]);
|
|
15
|
+
const _a = options, { pathParams = {}, watch: watchOptions } = _a, rest = __objRest(_a, ["pathParams", "watch"]);
|
|
16
|
+
const enableWatchPathParams = isFunction(pathParams) && ((watchOptions == null ? void 0 : watchOptions.pathParams) || watchOptions === true);
|
|
17
|
+
const normalizedWatchOptions = watchOptions === true ? { params: true, config: true, _custom: enableWatchPathParams ? pathParams : void 0 } : watchOptions === false ? { params: false, config: false } : __spreadProps(__spreadValues({ params: false, config: false }, watchOptions), { _custom: enableWatchPathParams ? pathParams : void 0 });
|
|
15
18
|
return useAxle(__spreadValues({
|
|
16
19
|
url: () => patchUrl(url, pathParams),
|
|
17
|
-
method
|
|
20
|
+
method,
|
|
21
|
+
watch: normalizedWatchOptions
|
|
18
22
|
}, rest));
|
|
19
23
|
}
|
|
20
24
|
function patchUrl(url2, pathParams) {
|
package/lib/index.cjs
CHANGED
|
@@ -183,12 +183,15 @@ function createAxle(config = {}) {
|
|
|
183
183
|
}
|
|
184
184
|
|
|
185
185
|
// src/matcher.ts
|
|
186
|
-
var
|
|
186
|
+
var import_rattail = require("rattail");
|
|
187
187
|
function matchPattern(pattern, method, url, status) {
|
|
188
|
+
if ((0, import_rattail.isFunction)(pattern)) {
|
|
189
|
+
return pattern({ url, method, status });
|
|
190
|
+
}
|
|
188
191
|
if (pattern.startsWith("status:")) {
|
|
189
192
|
return pattern.replace("status:", "").trim() === String(status);
|
|
190
193
|
}
|
|
191
|
-
return pattern.startsWith("method:") ? pattern.replace("method:", "").trim() === method :
|
|
194
|
+
return pattern.startsWith("method:") ? pattern.replace("method:", "").trim() === method : url === pattern;
|
|
192
195
|
}
|
|
193
196
|
function createMatcher(include, exclude) {
|
|
194
197
|
function matcher(method, url, status) {
|
|
@@ -210,7 +213,7 @@ function createMatcher(include, exclude) {
|
|
|
210
213
|
|
|
211
214
|
// src/helper.ts
|
|
212
215
|
var import_axios3 = require("axios");
|
|
213
|
-
var
|
|
216
|
+
var import_rattail2 = require("rattail");
|
|
214
217
|
function withResponse(promise) {
|
|
215
218
|
return new Promise((resolve, reject) => {
|
|
216
219
|
promise.then((response) => {
|
|
@@ -231,7 +234,7 @@ function withResponse(promise) {
|
|
|
231
234
|
});
|
|
232
235
|
}
|
|
233
236
|
function download(url, filename) {
|
|
234
|
-
if (!(0,
|
|
237
|
+
if (!(0, import_rattail2.inBrowser)()) {
|
|
235
238
|
return;
|
|
236
239
|
}
|
|
237
240
|
const a = document.createElement("a");
|
|
@@ -245,7 +248,7 @@ function download(url, filename) {
|
|
|
245
248
|
}
|
|
246
249
|
|
|
247
250
|
// src/interceptors/requestHeadersInterceptor.ts
|
|
248
|
-
var
|
|
251
|
+
var import_rattail3 = require("rattail");
|
|
249
252
|
function requestHeadersInterceptor(options = {}) {
|
|
250
253
|
const { headers: headersOrGetter } = options;
|
|
251
254
|
return {
|
|
@@ -255,7 +258,7 @@ function requestHeadersInterceptor(options = {}) {
|
|
|
255
258
|
if (!matcher((_a = config.method) != null ? _a : "", (_b = config.url) != null ? _b : "")) {
|
|
256
259
|
return config;
|
|
257
260
|
}
|
|
258
|
-
const headers = (_c = (0,
|
|
261
|
+
const headers = (_c = (0, import_rattail3.isFunction)(headersOrGetter) ? headersOrGetter() : headersOrGetter) != null ? _c : {};
|
|
259
262
|
Object.entries(headers).forEach(([key, value]) => {
|
|
260
263
|
config.headers[key] = value;
|
|
261
264
|
});
|
|
@@ -268,7 +271,7 @@ function requestHeadersInterceptor(options = {}) {
|
|
|
268
271
|
|
|
269
272
|
// src/interceptors/requestMockInterceptor.ts
|
|
270
273
|
var import_axios4 = require("axios");
|
|
271
|
-
var
|
|
274
|
+
var import_rattail4 = require("rattail");
|
|
272
275
|
function settle(response, resolve, reject, delay = 0) {
|
|
273
276
|
if (delay > 0) {
|
|
274
277
|
setTimeout(() => {
|
|
@@ -293,8 +296,8 @@ function requestMockInterceptor(options = {}) {
|
|
|
293
296
|
const findMapping = () => {
|
|
294
297
|
var _a2;
|
|
295
298
|
return ((_a2 = options.mappings) != null ? _a2 : []).find((mapping2) => {
|
|
296
|
-
var _a3;
|
|
297
|
-
const isMatchUrl = (0,
|
|
299
|
+
var _a3, _b2;
|
|
300
|
+
const isMatchUrl = (0, import_rattail4.isFunction)(mapping2.url) ? mapping2.url((_a3 = config.url) != null ? _a3 : "") : mapping2.url === ((_b2 = config.url) != null ? _b2 : "");
|
|
298
301
|
const isMatchMethod = mapping2.method != null ? config.method === mapping2.method : true;
|
|
299
302
|
return isMatchUrl && isMatchMethod;
|
|
300
303
|
});
|
|
@@ -430,9 +433,8 @@ function responseStatusInterceptor(options) {
|
|
|
430
433
|
var import_md5 = __toESM(require("crypto-js/md5.js"), 1);
|
|
431
434
|
var import_get = __toESM(require("lodash/get.js"), 1);
|
|
432
435
|
var import_set = __toESM(require("lodash/set.js"), 1);
|
|
433
|
-
var import_minimatch3 = require("minimatch");
|
|
434
436
|
var import_qs2 = __toESM(require("qs"), 1);
|
|
435
|
-
var
|
|
437
|
+
var import_rattail5 = require("rattail");
|
|
436
438
|
function withCtxMd5(ctx, mapping) {
|
|
437
439
|
mapping.path.forEach((path) => {
|
|
438
440
|
const targetValue = (0, import_get.default)(ctx, path);
|
|
@@ -448,7 +450,7 @@ function withMd5(config, mapping) {
|
|
|
448
450
|
params: JSON.parse(JSON.stringify(params)),
|
|
449
451
|
headers: JSON.parse(JSON.stringify(headers))
|
|
450
452
|
};
|
|
451
|
-
if ((0,
|
|
453
|
+
if ((0, import_rattail5.isString)(ctx.data) && ctx.data.length > 0 && ctx.headers["Content-Type"] === "application/x-www-form-urlencoded") {
|
|
452
454
|
ctx.data = import_qs2.default.parse(ctx.data);
|
|
453
455
|
withCtxMd5(ctx, mapping);
|
|
454
456
|
ctx.data = import_qs2.default.stringify(ctx.data);
|
|
@@ -472,8 +474,8 @@ function requestMd5Interceptor(options = {}) {
|
|
|
472
474
|
const findMapping = () => {
|
|
473
475
|
var _a2;
|
|
474
476
|
return ((_a2 = options.mappings) != null ? _a2 : []).find((mapping2) => {
|
|
475
|
-
var _a3;
|
|
476
|
-
const isMatchUrl = (0,
|
|
477
|
+
var _a3, _b2;
|
|
478
|
+
const isMatchUrl = (0, import_rattail5.isFunction)(mapping2.url) ? mapping2.url((_a3 = config.url) != null ? _a3 : "") : mapping2.url === ((_b2 = config.url) != null ? _b2 : "");
|
|
477
479
|
const isMatchMethod = mapping2.method != null ? config.method === mapping2.method : true;
|
|
478
480
|
return isMatchUrl && isMatchMethod;
|
|
479
481
|
});
|
package/lib/index.d.cts
CHANGED
|
@@ -3,8 +3,13 @@ export { e as AxleInstance, A as AxleRequestConfig, b as FetchMethod, F as Fetch
|
|
|
3
3
|
import { AxiosResponse, AxiosInterceptorOptions, AxiosRequestConfig } from 'axios';
|
|
4
4
|
export { AxiosError, AxiosInstance, AxiosInterceptorOptions, AxiosRequestConfig, AxiosRequestHeaders, AxiosResponse, HeadersDefaults, InternalAxiosRequestConfig, ResponseType, isAxiosError } from 'axios';
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
type MatchPattern = string | ((options: {
|
|
7
|
+
url: string;
|
|
8
|
+
method: string;
|
|
9
|
+
status?: number;
|
|
10
|
+
}) => boolean);
|
|
11
|
+
declare function matchPattern(pattern: MatchPattern, method: string, url: string, status?: number): boolean;
|
|
12
|
+
declare function createMatcher(include?: MatchPattern[], exclude?: MatchPattern[]): (method: string, url: string, status?: number) => boolean;
|
|
8
13
|
|
|
9
14
|
interface WithResponseReturn<R> {
|
|
10
15
|
response: R | undefined;
|
|
@@ -15,14 +20,14 @@ declare function download(url: string | Blob, filename: string): void;
|
|
|
15
20
|
|
|
16
21
|
interface RequestHeadersInterceptorOptions {
|
|
17
22
|
headers?: Record<string, string> | (() => Record<string, string>);
|
|
18
|
-
include?:
|
|
19
|
-
exclude?:
|
|
23
|
+
include?: MatchPattern[];
|
|
24
|
+
exclude?: MatchPattern[];
|
|
20
25
|
axiosInterceptorOptions?: AxiosInterceptorOptions;
|
|
21
26
|
}
|
|
22
27
|
declare function requestHeadersInterceptor(options?: RequestHeadersInterceptorOptions): RequestInterceptor;
|
|
23
28
|
|
|
24
29
|
type RequestMockInterceptorMapping = {
|
|
25
|
-
url: string;
|
|
30
|
+
url: string | ((url: string) => boolean);
|
|
26
31
|
handler: (config: AxiosRequestConfig) => {
|
|
27
32
|
data: any;
|
|
28
33
|
status?: number;
|
|
@@ -33,32 +38,32 @@ type RequestMockInterceptorMapping = {
|
|
|
33
38
|
};
|
|
34
39
|
interface RequestMockInterceptorOptions {
|
|
35
40
|
mappings?: RequestMockInterceptorMapping[];
|
|
36
|
-
include?:
|
|
37
|
-
exclude?:
|
|
41
|
+
include?: MatchPattern[];
|
|
42
|
+
exclude?: MatchPattern[];
|
|
38
43
|
axiosInterceptorOptions?: AxiosInterceptorOptions;
|
|
39
44
|
}
|
|
40
45
|
declare function requestMockInterceptor(options?: RequestMockInterceptorOptions): RequestInterceptor;
|
|
41
46
|
|
|
42
47
|
interface ResponseBlobInterceptorOptions {
|
|
43
48
|
onResponse?: (response: AxiosResponse<any, any>) => any;
|
|
44
|
-
include?:
|
|
45
|
-
exclude?:
|
|
49
|
+
include?: MatchPattern[];
|
|
50
|
+
exclude?: MatchPattern[];
|
|
46
51
|
axiosInterceptorOptions?: AxiosInterceptorOptions;
|
|
47
52
|
}
|
|
48
53
|
declare function responseBlobInterceptor(options?: ResponseBlobInterceptorOptions): ResponseInterceptor;
|
|
49
54
|
|
|
50
55
|
interface ResponseTimeoutInterceptorOptions {
|
|
51
56
|
normalizeErrorCode?: string;
|
|
52
|
-
include?:
|
|
53
|
-
exclude?:
|
|
57
|
+
include?: MatchPattern[];
|
|
58
|
+
exclude?: MatchPattern[];
|
|
54
59
|
axiosInterceptorOptions?: AxiosInterceptorOptions;
|
|
55
60
|
}
|
|
56
61
|
declare function responseTimeoutInterceptor(options?: ResponseTimeoutInterceptorOptions): ResponseInterceptor;
|
|
57
62
|
|
|
58
63
|
interface ResponseRetryInterceptorOptions {
|
|
59
64
|
count?: number;
|
|
60
|
-
include?:
|
|
61
|
-
exclude?:
|
|
65
|
+
include?: MatchPattern[];
|
|
66
|
+
exclude?: MatchPattern[];
|
|
62
67
|
axiosInterceptorOptions?: AxiosInterceptorOptions;
|
|
63
68
|
}
|
|
64
69
|
declare function responseRetryInterceptor(options: ResponseRetryInterceptorOptions): ResponseInterceptor;
|
|
@@ -66,23 +71,23 @@ declare function responseRetryInterceptor(options: ResponseRetryInterceptorOptio
|
|
|
66
71
|
interface ResponseStatusInterceptorOptions {
|
|
67
72
|
validStatusHandler?: Record<number | string, (response: AxiosResponse<any, any>) => any>;
|
|
68
73
|
invalidStatusHandler?: Record<number | string, (error: any) => any>;
|
|
69
|
-
include?:
|
|
70
|
-
exclude?:
|
|
74
|
+
include?: MatchPattern[];
|
|
75
|
+
exclude?: MatchPattern[];
|
|
71
76
|
axiosInterceptorOptions?: AxiosInterceptorOptions;
|
|
72
77
|
}
|
|
73
78
|
declare function responseStatusInterceptor(options: ResponseStatusInterceptorOptions): ResponseInterceptor;
|
|
74
79
|
|
|
75
80
|
type RequestMd5InterceptorMapping = {
|
|
76
|
-
url: string;
|
|
81
|
+
url: string | ((url: string) => boolean);
|
|
77
82
|
path: string[];
|
|
78
83
|
method?: string;
|
|
79
84
|
};
|
|
80
85
|
interface RequestMd5InterceptorOptions {
|
|
81
86
|
mappings?: RequestMd5InterceptorMapping[];
|
|
82
|
-
include?:
|
|
83
|
-
exclude?:
|
|
87
|
+
include?: MatchPattern[];
|
|
88
|
+
exclude?: MatchPattern[];
|
|
84
89
|
axiosInterceptorOptions?: AxiosInterceptorOptions;
|
|
85
90
|
}
|
|
86
91
|
declare function requestMd5Interceptor(options?: RequestMd5InterceptorOptions): RequestInterceptor;
|
|
87
92
|
|
|
88
|
-
export { RequestHeadersInterceptorOptions, RequestInterceptor, RequestMd5InterceptorMapping, RequestMd5InterceptorOptions, RequestMockInterceptorMapping, RequestMockInterceptorOptions, ResponseBlobInterceptorOptions, ResponseInterceptor, ResponseRetryInterceptorOptions, ResponseStatusInterceptorOptions, ResponseTimeoutInterceptorOptions, WithResponseReturn, createMatcher, download, matchPattern, requestHeadersInterceptor, requestMd5Interceptor, requestMockInterceptor, responseBlobInterceptor, responseRetryInterceptor, responseStatusInterceptor, responseTimeoutInterceptor, withResponse };
|
|
93
|
+
export { MatchPattern, RequestHeadersInterceptorOptions, RequestInterceptor, RequestMd5InterceptorMapping, RequestMd5InterceptorOptions, RequestMockInterceptorMapping, RequestMockInterceptorOptions, ResponseBlobInterceptorOptions, ResponseInterceptor, ResponseRetryInterceptorOptions, ResponseStatusInterceptorOptions, ResponseTimeoutInterceptorOptions, WithResponseReturn, createMatcher, download, matchPattern, requestHeadersInterceptor, requestMd5Interceptor, requestMockInterceptor, responseBlobInterceptor, responseRetryInterceptor, responseStatusInterceptor, responseTimeoutInterceptor, withResponse };
|
package/lib/index.d.ts
CHANGED
|
@@ -3,8 +3,13 @@ export { e as AxleInstance, A as AxleRequestConfig, b as FetchMethod, F as Fetch
|
|
|
3
3
|
import { AxiosResponse, AxiosInterceptorOptions, AxiosRequestConfig } from 'axios';
|
|
4
4
|
export { AxiosError, AxiosInstance, AxiosInterceptorOptions, AxiosRequestConfig, AxiosRequestHeaders, AxiosResponse, HeadersDefaults, InternalAxiosRequestConfig, ResponseType, isAxiosError } from 'axios';
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
type MatchPattern = string | ((options: {
|
|
7
|
+
url: string;
|
|
8
|
+
method: string;
|
|
9
|
+
status?: number;
|
|
10
|
+
}) => boolean);
|
|
11
|
+
declare function matchPattern(pattern: MatchPattern, method: string, url: string, status?: number): boolean;
|
|
12
|
+
declare function createMatcher(include?: MatchPattern[], exclude?: MatchPattern[]): (method: string, url: string, status?: number) => boolean;
|
|
8
13
|
|
|
9
14
|
interface WithResponseReturn<R> {
|
|
10
15
|
response: R | undefined;
|
|
@@ -15,14 +20,14 @@ declare function download(url: string | Blob, filename: string): void;
|
|
|
15
20
|
|
|
16
21
|
interface RequestHeadersInterceptorOptions {
|
|
17
22
|
headers?: Record<string, string> | (() => Record<string, string>);
|
|
18
|
-
include?:
|
|
19
|
-
exclude?:
|
|
23
|
+
include?: MatchPattern[];
|
|
24
|
+
exclude?: MatchPattern[];
|
|
20
25
|
axiosInterceptorOptions?: AxiosInterceptorOptions;
|
|
21
26
|
}
|
|
22
27
|
declare function requestHeadersInterceptor(options?: RequestHeadersInterceptorOptions): RequestInterceptor;
|
|
23
28
|
|
|
24
29
|
type RequestMockInterceptorMapping = {
|
|
25
|
-
url: string;
|
|
30
|
+
url: string | ((url: string) => boolean);
|
|
26
31
|
handler: (config: AxiosRequestConfig) => {
|
|
27
32
|
data: any;
|
|
28
33
|
status?: number;
|
|
@@ -33,32 +38,32 @@ type RequestMockInterceptorMapping = {
|
|
|
33
38
|
};
|
|
34
39
|
interface RequestMockInterceptorOptions {
|
|
35
40
|
mappings?: RequestMockInterceptorMapping[];
|
|
36
|
-
include?:
|
|
37
|
-
exclude?:
|
|
41
|
+
include?: MatchPattern[];
|
|
42
|
+
exclude?: MatchPattern[];
|
|
38
43
|
axiosInterceptorOptions?: AxiosInterceptorOptions;
|
|
39
44
|
}
|
|
40
45
|
declare function requestMockInterceptor(options?: RequestMockInterceptorOptions): RequestInterceptor;
|
|
41
46
|
|
|
42
47
|
interface ResponseBlobInterceptorOptions {
|
|
43
48
|
onResponse?: (response: AxiosResponse<any, any>) => any;
|
|
44
|
-
include?:
|
|
45
|
-
exclude?:
|
|
49
|
+
include?: MatchPattern[];
|
|
50
|
+
exclude?: MatchPattern[];
|
|
46
51
|
axiosInterceptorOptions?: AxiosInterceptorOptions;
|
|
47
52
|
}
|
|
48
53
|
declare function responseBlobInterceptor(options?: ResponseBlobInterceptorOptions): ResponseInterceptor;
|
|
49
54
|
|
|
50
55
|
interface ResponseTimeoutInterceptorOptions {
|
|
51
56
|
normalizeErrorCode?: string;
|
|
52
|
-
include?:
|
|
53
|
-
exclude?:
|
|
57
|
+
include?: MatchPattern[];
|
|
58
|
+
exclude?: MatchPattern[];
|
|
54
59
|
axiosInterceptorOptions?: AxiosInterceptorOptions;
|
|
55
60
|
}
|
|
56
61
|
declare function responseTimeoutInterceptor(options?: ResponseTimeoutInterceptorOptions): ResponseInterceptor;
|
|
57
62
|
|
|
58
63
|
interface ResponseRetryInterceptorOptions {
|
|
59
64
|
count?: number;
|
|
60
|
-
include?:
|
|
61
|
-
exclude?:
|
|
65
|
+
include?: MatchPattern[];
|
|
66
|
+
exclude?: MatchPattern[];
|
|
62
67
|
axiosInterceptorOptions?: AxiosInterceptorOptions;
|
|
63
68
|
}
|
|
64
69
|
declare function responseRetryInterceptor(options: ResponseRetryInterceptorOptions): ResponseInterceptor;
|
|
@@ -66,23 +71,23 @@ declare function responseRetryInterceptor(options: ResponseRetryInterceptorOptio
|
|
|
66
71
|
interface ResponseStatusInterceptorOptions {
|
|
67
72
|
validStatusHandler?: Record<number | string, (response: AxiosResponse<any, any>) => any>;
|
|
68
73
|
invalidStatusHandler?: Record<number | string, (error: any) => any>;
|
|
69
|
-
include?:
|
|
70
|
-
exclude?:
|
|
74
|
+
include?: MatchPattern[];
|
|
75
|
+
exclude?: MatchPattern[];
|
|
71
76
|
axiosInterceptorOptions?: AxiosInterceptorOptions;
|
|
72
77
|
}
|
|
73
78
|
declare function responseStatusInterceptor(options: ResponseStatusInterceptorOptions): ResponseInterceptor;
|
|
74
79
|
|
|
75
80
|
type RequestMd5InterceptorMapping = {
|
|
76
|
-
url: string;
|
|
81
|
+
url: string | ((url: string) => boolean);
|
|
77
82
|
path: string[];
|
|
78
83
|
method?: string;
|
|
79
84
|
};
|
|
80
85
|
interface RequestMd5InterceptorOptions {
|
|
81
86
|
mappings?: RequestMd5InterceptorMapping[];
|
|
82
|
-
include?:
|
|
83
|
-
exclude?:
|
|
87
|
+
include?: MatchPattern[];
|
|
88
|
+
exclude?: MatchPattern[];
|
|
84
89
|
axiosInterceptorOptions?: AxiosInterceptorOptions;
|
|
85
90
|
}
|
|
86
91
|
declare function requestMd5Interceptor(options?: RequestMd5InterceptorOptions): RequestInterceptor;
|
|
87
92
|
|
|
88
|
-
export { RequestHeadersInterceptorOptions, RequestInterceptor, RequestMd5InterceptorMapping, RequestMd5InterceptorOptions, RequestMockInterceptorMapping, RequestMockInterceptorOptions, ResponseBlobInterceptorOptions, ResponseInterceptor, ResponseRetryInterceptorOptions, ResponseStatusInterceptorOptions, ResponseTimeoutInterceptorOptions, WithResponseReturn, createMatcher, download, matchPattern, requestHeadersInterceptor, requestMd5Interceptor, requestMockInterceptor, responseBlobInterceptor, responseRetryInterceptor, responseStatusInterceptor, responseTimeoutInterceptor, withResponse };
|
|
93
|
+
export { MatchPattern, RequestHeadersInterceptorOptions, RequestInterceptor, RequestMd5InterceptorMapping, RequestMd5InterceptorOptions, RequestMockInterceptorMapping, RequestMockInterceptorOptions, ResponseBlobInterceptorOptions, ResponseInterceptor, ResponseRetryInterceptorOptions, ResponseStatusInterceptorOptions, ResponseTimeoutInterceptorOptions, WithResponseReturn, createMatcher, download, matchPattern, requestHeadersInterceptor, requestMd5Interceptor, requestMockInterceptor, responseBlobInterceptor, responseRetryInterceptor, responseStatusInterceptor, responseTimeoutInterceptor, withResponse };
|
package/lib/index.js
CHANGED
|
@@ -120,12 +120,15 @@ function createAxle(config = {}) {
|
|
|
120
120
|
}
|
|
121
121
|
|
|
122
122
|
// src/matcher.ts
|
|
123
|
-
import {
|
|
123
|
+
import { isFunction } from "rattail";
|
|
124
124
|
function matchPattern(pattern, method, url, status) {
|
|
125
|
+
if (isFunction(pattern)) {
|
|
126
|
+
return pattern({ url, method, status });
|
|
127
|
+
}
|
|
125
128
|
if (pattern.startsWith("status:")) {
|
|
126
129
|
return pattern.replace("status:", "").trim() === String(status);
|
|
127
130
|
}
|
|
128
|
-
return pattern.startsWith("method:") ? pattern.replace("method:", "").trim() === method :
|
|
131
|
+
return pattern.startsWith("method:") ? pattern.replace("method:", "").trim() === method : url === pattern;
|
|
129
132
|
}
|
|
130
133
|
function createMatcher(include, exclude) {
|
|
131
134
|
function matcher(method, url, status) {
|
|
@@ -182,7 +185,7 @@ function download(url, filename) {
|
|
|
182
185
|
}
|
|
183
186
|
|
|
184
187
|
// src/interceptors/requestHeadersInterceptor.ts
|
|
185
|
-
import { isFunction } from "rattail";
|
|
188
|
+
import { isFunction as isFunction2 } from "rattail";
|
|
186
189
|
function requestHeadersInterceptor(options = {}) {
|
|
187
190
|
const { headers: headersOrGetter } = options;
|
|
188
191
|
return {
|
|
@@ -192,7 +195,7 @@ function requestHeadersInterceptor(options = {}) {
|
|
|
192
195
|
if (!matcher((_a = config.method) != null ? _a : "", (_b = config.url) != null ? _b : "")) {
|
|
193
196
|
return config;
|
|
194
197
|
}
|
|
195
|
-
const headers = (_c =
|
|
198
|
+
const headers = (_c = isFunction2(headersOrGetter) ? headersOrGetter() : headersOrGetter) != null ? _c : {};
|
|
196
199
|
Object.entries(headers).forEach(([key, value]) => {
|
|
197
200
|
config.headers[key] = value;
|
|
198
201
|
});
|
|
@@ -205,7 +208,7 @@ function requestHeadersInterceptor(options = {}) {
|
|
|
205
208
|
|
|
206
209
|
// src/interceptors/requestMockInterceptor.ts
|
|
207
210
|
import { AxiosError as AxiosError2 } from "axios";
|
|
208
|
-
import {
|
|
211
|
+
import { isFunction as isFunction3 } from "rattail";
|
|
209
212
|
function settle(response, resolve, reject, delay = 0) {
|
|
210
213
|
if (delay > 0) {
|
|
211
214
|
setTimeout(() => {
|
|
@@ -230,8 +233,8 @@ function requestMockInterceptor(options = {}) {
|
|
|
230
233
|
const findMapping = () => {
|
|
231
234
|
var _a2;
|
|
232
235
|
return ((_a2 = options.mappings) != null ? _a2 : []).find((mapping2) => {
|
|
233
|
-
var _a3;
|
|
234
|
-
const isMatchUrl =
|
|
236
|
+
var _a3, _b2;
|
|
237
|
+
const isMatchUrl = isFunction3(mapping2.url) ? mapping2.url((_a3 = config.url) != null ? _a3 : "") : mapping2.url === ((_b2 = config.url) != null ? _b2 : "");
|
|
235
238
|
const isMatchMethod = mapping2.method != null ? config.method === mapping2.method : true;
|
|
236
239
|
return isMatchUrl && isMatchMethod;
|
|
237
240
|
});
|
|
@@ -367,9 +370,8 @@ function responseStatusInterceptor(options) {
|
|
|
367
370
|
import md5 from "crypto-js/md5.js";
|
|
368
371
|
import get from "lodash/get.js";
|
|
369
372
|
import set from "lodash/set.js";
|
|
370
|
-
import { minimatch as minimatch3 } from "minimatch";
|
|
371
373
|
import qs2 from "qs";
|
|
372
|
-
import { isString } from "rattail";
|
|
374
|
+
import { isFunction as isFunction4, isString } from "rattail";
|
|
373
375
|
function withCtxMd5(ctx, mapping) {
|
|
374
376
|
mapping.path.forEach((path) => {
|
|
375
377
|
const targetValue = get(ctx, path);
|
|
@@ -409,8 +411,8 @@ function requestMd5Interceptor(options = {}) {
|
|
|
409
411
|
const findMapping = () => {
|
|
410
412
|
var _a2;
|
|
411
413
|
return ((_a2 = options.mappings) != null ? _a2 : []).find((mapping2) => {
|
|
412
|
-
var _a3;
|
|
413
|
-
const isMatchUrl =
|
|
414
|
+
var _a3, _b2;
|
|
415
|
+
const isMatchUrl = isFunction4(mapping2.url) ? mapping2.url((_a3 = config.url) != null ? _a3 : "") : mapping2.url === ((_b2 = config.url) != null ? _b2 : "");
|
|
414
416
|
const isMatchMethod = mapping2.method != null ? config.method === mapping2.method : true;
|
|
415
417
|
return isMatchUrl && isMatchMethod;
|
|
416
418
|
});
|