@varlet/axle 1.0.1 → 1.1.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/dist/api.d.mts +27 -0
- package/dist/api.mjs +46 -0
- package/dist/index.d.mts +91 -0
- package/dist/index.mjs +324 -0
- package/dist/instance-ar9qY3vp.d.mts +64 -0
- package/dist/use.d.mts +90 -0
- package/dist/use.mjs +271 -0
- package/package.json +48 -35
- package/lib/api.cjs +0 -89
- package/lib/api.d.cts +0 -28
- package/lib/api.d.ts +0 -28
- package/lib/api.js +0 -41
- package/lib/chunk-G2ADBYYC.js +0 -37
- package/lib/index.cjs +0 -490
- package/lib/index.d.cts +0 -85
- package/lib/index.d.ts +0 -85
- package/lib/index.js +0 -427
- package/lib/instance-857fe07c.d.ts +0 -64
- package/lib/use.cjs +0 -351
- package/lib/use.d.cts +0 -85
- package/lib/use.d.ts +0 -85
- package/lib/use.js +0 -314
package/lib/index.cjs
DELETED
|
@@ -1,490 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __defProps = Object.defineProperties;
|
|
5
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
7
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
8
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
9
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
10
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
11
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
12
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
13
|
-
var __spreadValues = (a, b) => {
|
|
14
|
-
for (var prop in b || (b = {}))
|
|
15
|
-
if (__hasOwnProp.call(b, prop))
|
|
16
|
-
__defNormalProp(a, prop, b[prop]);
|
|
17
|
-
if (__getOwnPropSymbols)
|
|
18
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
19
|
-
if (__propIsEnum.call(b, prop))
|
|
20
|
-
__defNormalProp(a, prop, b[prop]);
|
|
21
|
-
}
|
|
22
|
-
return a;
|
|
23
|
-
};
|
|
24
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
25
|
-
var __export = (target, all) => {
|
|
26
|
-
for (var name in all)
|
|
27
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
28
|
-
};
|
|
29
|
-
var __copyProps = (to, from, except, desc) => {
|
|
30
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
31
|
-
for (let key of __getOwnPropNames(from))
|
|
32
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
33
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
34
|
-
}
|
|
35
|
-
return to;
|
|
36
|
-
};
|
|
37
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
38
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
39
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
40
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
41
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
42
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
43
|
-
mod
|
|
44
|
-
));
|
|
45
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
46
|
-
|
|
47
|
-
// src/index.ts
|
|
48
|
-
var src_exports = {};
|
|
49
|
-
__export(src_exports, {
|
|
50
|
-
AxiosError: () => import_axios2.AxiosError,
|
|
51
|
-
createAxle: () => createAxle,
|
|
52
|
-
createFetchRunner: () => createFetchRunner,
|
|
53
|
-
createMatcher: () => createMatcher,
|
|
54
|
-
createModifyRunner: () => createModifyRunner,
|
|
55
|
-
download: () => download,
|
|
56
|
-
isAxiosError: () => import_axios2.isAxiosError,
|
|
57
|
-
matchPattern: () => matchPattern,
|
|
58
|
-
requestHeadersInterceptor: () => requestHeadersInterceptor,
|
|
59
|
-
requestMd5Interceptor: () => requestMd5Interceptor,
|
|
60
|
-
requestMockInterceptor: () => requestMockInterceptor,
|
|
61
|
-
responseBlobInterceptor: () => responseBlobInterceptor,
|
|
62
|
-
responseRetryInterceptor: () => responseRetryInterceptor,
|
|
63
|
-
responseStatusInterceptor: () => responseStatusInterceptor,
|
|
64
|
-
withResponse: () => withResponse
|
|
65
|
-
});
|
|
66
|
-
module.exports = __toCommonJS(src_exports);
|
|
67
|
-
|
|
68
|
-
// src/instance.ts
|
|
69
|
-
var import_axios = __toESM(require("axios"), 1);
|
|
70
|
-
var import_qs = __toESM(require("qs"), 1);
|
|
71
|
-
|
|
72
|
-
// src/utils/index.ts
|
|
73
|
-
function isFormData(value) {
|
|
74
|
-
return typeof FormData !== "undefined" && value instanceof FormData;
|
|
75
|
-
}
|
|
76
|
-
function formDataToObject(formData) {
|
|
77
|
-
const normalizedData = {};
|
|
78
|
-
formData.forEach((value, key) => {
|
|
79
|
-
normalizedData[key] = value;
|
|
80
|
-
});
|
|
81
|
-
return normalizedData;
|
|
82
|
-
}
|
|
83
|
-
function objectToFormData(object) {
|
|
84
|
-
const formData = new FormData();
|
|
85
|
-
Object.keys(object).forEach((key) => formData.append(key, object[key]));
|
|
86
|
-
return formData;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// src/instance.ts
|
|
90
|
-
var import_axios2 = require("axios");
|
|
91
|
-
function createFetchRunner(service, method, responseType) {
|
|
92
|
-
return function(url, params, config) {
|
|
93
|
-
return service[method](url, __spreadValues({
|
|
94
|
-
params,
|
|
95
|
-
responseType
|
|
96
|
-
}, config));
|
|
97
|
-
};
|
|
98
|
-
}
|
|
99
|
-
function createModifyRunner(service, method, contentType) {
|
|
100
|
-
return function(url, params, config) {
|
|
101
|
-
let normalizedParams = params != null ? params : {};
|
|
102
|
-
if (contentType === "application/x-www-form-urlencoded") {
|
|
103
|
-
normalizedParams = import_qs.default.stringify(normalizedParams);
|
|
104
|
-
}
|
|
105
|
-
if (contentType === "multipart/form-data") {
|
|
106
|
-
normalizedParams = objectToFormData(normalizedParams);
|
|
107
|
-
}
|
|
108
|
-
return service[method](url, normalizedParams, __spreadValues({
|
|
109
|
-
headers: {
|
|
110
|
-
"Content-Type": contentType
|
|
111
|
-
}
|
|
112
|
-
}, config));
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
function createAxle(config = {}) {
|
|
116
|
-
const service = import_axios.default.create(config);
|
|
117
|
-
function getHeaders() {
|
|
118
|
-
return service.defaults.headers.common;
|
|
119
|
-
}
|
|
120
|
-
function setHeader(key, value) {
|
|
121
|
-
;
|
|
122
|
-
service.defaults.headers.common[key] = value;
|
|
123
|
-
}
|
|
124
|
-
function removeHeader(key) {
|
|
125
|
-
if (typeof key === "string") {
|
|
126
|
-
Reflect.deleteProperty(service.defaults.headers.common, key);
|
|
127
|
-
return;
|
|
128
|
-
}
|
|
129
|
-
key.forEach((k) => Reflect.deleteProperty(service.defaults.headers.common, k));
|
|
130
|
-
}
|
|
131
|
-
function useRequestInterceptor(...interceptors) {
|
|
132
|
-
interceptors.forEach((interceptor) => {
|
|
133
|
-
service.interceptors.request.use(interceptor.onFulfilled, interceptor.onRejected, interceptor.options);
|
|
134
|
-
});
|
|
135
|
-
}
|
|
136
|
-
function useResponseInterceptor(...interceptors) {
|
|
137
|
-
interceptors.forEach((interceptor) => {
|
|
138
|
-
service.interceptors.response.use(interceptor.onFulfilled, interceptor.onRejected, interceptor.options);
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
return {
|
|
142
|
-
get: createFetchRunner(service, "get", "json"),
|
|
143
|
-
getBlob: createFetchRunner(service, "get", "blob"),
|
|
144
|
-
getDocument: createFetchRunner(service, "get", "document"),
|
|
145
|
-
getArrayBuffer: createFetchRunner(service, "get", "arraybuffer"),
|
|
146
|
-
getText: createFetchRunner(service, "get", "text"),
|
|
147
|
-
getStream: createFetchRunner(service, "get", "stream"),
|
|
148
|
-
head: createFetchRunner(service, "head", "json"),
|
|
149
|
-
headBlob: createFetchRunner(service, "head", "blob"),
|
|
150
|
-
headDocument: createFetchRunner(service, "head", "document"),
|
|
151
|
-
headArrayBuffer: createFetchRunner(service, "head", "arraybuffer"),
|
|
152
|
-
headText: createFetchRunner(service, "head", "text"),
|
|
153
|
-
headStream: createFetchRunner(service, "head", "stream"),
|
|
154
|
-
options: createFetchRunner(service, "options", "json"),
|
|
155
|
-
optionsBlob: createFetchRunner(service, "options", "blob"),
|
|
156
|
-
optionsDocument: createFetchRunner(service, "options", "document"),
|
|
157
|
-
optionsArrayBuffer: createFetchRunner(service, "options", "arraybuffer"),
|
|
158
|
-
optionsText: createFetchRunner(service, "options", "text"),
|
|
159
|
-
optionsStream: createFetchRunner(service, "options", "stream"),
|
|
160
|
-
delete: createFetchRunner(service, "delete", "json"),
|
|
161
|
-
deleteBlob: createFetchRunner(service, "delete", "blob"),
|
|
162
|
-
deleteDocument: createFetchRunner(service, "delete", "document"),
|
|
163
|
-
deleteArrayBuffer: createFetchRunner(service, "delete", "arraybuffer"),
|
|
164
|
-
deleteText: createFetchRunner(service, "delete", "text"),
|
|
165
|
-
deleteStream: createFetchRunner(service, "delete", "stream"),
|
|
166
|
-
post: createModifyRunner(service, "post", "application/json"),
|
|
167
|
-
postUrlEncode: createModifyRunner(service, "post", "application/x-www-form-urlencoded"),
|
|
168
|
-
postMultipart: createModifyRunner(service, "post", "multipart/form-data"),
|
|
169
|
-
put: createModifyRunner(service, "put", "application/json"),
|
|
170
|
-
putUrlEncode: createModifyRunner(service, "put", "application/x-www-form-urlencoded"),
|
|
171
|
-
putMultipart: createModifyRunner(service, "put", "multipart/form-data"),
|
|
172
|
-
patch: createModifyRunner(service, "patch", "application/json"),
|
|
173
|
-
patchUrlEncode: createModifyRunner(service, "patch", "application/x-www-form-urlencoded"),
|
|
174
|
-
patchMultipart: createModifyRunner(service, "patch", "multipart/form-data"),
|
|
175
|
-
getHeaders,
|
|
176
|
-
setHeader,
|
|
177
|
-
removeHeader,
|
|
178
|
-
useRequestInterceptor,
|
|
179
|
-
useResponseInterceptor,
|
|
180
|
-
axios: service
|
|
181
|
-
};
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
// src/matcher.ts
|
|
185
|
-
var import_rattail = require("rattail");
|
|
186
|
-
function matchPattern(pattern, method, url, status) {
|
|
187
|
-
if ((0, import_rattail.isFunction)(pattern)) {
|
|
188
|
-
return pattern({ url, method, status });
|
|
189
|
-
}
|
|
190
|
-
if (pattern.startsWith("status:")) {
|
|
191
|
-
return pattern.replace("status:", "").trim() === String(status);
|
|
192
|
-
}
|
|
193
|
-
return pattern.startsWith("method:") ? pattern.replace("method:", "").trim() === method : url === pattern;
|
|
194
|
-
}
|
|
195
|
-
function createMatcher(include, exclude) {
|
|
196
|
-
function matcher(method, url, status) {
|
|
197
|
-
if (!include && !exclude) {
|
|
198
|
-
return true;
|
|
199
|
-
}
|
|
200
|
-
const isExclude = (exclude != null ? exclude : []).some((pattern) => matchPattern(pattern, method, url, status));
|
|
201
|
-
if (isExclude) {
|
|
202
|
-
return false;
|
|
203
|
-
}
|
|
204
|
-
if (!include) {
|
|
205
|
-
return true;
|
|
206
|
-
}
|
|
207
|
-
const isInclude = (include != null ? include : []).some((pattern) => matchPattern(pattern, method, url, status));
|
|
208
|
-
return isInclude;
|
|
209
|
-
}
|
|
210
|
-
return matcher;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
// src/helper.ts
|
|
214
|
-
var import_axios3 = require("axios");
|
|
215
|
-
var import_rattail2 = require("rattail");
|
|
216
|
-
function withResponse(promise) {
|
|
217
|
-
return new Promise((resolve, reject) => {
|
|
218
|
-
promise.then((response) => {
|
|
219
|
-
resolve({
|
|
220
|
-
response,
|
|
221
|
-
errorResponse: void 0
|
|
222
|
-
});
|
|
223
|
-
}).catch((error) => {
|
|
224
|
-
if ((0, import_axios3.isAxiosError)(error)) {
|
|
225
|
-
resolve({
|
|
226
|
-
response: void 0,
|
|
227
|
-
errorResponse: error.response
|
|
228
|
-
});
|
|
229
|
-
} else {
|
|
230
|
-
reject(error);
|
|
231
|
-
}
|
|
232
|
-
});
|
|
233
|
-
});
|
|
234
|
-
}
|
|
235
|
-
function download(url, filename) {
|
|
236
|
-
if (!(0, import_rattail2.inBrowser)()) {
|
|
237
|
-
return;
|
|
238
|
-
}
|
|
239
|
-
const a = document.createElement("a");
|
|
240
|
-
a.download = filename;
|
|
241
|
-
a.style.display = "none";
|
|
242
|
-
a.href = typeof url === "string" ? url : URL.createObjectURL(url);
|
|
243
|
-
document.body.appendChild(a);
|
|
244
|
-
a.click();
|
|
245
|
-
URL.revokeObjectURL(a.href);
|
|
246
|
-
document.body.removeChild(a);
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
// src/interceptors/requestHeadersInterceptor.ts
|
|
250
|
-
var import_rattail3 = require("rattail");
|
|
251
|
-
function requestHeadersInterceptor(options = {}) {
|
|
252
|
-
const { headers: headersOrGetter } = options;
|
|
253
|
-
return {
|
|
254
|
-
onFulfilled(config) {
|
|
255
|
-
var _a, _b, _c;
|
|
256
|
-
const matcher = createMatcher(options.include, options.exclude);
|
|
257
|
-
if (!matcher((_a = config.method) != null ? _a : "", (_b = config.url) != null ? _b : "")) {
|
|
258
|
-
return config;
|
|
259
|
-
}
|
|
260
|
-
const headers = (_c = (0, import_rattail3.isFunction)(headersOrGetter) ? headersOrGetter() : headersOrGetter) != null ? _c : {};
|
|
261
|
-
Object.entries(headers).forEach(([key, value]) => {
|
|
262
|
-
config.headers[key] = value;
|
|
263
|
-
});
|
|
264
|
-
return config;
|
|
265
|
-
},
|
|
266
|
-
onRejected: (error) => Promise.reject(error),
|
|
267
|
-
options: options.axiosInterceptorOptions
|
|
268
|
-
};
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
// src/interceptors/requestMockInterceptor.ts
|
|
272
|
-
var import_axios4 = require("axios");
|
|
273
|
-
var import_rattail4 = require("rattail");
|
|
274
|
-
function settle(response, resolve, reject, delay = 0) {
|
|
275
|
-
if (delay > 0) {
|
|
276
|
-
setTimeout(() => {
|
|
277
|
-
settle(response, resolve, reject);
|
|
278
|
-
}, delay);
|
|
279
|
-
return;
|
|
280
|
-
}
|
|
281
|
-
if (!response.config.validateStatus || response.config.validateStatus(response.status)) {
|
|
282
|
-
resolve(response);
|
|
283
|
-
} else {
|
|
284
|
-
reject(new import_axios4.AxiosError(`Request failed with status code ${response.status}`, void 0, response.config, response));
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
function requestMockInterceptor(options = {}) {
|
|
288
|
-
return {
|
|
289
|
-
onFulfilled(config) {
|
|
290
|
-
var _a, _b;
|
|
291
|
-
const matcher = createMatcher(options.include, options.exclude);
|
|
292
|
-
if (!matcher((_a = config.method) != null ? _a : "", (_b = config.url) != null ? _b : "")) {
|
|
293
|
-
return config;
|
|
294
|
-
}
|
|
295
|
-
const findMapping = () => {
|
|
296
|
-
var _a2;
|
|
297
|
-
return ((_a2 = options.mappings) != null ? _a2 : []).find((mapping2) => {
|
|
298
|
-
var _a3, _b2;
|
|
299
|
-
const isMatchUrl = (0, import_rattail4.isFunction)(mapping2.url) ? mapping2.url((_a3 = config.url) != null ? _a3 : "") : mapping2.url === ((_b2 = config.url) != null ? _b2 : "");
|
|
300
|
-
const isMatchMethod = mapping2.method != null ? config.method === mapping2.method : true;
|
|
301
|
-
return isMatchUrl && isMatchMethod;
|
|
302
|
-
});
|
|
303
|
-
};
|
|
304
|
-
const mapping = findMapping();
|
|
305
|
-
if (!mapping) {
|
|
306
|
-
return config;
|
|
307
|
-
}
|
|
308
|
-
config.adapter = () => {
|
|
309
|
-
var _a2, _b2;
|
|
310
|
-
const partialResponse = mapping.handler(config);
|
|
311
|
-
const response = __spreadProps(__spreadValues({}, partialResponse), {
|
|
312
|
-
headers: config.headers,
|
|
313
|
-
config,
|
|
314
|
-
request: {},
|
|
315
|
-
data: partialResponse.data,
|
|
316
|
-
status: (_a2 = partialResponse.status) != null ? _a2 : 200,
|
|
317
|
-
statusText: (_b2 = partialResponse.statusText) != null ? _b2 : "OK"
|
|
318
|
-
});
|
|
319
|
-
return new Promise((resolve, reject) => {
|
|
320
|
-
settle(response, resolve, reject, mapping.delay);
|
|
321
|
-
});
|
|
322
|
-
};
|
|
323
|
-
return config;
|
|
324
|
-
},
|
|
325
|
-
onRejected: (error) => Promise.reject(error),
|
|
326
|
-
options: options.axiosInterceptorOptions
|
|
327
|
-
};
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
// src/interceptors/responseBlobInterceptor.ts
|
|
331
|
-
function responseBlobInterceptor(options = {}) {
|
|
332
|
-
return {
|
|
333
|
-
onFulfilled(response) {
|
|
334
|
-
var _a, _b, _c, _d;
|
|
335
|
-
const matcher = createMatcher(options.include, options.exclude);
|
|
336
|
-
if (!matcher((_a = response.config.method) != null ? _a : "", (_b = response.config.url) != null ? _b : "", response.status)) {
|
|
337
|
-
return response;
|
|
338
|
-
}
|
|
339
|
-
if (response.request.responseType === "blob") {
|
|
340
|
-
return (_d = (_c = options.onResponse) == null ? void 0 : _c.call(options, response)) != null ? _d : response;
|
|
341
|
-
}
|
|
342
|
-
return response;
|
|
343
|
-
},
|
|
344
|
-
onRejected: (error) => Promise.reject(error),
|
|
345
|
-
options: options.axiosInterceptorOptions
|
|
346
|
-
};
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
// src/interceptors/responseRetryInterceptor.ts
|
|
350
|
-
var import_axios5 = __toESM(require("axios"), 1);
|
|
351
|
-
function responseRetryInterceptor(options) {
|
|
352
|
-
return {
|
|
353
|
-
onFulfilled: (response) => response,
|
|
354
|
-
onRejected(error) {
|
|
355
|
-
var _a, _b, _c;
|
|
356
|
-
const matcher = createMatcher(options.include, options.exclude);
|
|
357
|
-
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) || (0, import_axios5.isCancel)(error)) {
|
|
358
|
-
return Promise.reject(error);
|
|
359
|
-
}
|
|
360
|
-
const { count = 1 } = options;
|
|
361
|
-
let retryCount = 0;
|
|
362
|
-
async function retry() {
|
|
363
|
-
try {
|
|
364
|
-
retryCount++;
|
|
365
|
-
return await import_axios5.default.create()(error.config);
|
|
366
|
-
} catch (error2) {
|
|
367
|
-
if (retryCount === count) {
|
|
368
|
-
return Promise.reject(error2);
|
|
369
|
-
}
|
|
370
|
-
return retry();
|
|
371
|
-
}
|
|
372
|
-
}
|
|
373
|
-
return retry();
|
|
374
|
-
},
|
|
375
|
-
options: options.axiosInterceptorOptions
|
|
376
|
-
};
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
// src/interceptors/responseStatusInterceptor.ts
|
|
380
|
-
function responseStatusInterceptor(options) {
|
|
381
|
-
return {
|
|
382
|
-
onFulfilled: (response) => {
|
|
383
|
-
var _a, _b, _c;
|
|
384
|
-
const matcher = createMatcher(options.include, options.exclude);
|
|
385
|
-
if (!matcher((_a = response.config.method) != null ? _a : "", (_b = response.config.url) != null ? _b : "", response.status)) {
|
|
386
|
-
return response;
|
|
387
|
-
}
|
|
388
|
-
const handler = ((_c = options.validStatusHandler) != null ? _c : {})[response.status];
|
|
389
|
-
if (!handler) {
|
|
390
|
-
return response;
|
|
391
|
-
}
|
|
392
|
-
const handlerResponse = handler(response);
|
|
393
|
-
return handlerResponse != null ? handlerResponse : response;
|
|
394
|
-
},
|
|
395
|
-
onRejected: (error) => {
|
|
396
|
-
var _a, _b, _c, _d;
|
|
397
|
-
const matcher = createMatcher(options.include, options.exclude);
|
|
398
|
-
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)) {
|
|
399
|
-
return Promise.reject(error);
|
|
400
|
-
}
|
|
401
|
-
const handler = ((_d = options.invalidStatusHandler) != null ? _d : {})[error.response.status];
|
|
402
|
-
if (!handler) {
|
|
403
|
-
return Promise.reject(error);
|
|
404
|
-
}
|
|
405
|
-
const handlerError = handler(error);
|
|
406
|
-
return handlerError != null ? handlerError : Promise.reject(error);
|
|
407
|
-
},
|
|
408
|
-
options: options.axiosInterceptorOptions
|
|
409
|
-
};
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
// src/interceptors/requestMd5Interceptor.ts
|
|
413
|
-
var import_md5 = __toESM(require("crypto-js/md5.js"), 1);
|
|
414
|
-
var import_get = __toESM(require("lodash/get.js"), 1);
|
|
415
|
-
var import_set = __toESM(require("lodash/set.js"), 1);
|
|
416
|
-
var import_qs2 = __toESM(require("qs"), 1);
|
|
417
|
-
var import_rattail5 = require("rattail");
|
|
418
|
-
function withCtxMd5(ctx, mapping) {
|
|
419
|
-
mapping.path.forEach((path) => {
|
|
420
|
-
const targetValue = (0, import_get.default)(ctx, path);
|
|
421
|
-
if (targetValue != null) {
|
|
422
|
-
(0, import_set.default)(ctx, path, (0, import_md5.default)(String(targetValue)).toString());
|
|
423
|
-
}
|
|
424
|
-
});
|
|
425
|
-
}
|
|
426
|
-
function withMd5(config, mapping) {
|
|
427
|
-
const { data = {}, params = {}, headers = {} } = config;
|
|
428
|
-
const ctx = {
|
|
429
|
-
data: isFormData(data) ? data : JSON.parse(JSON.stringify(data)),
|
|
430
|
-
params: JSON.parse(JSON.stringify(params)),
|
|
431
|
-
headers: JSON.parse(JSON.stringify(headers))
|
|
432
|
-
};
|
|
433
|
-
if ((0, import_rattail5.isString)(ctx.data) && ctx.data.length > 0 && ctx.headers["Content-Type"] === "application/x-www-form-urlencoded") {
|
|
434
|
-
ctx.data = import_qs2.default.parse(ctx.data);
|
|
435
|
-
withCtxMd5(ctx, mapping);
|
|
436
|
-
ctx.data = import_qs2.default.stringify(ctx.data);
|
|
437
|
-
} else if (isFormData(ctx.data)) {
|
|
438
|
-
ctx.data = formDataToObject(ctx.data);
|
|
439
|
-
withCtxMd5(ctx, mapping);
|
|
440
|
-
ctx.data = objectToFormData(ctx.data);
|
|
441
|
-
} else {
|
|
442
|
-
withCtxMd5(ctx, mapping);
|
|
443
|
-
}
|
|
444
|
-
return __spreadValues(__spreadValues({}, config), ctx);
|
|
445
|
-
}
|
|
446
|
-
function requestMd5Interceptor(options = {}) {
|
|
447
|
-
return {
|
|
448
|
-
onFulfilled(config) {
|
|
449
|
-
var _a, _b;
|
|
450
|
-
const matcher = createMatcher(options.include, options.exclude);
|
|
451
|
-
if (!matcher((_a = config.method) != null ? _a : "", (_b = config.url) != null ? _b : "")) {
|
|
452
|
-
return config;
|
|
453
|
-
}
|
|
454
|
-
const findMapping = () => {
|
|
455
|
-
var _a2;
|
|
456
|
-
return ((_a2 = options.mappings) != null ? _a2 : []).find((mapping2) => {
|
|
457
|
-
var _a3, _b2;
|
|
458
|
-
const isMatchUrl = (0, import_rattail5.isFunction)(mapping2.url) ? mapping2.url((_a3 = config.url) != null ? _a3 : "") : mapping2.url === ((_b2 = config.url) != null ? _b2 : "");
|
|
459
|
-
const isMatchMethod = mapping2.method != null ? config.method === mapping2.method : true;
|
|
460
|
-
return isMatchUrl && isMatchMethod;
|
|
461
|
-
});
|
|
462
|
-
};
|
|
463
|
-
const mapping = findMapping();
|
|
464
|
-
if (!mapping) {
|
|
465
|
-
return config;
|
|
466
|
-
}
|
|
467
|
-
return withMd5(config, mapping);
|
|
468
|
-
},
|
|
469
|
-
onRejected: (error) => Promise.reject(error),
|
|
470
|
-
options: options.axiosInterceptorOptions
|
|
471
|
-
};
|
|
472
|
-
}
|
|
473
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
474
|
-
0 && (module.exports = {
|
|
475
|
-
AxiosError,
|
|
476
|
-
createAxle,
|
|
477
|
-
createFetchRunner,
|
|
478
|
-
createMatcher,
|
|
479
|
-
createModifyRunner,
|
|
480
|
-
download,
|
|
481
|
-
isAxiosError,
|
|
482
|
-
matchPattern,
|
|
483
|
-
requestHeadersInterceptor,
|
|
484
|
-
requestMd5Interceptor,
|
|
485
|
-
requestMockInterceptor,
|
|
486
|
-
responseBlobInterceptor,
|
|
487
|
-
responseRetryInterceptor,
|
|
488
|
-
responseStatusInterceptor,
|
|
489
|
-
withResponse
|
|
490
|
-
});
|
package/lib/index.d.cts
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import { R as RequestInterceptor, a as ResponseInterceptor } from './instance-857fe07c.js';
|
|
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, h as createAxle, f as createFetchRunner, g as createModifyRunner } from './instance-857fe07c.js';
|
|
3
|
-
import { AxiosResponse, AxiosInterceptorOptions, AxiosRequestConfig } from 'axios';
|
|
4
|
-
export { AxiosError, AxiosInstance, AxiosInterceptorOptions, AxiosRequestConfig, AxiosRequestHeaders, AxiosResponse, HeadersDefaults, InternalAxiosRequestConfig, ResponseType, isAxiosError } from 'axios';
|
|
5
|
-
|
|
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;
|
|
13
|
-
|
|
14
|
-
interface WithResponseReturn<R> {
|
|
15
|
-
response: R | undefined;
|
|
16
|
-
errorResponse: AxiosResponse | undefined;
|
|
17
|
-
}
|
|
18
|
-
declare function withResponse<R>(promise: Promise<R>): Promise<WithResponseReturn<R>>;
|
|
19
|
-
declare function download(url: string | Blob, filename: string): void;
|
|
20
|
-
|
|
21
|
-
interface RequestHeadersInterceptorOptions {
|
|
22
|
-
headers?: Record<string, string> | (() => Record<string, string>);
|
|
23
|
-
include?: MatchPattern[];
|
|
24
|
-
exclude?: MatchPattern[];
|
|
25
|
-
axiosInterceptorOptions?: AxiosInterceptorOptions;
|
|
26
|
-
}
|
|
27
|
-
declare function requestHeadersInterceptor(options?: RequestHeadersInterceptorOptions): RequestInterceptor;
|
|
28
|
-
|
|
29
|
-
type RequestMockInterceptorMapping = {
|
|
30
|
-
url: string | ((url: string) => boolean);
|
|
31
|
-
handler: (config: AxiosRequestConfig) => {
|
|
32
|
-
data: any;
|
|
33
|
-
status?: number;
|
|
34
|
-
statusText?: string;
|
|
35
|
-
};
|
|
36
|
-
method?: string;
|
|
37
|
-
delay?: number;
|
|
38
|
-
};
|
|
39
|
-
interface RequestMockInterceptorOptions {
|
|
40
|
-
mappings?: RequestMockInterceptorMapping[];
|
|
41
|
-
include?: MatchPattern[];
|
|
42
|
-
exclude?: MatchPattern[];
|
|
43
|
-
axiosInterceptorOptions?: AxiosInterceptorOptions;
|
|
44
|
-
}
|
|
45
|
-
declare function requestMockInterceptor(options?: RequestMockInterceptorOptions): RequestInterceptor;
|
|
46
|
-
|
|
47
|
-
interface ResponseBlobInterceptorOptions {
|
|
48
|
-
onResponse?: (response: AxiosResponse<any, any>) => any;
|
|
49
|
-
include?: MatchPattern[];
|
|
50
|
-
exclude?: MatchPattern[];
|
|
51
|
-
axiosInterceptorOptions?: AxiosInterceptorOptions;
|
|
52
|
-
}
|
|
53
|
-
declare function responseBlobInterceptor(options?: ResponseBlobInterceptorOptions): ResponseInterceptor;
|
|
54
|
-
|
|
55
|
-
interface ResponseRetryInterceptorOptions {
|
|
56
|
-
count?: number;
|
|
57
|
-
include?: MatchPattern[];
|
|
58
|
-
exclude?: MatchPattern[];
|
|
59
|
-
axiosInterceptorOptions?: AxiosInterceptorOptions;
|
|
60
|
-
}
|
|
61
|
-
declare function responseRetryInterceptor(options: ResponseRetryInterceptorOptions): ResponseInterceptor;
|
|
62
|
-
|
|
63
|
-
interface ResponseStatusInterceptorOptions {
|
|
64
|
-
validStatusHandler?: Record<number | string, (response: AxiosResponse<any, any>) => any>;
|
|
65
|
-
invalidStatusHandler?: Record<number | string, (error: any) => any>;
|
|
66
|
-
include?: MatchPattern[];
|
|
67
|
-
exclude?: MatchPattern[];
|
|
68
|
-
axiosInterceptorOptions?: AxiosInterceptorOptions;
|
|
69
|
-
}
|
|
70
|
-
declare function responseStatusInterceptor(options: ResponseStatusInterceptorOptions): ResponseInterceptor;
|
|
71
|
-
|
|
72
|
-
type RequestMd5InterceptorMapping = {
|
|
73
|
-
url: string | ((url: string) => boolean);
|
|
74
|
-
path: string[];
|
|
75
|
-
method?: string;
|
|
76
|
-
};
|
|
77
|
-
interface RequestMd5InterceptorOptions {
|
|
78
|
-
mappings?: RequestMd5InterceptorMapping[];
|
|
79
|
-
include?: MatchPattern[];
|
|
80
|
-
exclude?: MatchPattern[];
|
|
81
|
-
axiosInterceptorOptions?: AxiosInterceptorOptions;
|
|
82
|
-
}
|
|
83
|
-
declare function requestMd5Interceptor(options?: RequestMd5InterceptorOptions): RequestInterceptor;
|
|
84
|
-
|
|
85
|
-
export { MatchPattern, RequestHeadersInterceptorOptions, RequestInterceptor, RequestMd5InterceptorMapping, RequestMd5InterceptorOptions, RequestMockInterceptorMapping, RequestMockInterceptorOptions, ResponseBlobInterceptorOptions, ResponseInterceptor, ResponseRetryInterceptorOptions, ResponseStatusInterceptorOptions, WithResponseReturn, createMatcher, download, matchPattern, requestHeadersInterceptor, requestMd5Interceptor, requestMockInterceptor, responseBlobInterceptor, responseRetryInterceptor, responseStatusInterceptor, withResponse };
|
package/lib/index.d.ts
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import { R as RequestInterceptor, a as ResponseInterceptor } from './instance-857fe07c.js';
|
|
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, h as createAxle, f as createFetchRunner, g as createModifyRunner } from './instance-857fe07c.js';
|
|
3
|
-
import { AxiosResponse, AxiosInterceptorOptions, AxiosRequestConfig } from 'axios';
|
|
4
|
-
export { AxiosError, AxiosInstance, AxiosInterceptorOptions, AxiosRequestConfig, AxiosRequestHeaders, AxiosResponse, HeadersDefaults, InternalAxiosRequestConfig, ResponseType, isAxiosError } from 'axios';
|
|
5
|
-
|
|
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;
|
|
13
|
-
|
|
14
|
-
interface WithResponseReturn<R> {
|
|
15
|
-
response: R | undefined;
|
|
16
|
-
errorResponse: AxiosResponse | undefined;
|
|
17
|
-
}
|
|
18
|
-
declare function withResponse<R>(promise: Promise<R>): Promise<WithResponseReturn<R>>;
|
|
19
|
-
declare function download(url: string | Blob, filename: string): void;
|
|
20
|
-
|
|
21
|
-
interface RequestHeadersInterceptorOptions {
|
|
22
|
-
headers?: Record<string, string> | (() => Record<string, string>);
|
|
23
|
-
include?: MatchPattern[];
|
|
24
|
-
exclude?: MatchPattern[];
|
|
25
|
-
axiosInterceptorOptions?: AxiosInterceptorOptions;
|
|
26
|
-
}
|
|
27
|
-
declare function requestHeadersInterceptor(options?: RequestHeadersInterceptorOptions): RequestInterceptor;
|
|
28
|
-
|
|
29
|
-
type RequestMockInterceptorMapping = {
|
|
30
|
-
url: string | ((url: string) => boolean);
|
|
31
|
-
handler: (config: AxiosRequestConfig) => {
|
|
32
|
-
data: any;
|
|
33
|
-
status?: number;
|
|
34
|
-
statusText?: string;
|
|
35
|
-
};
|
|
36
|
-
method?: string;
|
|
37
|
-
delay?: number;
|
|
38
|
-
};
|
|
39
|
-
interface RequestMockInterceptorOptions {
|
|
40
|
-
mappings?: RequestMockInterceptorMapping[];
|
|
41
|
-
include?: MatchPattern[];
|
|
42
|
-
exclude?: MatchPattern[];
|
|
43
|
-
axiosInterceptorOptions?: AxiosInterceptorOptions;
|
|
44
|
-
}
|
|
45
|
-
declare function requestMockInterceptor(options?: RequestMockInterceptorOptions): RequestInterceptor;
|
|
46
|
-
|
|
47
|
-
interface ResponseBlobInterceptorOptions {
|
|
48
|
-
onResponse?: (response: AxiosResponse<any, any>) => any;
|
|
49
|
-
include?: MatchPattern[];
|
|
50
|
-
exclude?: MatchPattern[];
|
|
51
|
-
axiosInterceptorOptions?: AxiosInterceptorOptions;
|
|
52
|
-
}
|
|
53
|
-
declare function responseBlobInterceptor(options?: ResponseBlobInterceptorOptions): ResponseInterceptor;
|
|
54
|
-
|
|
55
|
-
interface ResponseRetryInterceptorOptions {
|
|
56
|
-
count?: number;
|
|
57
|
-
include?: MatchPattern[];
|
|
58
|
-
exclude?: MatchPattern[];
|
|
59
|
-
axiosInterceptorOptions?: AxiosInterceptorOptions;
|
|
60
|
-
}
|
|
61
|
-
declare function responseRetryInterceptor(options: ResponseRetryInterceptorOptions): ResponseInterceptor;
|
|
62
|
-
|
|
63
|
-
interface ResponseStatusInterceptorOptions {
|
|
64
|
-
validStatusHandler?: Record<number | string, (response: AxiosResponse<any, any>) => any>;
|
|
65
|
-
invalidStatusHandler?: Record<number | string, (error: any) => any>;
|
|
66
|
-
include?: MatchPattern[];
|
|
67
|
-
exclude?: MatchPattern[];
|
|
68
|
-
axiosInterceptorOptions?: AxiosInterceptorOptions;
|
|
69
|
-
}
|
|
70
|
-
declare function responseStatusInterceptor(options: ResponseStatusInterceptorOptions): ResponseInterceptor;
|
|
71
|
-
|
|
72
|
-
type RequestMd5InterceptorMapping = {
|
|
73
|
-
url: string | ((url: string) => boolean);
|
|
74
|
-
path: string[];
|
|
75
|
-
method?: string;
|
|
76
|
-
};
|
|
77
|
-
interface RequestMd5InterceptorOptions {
|
|
78
|
-
mappings?: RequestMd5InterceptorMapping[];
|
|
79
|
-
include?: MatchPattern[];
|
|
80
|
-
exclude?: MatchPattern[];
|
|
81
|
-
axiosInterceptorOptions?: AxiosInterceptorOptions;
|
|
82
|
-
}
|
|
83
|
-
declare function requestMd5Interceptor(options?: RequestMd5InterceptorOptions): RequestInterceptor;
|
|
84
|
-
|
|
85
|
-
export { MatchPattern, RequestHeadersInterceptorOptions, RequestInterceptor, RequestMd5InterceptorMapping, RequestMd5InterceptorOptions, RequestMockInterceptorMapping, RequestMockInterceptorOptions, ResponseBlobInterceptorOptions, ResponseInterceptor, ResponseRetryInterceptorOptions, ResponseStatusInterceptorOptions, WithResponseReturn, createMatcher, download, matchPattern, requestHeadersInterceptor, requestMd5Interceptor, requestMockInterceptor, responseBlobInterceptor, responseRetryInterceptor, responseStatusInterceptor, withResponse };
|