@varlet/axle 0.2.9 → 0.2.11
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 +5 -4
- package/README.zh-CN.md +5 -4
- package/es/{chunk-ZSUG26OQ.js → chunk-KLNSLHAC.js} +5 -1
- package/es/index.d.ts +19 -2
- package/es/index.js +67 -8
- package/es/use.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -266,10 +266,11 @@ axle.useResponseInterceptor(
|
|
|
266
266
|
|
|
267
267
|
| Name | Description |
|
|
268
268
|
| --- | --- |
|
|
269
|
-
| [requestHeadersInterceptor](https://github.com/varletjs/axle/blob/main/src/interceptors/requestHeadersInterceptor.
|
|
270
|
-
| [
|
|
271
|
-
| [
|
|
272
|
-
| [
|
|
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
|
+
| [responseBlobInterceptor](https://github.com/varletjs/axle/blob/main/src/interceptors/examples/responseBlobInterceptor.md) | Used to intercept blob type |
|
|
273
|
+
| [responseTimeoutInterceptor](https://github.com/varletjs/axle/blob/main/src/interceptors/examples/responseTimeoutInterceptor.md) | Used to abnormal timeout |
|
|
273
274
|
|
|
274
275
|
## Vue Composition API
|
|
275
276
|
|
package/README.zh-CN.md
CHANGED
|
@@ -266,10 +266,11 @@ axle.useResponseInterceptor(
|
|
|
266
266
|
|
|
267
267
|
| 名称 | 描述 |
|
|
268
268
|
| --- | --- |
|
|
269
|
-
| [requestHeadersInterceptor](https://github.com/varletjs/axle/blob/main/src/interceptors/requestHeadersInterceptor.
|
|
270
|
-
| [
|
|
271
|
-
| [
|
|
272
|
-
| [
|
|
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
|
+
| [responseBlobInterceptor](https://github.com/varletjs/axle/blob/main/src/interceptors/examples/responseBlobInterceptor.md) | 用于拦截 blob 类型 |
|
|
273
|
+
| [responseTimeoutInterceptor](https://github.com/varletjs/axle/blob/main/src/interceptors/examples/responseTimeoutInterceptor.md) | 用于归一化超时异常 |
|
|
273
274
|
|
|
274
275
|
## Vue 组合式 API
|
|
275
276
|
|
|
@@ -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,4 @@ 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
|
+
export { RequestHeadersInterceptorOptions, RequestInterceptor, RequestMockInterceptorMappingValue, RequestMockInterceptorOptions, ResponseBlobInterceptorOptions, ResponseInterceptor, ResponseRetryInterceptorOptions, ResponseTimeoutInterceptorOptions, createMatcher, matchPattern, requestHeadersInterceptor, requestMockInterceptor, responseBlobInterceptor, responseRetryInterceptor, responseTimeoutInterceptor };
|
package/es/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
|
+
__spreadProps,
|
|
2
3
|
__spreadValues
|
|
3
|
-
} from "./chunk-
|
|
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 {
|
|
@@ -204,15 +265,12 @@ function responseRetryInterceptor(options) {
|
|
|
204
265
|
async onRejected(error) {
|
|
205
266
|
var _a, _b;
|
|
206
267
|
const matcher = createMatcher(options.include, options.exclude);
|
|
207
|
-
if (!matcher((_a = error.config.method) != null ? _a : "", (_b = error.config.url) != null ? _b : "")) {
|
|
268
|
+
if (!matcher((_a = error.config.method) != null ? _a : "", (_b = error.config.url) != null ? _b : "") || isCancel(error)) {
|
|
208
269
|
return Promise.reject(error);
|
|
209
270
|
}
|
|
210
271
|
const { count = 1 } = options;
|
|
211
272
|
let retryCount = 0;
|
|
212
|
-
|
|
213
|
-
return Promise.reject(error);
|
|
214
|
-
}
|
|
215
|
-
async function loop() {
|
|
273
|
+
async function retry() {
|
|
216
274
|
try {
|
|
217
275
|
retryCount++;
|
|
218
276
|
return await axios2.create()(error.config);
|
|
@@ -220,10 +278,10 @@ function responseRetryInterceptor(options) {
|
|
|
220
278
|
if (retryCount === count) {
|
|
221
279
|
return Promise.reject(error2);
|
|
222
280
|
}
|
|
223
|
-
return
|
|
281
|
+
return retry();
|
|
224
282
|
}
|
|
225
283
|
}
|
|
226
|
-
return
|
|
284
|
+
return retry();
|
|
227
285
|
},
|
|
228
286
|
options: options.axiosInterceptorOptions
|
|
229
287
|
};
|
|
@@ -236,6 +294,7 @@ export {
|
|
|
236
294
|
download,
|
|
237
295
|
matchPattern,
|
|
238
296
|
requestHeadersInterceptor,
|
|
297
|
+
requestMockInterceptor,
|
|
239
298
|
responseBlobInterceptor,
|
|
240
299
|
responseRetryInterceptor,
|
|
241
300
|
responseTimeoutInterceptor
|
package/es/use.js
CHANGED