@robot-admin/request-core 0.2.0 → 0.4.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/CHANGELOG.md +102 -0
- package/LICENSE +21 -0
- package/README.md +248 -357
- package/SECURITY.md +14 -0
- package/dist/axios.cjs +134 -809
- package/dist/axios.cjs.map +1 -1
- package/dist/axios.d.cts +55 -220
- package/dist/axios.d.ts +55 -220
- package/dist/axios.js +2 -790
- package/dist/axios.js.map +1 -1
- package/dist/chunk-2JXH7SIL.js +711 -0
- package/dist/chunk-2JXH7SIL.js.map +1 -0
- package/dist/chunk-56QOEWUS.js +8 -0
- package/dist/chunk-56QOEWUS.js.map +1 -0
- package/dist/chunk-DUP3Y4DN.js +320 -0
- package/dist/chunk-DUP3Y4DN.js.map +1 -0
- package/dist/chunk-JGFRUAZT.cjs +1007 -0
- package/dist/chunk-JGFRUAZT.cjs.map +1 -0
- package/dist/chunk-KHI6XQRN.cjs +717 -0
- package/dist/chunk-KHI6XQRN.cjs.map +1 -0
- package/dist/chunk-OAZ5BUBU.js +969 -0
- package/dist/chunk-OAZ5BUBU.js.map +1 -0
- package/dist/chunk-QSDNDTOZ.js +28 -0
- package/dist/chunk-QSDNDTOZ.js.map +1 -0
- package/dist/chunk-UHXBAFY6.cjs +323 -0
- package/dist/chunk-UHXBAFY6.cjs.map +1 -0
- package/dist/chunk-WUZ43MLM.cjs +30 -0
- package/dist/chunk-WUZ43MLM.cjs.map +1 -0
- package/dist/chunk-WXHQXPS4.cjs +10 -0
- package/dist/chunk-WXHQXPS4.cjs.map +1 -0
- package/dist/crud.cjs +12 -484
- package/dist/crud.cjs.map +1 -1
- package/dist/crud.d.cts +8 -269
- package/dist/crud.d.ts +8 -269
- package/dist/crud.js +4 -487
- package/dist/crud.js.map +1 -1
- package/dist/index.cjs +136 -1302
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +7 -165
- package/dist/index.d.ts +7 -165
- package/dist/index.js +5 -1283
- package/dist/index.js.map +1 -1
- package/dist/naive.cjs +14 -0
- package/dist/naive.cjs.map +1 -0
- package/dist/naive.d.cts +9 -0
- package/dist/naive.d.ts +9 -0
- package/dist/naive.js +5 -0
- package/dist/naive.js.map +1 -0
- package/dist/types-BRDRqYFs.d.cts +197 -0
- package/dist/types-BVJhlSuh.d.cts +310 -0
- package/dist/types-BVJhlSuh.d.ts +310 -0
- package/dist/types-CUyibJZX.d.ts +197 -0
- package/dist/vue.cjs +93 -0
- package/dist/vue.cjs.map +1 -0
- package/dist/vue.d.cts +38 -0
- package/dist/vue.d.ts +38 -0
- package/dist/vue.js +71 -0
- package/dist/vue.js.map +1 -0
- package/package.json +60 -16
|
@@ -0,0 +1,969 @@
|
|
|
1
|
+
import axios2 from 'axios';
|
|
2
|
+
|
|
3
|
+
// src/axios/utils/helpers.ts
|
|
4
|
+
var binaryObjectIds = /* @__PURE__ */ new WeakMap();
|
|
5
|
+
var nextBinaryObjectId = 0;
|
|
6
|
+
function getBinaryObjectId(value) {
|
|
7
|
+
let id = binaryObjectIds.get(value);
|
|
8
|
+
if (id === void 0) {
|
|
9
|
+
id = ++nextBinaryObjectId;
|
|
10
|
+
binaryObjectIds.set(value, id);
|
|
11
|
+
}
|
|
12
|
+
return id;
|
|
13
|
+
}
|
|
14
|
+
function sortedStringify(value, seen = /* @__PURE__ */ new WeakSet()) {
|
|
15
|
+
if (value === null || value === void 0) return "";
|
|
16
|
+
if (typeof value === "bigint") return `bigint:${value.toString()}`;
|
|
17
|
+
if (typeof value !== "object") return `${typeof value}:${String(value)}`;
|
|
18
|
+
if (seen.has(value)) {
|
|
19
|
+
throw new Error("Cannot generate a stable request key from a circular value.");
|
|
20
|
+
}
|
|
21
|
+
seen.add(value);
|
|
22
|
+
try {
|
|
23
|
+
if (Array.isArray(value)) {
|
|
24
|
+
return `[${value.map((item) => sortedStringify(item, seen)).join(",")}]`;
|
|
25
|
+
}
|
|
26
|
+
if (value instanceof Date) return `date:${value.toISOString()}`;
|
|
27
|
+
if (value instanceof Map) {
|
|
28
|
+
return `map:${sortedStringify(
|
|
29
|
+
[...value.entries()].sort(
|
|
30
|
+
([left], [right]) => String(left).localeCompare(String(right))
|
|
31
|
+
),
|
|
32
|
+
seen
|
|
33
|
+
)}`;
|
|
34
|
+
}
|
|
35
|
+
if (value instanceof Set) {
|
|
36
|
+
return `set:${sortedStringify(
|
|
37
|
+
[...value.values()].sort(
|
|
38
|
+
(left, right) => String(left).localeCompare(String(right))
|
|
39
|
+
),
|
|
40
|
+
seen
|
|
41
|
+
)}`;
|
|
42
|
+
}
|
|
43
|
+
if (typeof URLSearchParams !== "undefined" && value instanceof URLSearchParams) {
|
|
44
|
+
return `url-search:${JSON.stringify([...value.entries()].sort())}`;
|
|
45
|
+
}
|
|
46
|
+
if (typeof FormData !== "undefined" && value instanceof FormData) {
|
|
47
|
+
const entries = [...value.entries()].map(([key, item]) => [
|
|
48
|
+
key,
|
|
49
|
+
typeof item === "string" ? `string:${item}` : `binary:${getBinaryObjectId(item)}:${item.name}:${item.size}:${item.type}`
|
|
50
|
+
]);
|
|
51
|
+
return `form-data:${JSON.stringify(entries)}`;
|
|
52
|
+
}
|
|
53
|
+
if (typeof Blob !== "undefined" && value instanceof Blob || value instanceof ArrayBuffer || ArrayBuffer.isView(value)) {
|
|
54
|
+
return `binary:${getBinaryObjectId(value)}`;
|
|
55
|
+
}
|
|
56
|
+
const candidate = value;
|
|
57
|
+
const jsonValue = typeof candidate.toJSON === "function" && candidate.constructor?.name === "AxiosHeaders" ? candidate.toJSON() : candidate;
|
|
58
|
+
if (!jsonValue || typeof jsonValue !== "object") {
|
|
59
|
+
return sortedStringify(jsonValue, seen);
|
|
60
|
+
}
|
|
61
|
+
const source = jsonValue;
|
|
62
|
+
return `{${Object.keys(source).sort().map((key) => `${JSON.stringify(key)}:${sortedStringify(source[key], seen)}`).join(",")}}`;
|
|
63
|
+
} finally {
|
|
64
|
+
seen.delete(value);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
function fingerprint(value) {
|
|
68
|
+
let hash = 2166136261;
|
|
69
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
70
|
+
hash ^= value.charCodeAt(index);
|
|
71
|
+
hash = Math.imul(hash, 16777619);
|
|
72
|
+
}
|
|
73
|
+
return (hash >>> 0).toString(36);
|
|
74
|
+
}
|
|
75
|
+
function generateRequestKey(config, options = {}) {
|
|
76
|
+
const {
|
|
77
|
+
method = "get",
|
|
78
|
+
url = "",
|
|
79
|
+
baseURL = "",
|
|
80
|
+
params,
|
|
81
|
+
data,
|
|
82
|
+
headers,
|
|
83
|
+
responseType = "json"
|
|
84
|
+
} = config;
|
|
85
|
+
const parts = [method.toUpperCase(), baseURL, url, responseType];
|
|
86
|
+
if (params != null) parts.push(sortedStringify(params));
|
|
87
|
+
if (data != null) parts.push(sortedStringify(data));
|
|
88
|
+
if (headers) {
|
|
89
|
+
const headerSource = typeof headers.toJSON === "function" ? headers.toJSON() : headers;
|
|
90
|
+
const normalized = {};
|
|
91
|
+
for (const [key, value] of Object.entries(
|
|
92
|
+
headerSource
|
|
93
|
+
)) {
|
|
94
|
+
normalized[key.toLowerCase()] = value;
|
|
95
|
+
}
|
|
96
|
+
const varyHeaders = new Set(
|
|
97
|
+
["authorization", "x-tenant-id", "x-user-id", ...options.varyHeaders ?? []].map(
|
|
98
|
+
(key) => key.toLowerCase()
|
|
99
|
+
)
|
|
100
|
+
);
|
|
101
|
+
const selected = {};
|
|
102
|
+
for (const key of [...varyHeaders].sort()) {
|
|
103
|
+
if (normalized[key] != null) {
|
|
104
|
+
selected[key] = fingerprint(String(normalized[key]));
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (Object.keys(selected).length > 0) parts.push(sortedStringify(selected));
|
|
108
|
+
}
|
|
109
|
+
return parts.join("|");
|
|
110
|
+
}
|
|
111
|
+
function defaultClone(value) {
|
|
112
|
+
if (typeof structuredClone !== "function") return value;
|
|
113
|
+
try {
|
|
114
|
+
return structuredClone(value);
|
|
115
|
+
} catch {
|
|
116
|
+
return value;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
var MemoryCache = class {
|
|
120
|
+
constructor(options = {}) {
|
|
121
|
+
this.cache = /* @__PURE__ */ new Map();
|
|
122
|
+
this.accessOrder = /* @__PURE__ */ new Set();
|
|
123
|
+
this.maxSize = options.maxSize ?? 1e3;
|
|
124
|
+
this.assertMaxSize(this.maxSize);
|
|
125
|
+
this.cloneValue = typeof options.clone === "function" ? options.clone : options.clone === false ? (value) => value : defaultClone;
|
|
126
|
+
}
|
|
127
|
+
get(key) {
|
|
128
|
+
const item = this.cache.get(key);
|
|
129
|
+
if (!item) return null;
|
|
130
|
+
if (Date.now() >= item.expireAt) {
|
|
131
|
+
this.delete(key);
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
this.accessOrder.delete(key);
|
|
135
|
+
this.accessOrder.add(key);
|
|
136
|
+
return this.cloneValue(item.data);
|
|
137
|
+
}
|
|
138
|
+
set(key, data, ttl, options = {}) {
|
|
139
|
+
if (!Number.isFinite(ttl) || ttl < 0) {
|
|
140
|
+
throw new RangeError("Cache TTL must be a finite number greater than or equal to 0.");
|
|
141
|
+
}
|
|
142
|
+
if (this.maxSize === 0) return;
|
|
143
|
+
if (this.cache.size >= this.maxSize && !this.cache.has(key)) this.evictOldest();
|
|
144
|
+
this.cache.set(key, {
|
|
145
|
+
data: this.cloneValue(data),
|
|
146
|
+
expireAt: Date.now() + ttl,
|
|
147
|
+
tags: options.tags ? [...options.tags] : void 0
|
|
148
|
+
});
|
|
149
|
+
this.accessOrder.delete(key);
|
|
150
|
+
this.accessOrder.add(key);
|
|
151
|
+
}
|
|
152
|
+
delete(key) {
|
|
153
|
+
this.accessOrder.delete(key);
|
|
154
|
+
return this.cache.delete(key);
|
|
155
|
+
}
|
|
156
|
+
clear() {
|
|
157
|
+
this.cache.clear();
|
|
158
|
+
this.accessOrder.clear();
|
|
159
|
+
}
|
|
160
|
+
cleanup() {
|
|
161
|
+
const now = Date.now();
|
|
162
|
+
for (const [key, item] of this.cache) {
|
|
163
|
+
if (now >= item.expireAt) this.delete(key);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
deleteByPrefix(prefix) {
|
|
167
|
+
let removed = 0;
|
|
168
|
+
for (const key of [...this.cache.keys()]) {
|
|
169
|
+
if (key.startsWith(prefix) && this.delete(key)) removed += 1;
|
|
170
|
+
}
|
|
171
|
+
return removed;
|
|
172
|
+
}
|
|
173
|
+
deleteByTag(tag) {
|
|
174
|
+
let removed = 0;
|
|
175
|
+
for (const [key, item] of [...this.cache.entries()]) {
|
|
176
|
+
if (item.tags?.includes(tag) && this.delete(key)) removed += 1;
|
|
177
|
+
}
|
|
178
|
+
return removed;
|
|
179
|
+
}
|
|
180
|
+
get size() {
|
|
181
|
+
return this.cache.size;
|
|
182
|
+
}
|
|
183
|
+
setMaxSize(size) {
|
|
184
|
+
this.assertMaxSize(size);
|
|
185
|
+
this.maxSize = size;
|
|
186
|
+
while (this.cache.size > this.maxSize) this.evictOldest();
|
|
187
|
+
}
|
|
188
|
+
assertMaxSize(size) {
|
|
189
|
+
if (!Number.isInteger(size) || size < 0) {
|
|
190
|
+
throw new RangeError(
|
|
191
|
+
"Cache capacity must be an integer greater than or equal to 0."
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
evictOldest() {
|
|
196
|
+
const oldestKey = this.accessOrder.values().next().value;
|
|
197
|
+
if (oldestKey !== void 0) this.delete(oldestKey);
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
function delay(ms, signal) {
|
|
201
|
+
if (signal?.aborted) {
|
|
202
|
+
return Promise.reject(createAbortError(signal.reason));
|
|
203
|
+
}
|
|
204
|
+
return new Promise((resolve, reject) => {
|
|
205
|
+
const onAbort = () => {
|
|
206
|
+
clearTimeout(timer);
|
|
207
|
+
signal?.removeEventListener?.("abort", onAbort);
|
|
208
|
+
reject(createAbortError(signal?.reason));
|
|
209
|
+
};
|
|
210
|
+
const timer = setTimeout(() => {
|
|
211
|
+
signal?.removeEventListener?.("abort", onAbort);
|
|
212
|
+
resolve();
|
|
213
|
+
}, Math.max(0, Number.isFinite(ms) ? ms : 0));
|
|
214
|
+
signal?.addEventListener?.("abort", onAbort, { once: true });
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
function createAbortError(reason) {
|
|
218
|
+
if (reason instanceof Error) return reason;
|
|
219
|
+
return Object.assign(new Error("canceled"), {
|
|
220
|
+
name: "AbortError",
|
|
221
|
+
code: "ERR_CANCELED",
|
|
222
|
+
reason
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
function isNetworkError(error) {
|
|
226
|
+
const candidate = error;
|
|
227
|
+
return !candidate?.response && Boolean(candidate?.code) && candidate.code !== "ECONNABORTED" && candidate.code !== "ERR_CANCELED" && candidate.message !== "canceled" && candidate.message !== "Request aborted" && candidate.message !== "Request cancelled";
|
|
228
|
+
}
|
|
229
|
+
function isTimeoutError(error) {
|
|
230
|
+
const code = error?.code;
|
|
231
|
+
return code === "ECONNABORTED" || code === "ETIMEDOUT";
|
|
232
|
+
}
|
|
233
|
+
function isRetryableStatus(status, retryableStatusCodes) {
|
|
234
|
+
return retryableStatusCodes.includes(status);
|
|
235
|
+
}
|
|
236
|
+
function normalizeConfig(config, defaults) {
|
|
237
|
+
if (config === true) return { ...defaults, enabled: true };
|
|
238
|
+
if (config === false) return { ...defaults, enabled: false };
|
|
239
|
+
if (config && typeof config === "object") {
|
|
240
|
+
return {
|
|
241
|
+
...defaults,
|
|
242
|
+
...Object.fromEntries(
|
|
243
|
+
Object.entries(config).filter(
|
|
244
|
+
([, value]) => value !== void 0
|
|
245
|
+
)
|
|
246
|
+
)
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
return { ...defaults };
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// src/axios/runtime.ts
|
|
253
|
+
var RUNTIME_KEY = /* @__PURE__ */ Symbol.for("@robot-admin/request-core/runtime/v1");
|
|
254
|
+
var PACKAGE_STATE_KEY = /* @__PURE__ */ Symbol.for("@robot-admin/request-core/state/v1");
|
|
255
|
+
var DEFAULT_GLOBAL_CONFIG = {
|
|
256
|
+
successCodes: [200, 0, "200", "0"],
|
|
257
|
+
fieldAliases: {
|
|
258
|
+
data: ["data", "list", "items", "records"],
|
|
259
|
+
list: ["list", "items", "records", "rows", "data"],
|
|
260
|
+
total: ["total", "totalCount", "count", "totalElements"]
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
function cloneGlobalConfig(config) {
|
|
264
|
+
return {
|
|
265
|
+
successCodes: [...config.successCodes],
|
|
266
|
+
fieldAliases: {
|
|
267
|
+
data: [...config.fieldAliases.data],
|
|
268
|
+
list: [...config.fieldAliases.list],
|
|
269
|
+
total: [...config.fieldAliases.total]
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
function getSharedState() {
|
|
274
|
+
const target = globalThis;
|
|
275
|
+
if (!target[PACKAGE_STATE_KEY]) {
|
|
276
|
+
target[PACKAGE_STATE_KEY] = {
|
|
277
|
+
defaultInstance: null,
|
|
278
|
+
lastInstance: null,
|
|
279
|
+
globalConfig: cloneGlobalConfig(DEFAULT_GLOBAL_CONFIG)
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
return target[PACKAGE_STATE_KEY];
|
|
283
|
+
}
|
|
284
|
+
function createRequestRuntime(options = {}) {
|
|
285
|
+
return {
|
|
286
|
+
cache: options.cache ?? new MemoryCache(),
|
|
287
|
+
defaults: { ...options.defaults },
|
|
288
|
+
pendingRequests: /* @__PURE__ */ new Map(),
|
|
289
|
+
cancelableRequests: /* @__PURE__ */ new Map(),
|
|
290
|
+
joinedRequests: /* @__PURE__ */ new Map(),
|
|
291
|
+
requestId: 0,
|
|
292
|
+
dedupeCleanupTimer: null,
|
|
293
|
+
cancelCleanupTimer: null,
|
|
294
|
+
reLogin: { promise: null, resolve: null, reject: null },
|
|
295
|
+
authRefreshPromise: null,
|
|
296
|
+
reauthenticatePromise: null,
|
|
297
|
+
disposed: false
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
function attachRequestRuntime(instance, runtime) {
|
|
301
|
+
Object.defineProperty(instance, RUNTIME_KEY, {
|
|
302
|
+
configurable: false,
|
|
303
|
+
enumerable: false,
|
|
304
|
+
writable: false,
|
|
305
|
+
value: runtime
|
|
306
|
+
});
|
|
307
|
+
getSharedState().lastInstance = instance;
|
|
308
|
+
}
|
|
309
|
+
function getRequestRuntime(instance) {
|
|
310
|
+
const runtime = instance[RUNTIME_KEY];
|
|
311
|
+
if (!runtime) {
|
|
312
|
+
throw new Error("The Axios instance is not managed by request-core.");
|
|
313
|
+
}
|
|
314
|
+
return runtime;
|
|
315
|
+
}
|
|
316
|
+
function getActiveRequestRuntime(instance) {
|
|
317
|
+
const shared = getSharedState();
|
|
318
|
+
const target = instance ?? shared.defaultInstance ?? shared.lastInstance;
|
|
319
|
+
if (!target) {
|
|
320
|
+
throw new Error(
|
|
321
|
+
"Request client not initialized. Create a client or call createRequestCore() first."
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
return getRequestRuntime(target);
|
|
325
|
+
}
|
|
326
|
+
function setDefaultAxiosInstance(instance) {
|
|
327
|
+
getRequestRuntime(instance);
|
|
328
|
+
getSharedState().defaultInstance = instance;
|
|
329
|
+
}
|
|
330
|
+
function getDefaultAxiosInstance() {
|
|
331
|
+
return getSharedState().defaultInstance;
|
|
332
|
+
}
|
|
333
|
+
function getGlobalRuntimeConfig() {
|
|
334
|
+
return cloneGlobalConfig(getSharedState().globalConfig);
|
|
335
|
+
}
|
|
336
|
+
function setGlobalRuntimeConfig(config) {
|
|
337
|
+
const next = cloneGlobalConfig(DEFAULT_GLOBAL_CONFIG);
|
|
338
|
+
if (config.successCodes) next.successCodes = [...config.successCodes];
|
|
339
|
+
if (config.fieldAliases?.data) next.fieldAliases.data = [...config.fieldAliases.data];
|
|
340
|
+
if (config.fieldAliases?.list) next.fieldAliases.list = [...config.fieldAliases.list];
|
|
341
|
+
if (config.fieldAliases?.total) next.fieldAliases.total = [...config.fieldAliases.total];
|
|
342
|
+
getSharedState().globalConfig = next;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
// src/axios/plugins/cache.ts
|
|
346
|
+
var DEFAULT_CACHE_CONFIG = {
|
|
347
|
+
enabled: false,
|
|
348
|
+
ttl: 5 * 60 * 1e3,
|
|
349
|
+
forceUpdate: false,
|
|
350
|
+
tags: [],
|
|
351
|
+
varyHeaders: []
|
|
352
|
+
};
|
|
353
|
+
function resolveCacheConfig(config, runtime) {
|
|
354
|
+
const inherited = normalizeConfig(
|
|
355
|
+
runtime.defaults.cache,
|
|
356
|
+
DEFAULT_CACHE_CONFIG
|
|
357
|
+
);
|
|
358
|
+
const resolved = normalizeConfig(config.cache, inherited);
|
|
359
|
+
if (!Number.isFinite(resolved.ttl) || resolved.ttl < 0) {
|
|
360
|
+
throw new RangeError("Cache TTL must be a finite number greater than or equal to 0.");
|
|
361
|
+
}
|
|
362
|
+
return resolved;
|
|
363
|
+
}
|
|
364
|
+
function cacheKey(config, cacheConfig) {
|
|
365
|
+
return cacheConfig.key ?? generateRequestKey(config, { varyHeaders: cacheConfig.varyHeaders });
|
|
366
|
+
}
|
|
367
|
+
function createCacheResponse(cached, config) {
|
|
368
|
+
return {
|
|
369
|
+
data: cached.data,
|
|
370
|
+
status: cached.status,
|
|
371
|
+
statusText: `${cached.statusText} (from cache)`,
|
|
372
|
+
headers: cached.headers,
|
|
373
|
+
config
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
function setupCachePlugin(instance, runtime) {
|
|
377
|
+
instance.interceptors.request.use((config) => {
|
|
378
|
+
const enhanced = config;
|
|
379
|
+
const cacheConfig = resolveCacheConfig(enhanced, runtime);
|
|
380
|
+
if (config.method?.toUpperCase() !== "GET" || !cacheConfig.enabled || cacheConfig.forceUpdate) {
|
|
381
|
+
return config;
|
|
382
|
+
}
|
|
383
|
+
const cached = runtime.cache.get(
|
|
384
|
+
cacheKey(config, cacheConfig)
|
|
385
|
+
);
|
|
386
|
+
if (!cached) return config;
|
|
387
|
+
enhanced.__fromCache = true;
|
|
388
|
+
const hit = {
|
|
389
|
+
__fromCache: true,
|
|
390
|
+
__cachedResponse: createCacheResponse(cached, config),
|
|
391
|
+
config
|
|
392
|
+
};
|
|
393
|
+
return Promise.reject(hit);
|
|
394
|
+
});
|
|
395
|
+
instance.interceptors.response.use(
|
|
396
|
+
(response) => {
|
|
397
|
+
const config = response.config;
|
|
398
|
+
if (config.__fromCache && config.__cachedResponse) {
|
|
399
|
+
return config.__cachedResponse;
|
|
400
|
+
}
|
|
401
|
+
const cacheConfig = resolveCacheConfig(config, runtime);
|
|
402
|
+
if (config.method?.toUpperCase() === "GET" && cacheConfig.enabled && response.status >= 200 && response.status < 300) {
|
|
403
|
+
const cached = {
|
|
404
|
+
data: response.data,
|
|
405
|
+
status: response.status,
|
|
406
|
+
statusText: response.statusText,
|
|
407
|
+
headers: { ...response.headers }
|
|
408
|
+
};
|
|
409
|
+
runtime.cache.set(cacheKey(config, cacheConfig), cached, cacheConfig.ttl, {
|
|
410
|
+
tags: cacheConfig.tags
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
return response;
|
|
414
|
+
},
|
|
415
|
+
(error) => {
|
|
416
|
+
const hit = error;
|
|
417
|
+
if (hit.__fromCache && hit.__cachedResponse) {
|
|
418
|
+
return Promise.resolve(hit.__cachedResponse);
|
|
419
|
+
}
|
|
420
|
+
return Promise.reject(error);
|
|
421
|
+
}
|
|
422
|
+
);
|
|
423
|
+
}
|
|
424
|
+
function clearAllCache(instance) {
|
|
425
|
+
getActiveRequestRuntime(instance).cache.clear();
|
|
426
|
+
}
|
|
427
|
+
function clearCache(config, instance) {
|
|
428
|
+
const runtime = getActiveRequestRuntime(instance);
|
|
429
|
+
const cacheConfig = resolveCacheConfig(config, runtime);
|
|
430
|
+
return runtime.cache.delete(cacheKey(config, cacheConfig));
|
|
431
|
+
}
|
|
432
|
+
function clearCacheByPrefix(prefix, instance) {
|
|
433
|
+
return getActiveRequestRuntime(instance).cache.deleteByPrefix?.(prefix) ?? 0;
|
|
434
|
+
}
|
|
435
|
+
function clearCacheByTag(tag, instance) {
|
|
436
|
+
return getActiveRequestRuntime(instance).cache.deleteByTag?.(tag) ?? 0;
|
|
437
|
+
}
|
|
438
|
+
function cleanupExpiredCache(instance) {
|
|
439
|
+
getActiveRequestRuntime(instance).cache.cleanup?.();
|
|
440
|
+
}
|
|
441
|
+
function getCacheSize(instance) {
|
|
442
|
+
return getActiveRequestRuntime(instance).cache.size;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
// src/axios/utils/abort.ts
|
|
446
|
+
function ensureSharedAbortController(config) {
|
|
447
|
+
if (typeof AbortController === "undefined") return null;
|
|
448
|
+
const existing = config.__abortController;
|
|
449
|
+
if (existing) {
|
|
450
|
+
existing._startTime ?? (existing._startTime = Date.now());
|
|
451
|
+
config.signal = existing.signal;
|
|
452
|
+
return existing;
|
|
453
|
+
}
|
|
454
|
+
const externalSignal = config.signal;
|
|
455
|
+
const controller = new AbortController();
|
|
456
|
+
controller._startTime = Date.now();
|
|
457
|
+
if (externalSignal && externalSignal !== controller.signal) {
|
|
458
|
+
config.__externalSignal = externalSignal;
|
|
459
|
+
const forwardAbort = () => {
|
|
460
|
+
if (!controller.signal.aborted) {
|
|
461
|
+
controller.abort(externalSignal.reason);
|
|
462
|
+
}
|
|
463
|
+
};
|
|
464
|
+
if (externalSignal.aborted) {
|
|
465
|
+
forwardAbort();
|
|
466
|
+
} else {
|
|
467
|
+
externalSignal.addEventListener?.("abort", forwardAbort, { once: true });
|
|
468
|
+
config.__abortCleanup = () => {
|
|
469
|
+
externalSignal.removeEventListener?.("abort", forwardAbort);
|
|
470
|
+
};
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
config.__abortController = controller;
|
|
474
|
+
config.signal = controller.signal;
|
|
475
|
+
return controller;
|
|
476
|
+
}
|
|
477
|
+
function cleanupAbortContext(config) {
|
|
478
|
+
if (!config) return;
|
|
479
|
+
config.__abortCleanup?.();
|
|
480
|
+
if (config.__externalSignal) {
|
|
481
|
+
config.signal = config.__externalSignal;
|
|
482
|
+
} else if (config.signal === config.__abortController?.signal) {
|
|
483
|
+
delete config.signal;
|
|
484
|
+
}
|
|
485
|
+
delete config.__abortCleanup;
|
|
486
|
+
delete config.__externalSignal;
|
|
487
|
+
delete config.__abortController;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
// src/axios/plugins/cancel.ts
|
|
491
|
+
var DEFAULT_CANCEL_CONFIG = {
|
|
492
|
+
enabled: true,
|
|
493
|
+
whitelist: []
|
|
494
|
+
};
|
|
495
|
+
var CLEANUP_INTERVAL = 3e4;
|
|
496
|
+
var REQUEST_TIMEOUT = 5 * 6e4;
|
|
497
|
+
function stopTimerIfIdle(runtime) {
|
|
498
|
+
if (runtime.cancelableRequests.size === 0 && runtime.cancelCleanupTimer) {
|
|
499
|
+
clearInterval(runtime.cancelCleanupTimer);
|
|
500
|
+
runtime.cancelCleanupTimer = null;
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
function startTimer(runtime) {
|
|
504
|
+
if (runtime.cancelCleanupTimer) return;
|
|
505
|
+
runtime.cancelCleanupTimer = setInterval(() => {
|
|
506
|
+
const now = Date.now();
|
|
507
|
+
for (const [id, entry] of runtime.cancelableRequests) {
|
|
508
|
+
if (now - (entry.controller._startTime ?? now) > REQUEST_TIMEOUT) {
|
|
509
|
+
entry.controller.abort(new Error("Request lifecycle expired."));
|
|
510
|
+
runtime.cancelableRequests.delete(id);
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
stopTimerIfIdle(runtime);
|
|
514
|
+
}, CLEANUP_INTERVAL);
|
|
515
|
+
runtime.cancelCleanupTimer.unref?.();
|
|
516
|
+
}
|
|
517
|
+
function isWhitelisted(url, whitelist) {
|
|
518
|
+
return whitelist.some((pattern) => new RegExp(pattern.source, pattern.flags).test(url));
|
|
519
|
+
}
|
|
520
|
+
function removeCancelable(config, runtime) {
|
|
521
|
+
if (config?.__cancelId) runtime.cancelableRequests.delete(config.__cancelId);
|
|
522
|
+
stopTimerIfIdle(runtime);
|
|
523
|
+
}
|
|
524
|
+
function setupCancelPlugin(instance, runtime) {
|
|
525
|
+
instance.interceptors.request.use((config) => {
|
|
526
|
+
const enhanced = config;
|
|
527
|
+
const inherited = normalizeConfig(
|
|
528
|
+
runtime.defaults.cancel,
|
|
529
|
+
DEFAULT_CANCEL_CONFIG
|
|
530
|
+
);
|
|
531
|
+
const cancel = normalizeConfig(enhanced.cancel, inherited);
|
|
532
|
+
if (!cancel.enabled || enhanced.__fromCache || isWhitelisted(config.url ?? "", cancel.whitelist)) {
|
|
533
|
+
return config;
|
|
534
|
+
}
|
|
535
|
+
const controller = ensureSharedAbortController(enhanced);
|
|
536
|
+
if (!controller) return config;
|
|
537
|
+
const id = `request_${++runtime.requestId}`;
|
|
538
|
+
enhanced.__cancelId = id;
|
|
539
|
+
runtime.cancelableRequests.set(id, {
|
|
540
|
+
controller,
|
|
541
|
+
scope: cancel.scope ?? enhanced.scope
|
|
542
|
+
});
|
|
543
|
+
startTimer(runtime);
|
|
544
|
+
return config;
|
|
545
|
+
});
|
|
546
|
+
instance.interceptors.response.use(
|
|
547
|
+
(response) => {
|
|
548
|
+
removeCancelable(response.config, runtime);
|
|
549
|
+
return response;
|
|
550
|
+
},
|
|
551
|
+
(error) => {
|
|
552
|
+
removeCancelable(
|
|
553
|
+
error?.config,
|
|
554
|
+
runtime
|
|
555
|
+
);
|
|
556
|
+
return Promise.reject(error);
|
|
557
|
+
}
|
|
558
|
+
);
|
|
559
|
+
}
|
|
560
|
+
function cancelAllRequests(instance) {
|
|
561
|
+
const runtime = getActiveRequestRuntime(instance);
|
|
562
|
+
for (const entry of runtime.cancelableRequests.values()) entry.controller.abort();
|
|
563
|
+
runtime.cancelableRequests.clear();
|
|
564
|
+
stopTimerIfIdle(runtime);
|
|
565
|
+
}
|
|
566
|
+
function cancelRequestScope(scope, instance) {
|
|
567
|
+
const runtime = getActiveRequestRuntime(instance);
|
|
568
|
+
let canceled = 0;
|
|
569
|
+
for (const [id, entry] of [...runtime.cancelableRequests.entries()]) {
|
|
570
|
+
if (entry.scope === scope) {
|
|
571
|
+
entry.controller.abort(new Error(`Request scope canceled: ${String(scope)}`));
|
|
572
|
+
runtime.cancelableRequests.delete(id);
|
|
573
|
+
canceled += 1;
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
stopTimerIfIdle(runtime);
|
|
577
|
+
return canceled;
|
|
578
|
+
}
|
|
579
|
+
function getCancelableRequestCount(instance) {
|
|
580
|
+
return getActiveRequestRuntime(instance).cancelableRequests.size;
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
// src/axios/plugins/dedupe.ts
|
|
584
|
+
var DEFAULT_DEDUPE_CONFIG = {
|
|
585
|
+
enabled: true,
|
|
586
|
+
keyGenerator: generateRequestKey
|
|
587
|
+
};
|
|
588
|
+
var SAFE_METHODS = /* @__PURE__ */ new Set(["GET", "HEAD", "OPTIONS"]);
|
|
589
|
+
var CLEANUP_INTERVAL2 = 3e4;
|
|
590
|
+
var REQUEST_TIMEOUT2 = 5 * 6e4;
|
|
591
|
+
function stopTimerIfIdle2(runtime) {
|
|
592
|
+
if (runtime.pendingRequests.size === 0 && runtime.dedupeCleanupTimer) {
|
|
593
|
+
clearInterval(runtime.dedupeCleanupTimer);
|
|
594
|
+
runtime.dedupeCleanupTimer = null;
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
function startTimer2(runtime) {
|
|
598
|
+
if (runtime.dedupeCleanupTimer) return;
|
|
599
|
+
runtime.dedupeCleanupTimer = setInterval(() => {
|
|
600
|
+
const now = Date.now();
|
|
601
|
+
for (const [key, controller] of runtime.pendingRequests) {
|
|
602
|
+
if (now - (controller._startTime ?? now) > REQUEST_TIMEOUT2) {
|
|
603
|
+
controller.abort();
|
|
604
|
+
runtime.pendingRequests.delete(key);
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
stopTimerIfIdle2(runtime);
|
|
608
|
+
}, CLEANUP_INTERVAL2);
|
|
609
|
+
runtime.dedupeCleanupTimer.unref?.();
|
|
610
|
+
}
|
|
611
|
+
function removePending(config, runtime) {
|
|
612
|
+
const key = config.__requestKey;
|
|
613
|
+
const controller = config.__abortController;
|
|
614
|
+
if (key && controller && runtime.pendingRequests.get(key) === controller) {
|
|
615
|
+
runtime.pendingRequests.delete(key);
|
|
616
|
+
}
|
|
617
|
+
stopTimerIfIdle2(runtime);
|
|
618
|
+
}
|
|
619
|
+
function setupDedupePlugin(instance, runtime) {
|
|
620
|
+
instance.interceptors.request.use((config) => {
|
|
621
|
+
const enhanced = config;
|
|
622
|
+
const method = (config.method ?? "get").toUpperCase();
|
|
623
|
+
const requestSetting = enhanced.dedupe ?? runtime.defaults.dedupe;
|
|
624
|
+
const isImplicitDefault = requestSetting === void 0;
|
|
625
|
+
if (isImplicitDefault && !SAFE_METHODS.has(method)) return config;
|
|
626
|
+
const dedupe = normalizeConfig(
|
|
627
|
+
requestSetting,
|
|
628
|
+
DEFAULT_DEDUPE_CONFIG
|
|
629
|
+
);
|
|
630
|
+
if (!dedupe.enabled || enhanced.__fromCache) return config;
|
|
631
|
+
const key = dedupe.keyGenerator(config);
|
|
632
|
+
const existing = runtime.pendingRequests.get(key);
|
|
633
|
+
if (existing) {
|
|
634
|
+
existing.abort(new Error("Superseded by a newer request."));
|
|
635
|
+
runtime.pendingRequests.delete(key);
|
|
636
|
+
}
|
|
637
|
+
const controller = ensureSharedAbortController(enhanced);
|
|
638
|
+
if (!controller) return config;
|
|
639
|
+
enhanced.__requestKey = key;
|
|
640
|
+
runtime.pendingRequests.set(key, controller);
|
|
641
|
+
startTimer2(runtime);
|
|
642
|
+
return config;
|
|
643
|
+
});
|
|
644
|
+
instance.interceptors.response.use(
|
|
645
|
+
(response) => {
|
|
646
|
+
removePending(response.config, runtime);
|
|
647
|
+
return response;
|
|
648
|
+
},
|
|
649
|
+
(error) => {
|
|
650
|
+
const config = error?.config;
|
|
651
|
+
if (config) removePending(config, runtime);
|
|
652
|
+
return Promise.reject(error);
|
|
653
|
+
}
|
|
654
|
+
);
|
|
655
|
+
}
|
|
656
|
+
function cancelAllPendingRequests(instance) {
|
|
657
|
+
const runtime = getActiveRequestRuntime(instance);
|
|
658
|
+
for (const controller of runtime.pendingRequests.values()) controller.abort();
|
|
659
|
+
runtime.pendingRequests.clear();
|
|
660
|
+
stopTimerIfIdle2(runtime);
|
|
661
|
+
}
|
|
662
|
+
function getPendingRequestCount(instance) {
|
|
663
|
+
return getActiveRequestRuntime(instance).pendingRequests.size;
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
// src/axios/plugins/request.ts
|
|
667
|
+
function waitForReLogin(instance) {
|
|
668
|
+
const state = getActiveRequestRuntime(instance).reLogin;
|
|
669
|
+
if (!state.promise) {
|
|
670
|
+
state.promise = new Promise((resolve, reject) => {
|
|
671
|
+
state.resolve = resolve;
|
|
672
|
+
state.reject = reject;
|
|
673
|
+
}).finally(() => {
|
|
674
|
+
state.promise = null;
|
|
675
|
+
state.resolve = null;
|
|
676
|
+
state.reject = null;
|
|
677
|
+
});
|
|
678
|
+
}
|
|
679
|
+
return state.promise;
|
|
680
|
+
}
|
|
681
|
+
function getReLoginPromise(instance) {
|
|
682
|
+
return getActiveRequestRuntime(instance).reLogin.promise;
|
|
683
|
+
}
|
|
684
|
+
function resolveReLogin(instance) {
|
|
685
|
+
getActiveRequestRuntime(instance).reLogin.resolve?.();
|
|
686
|
+
}
|
|
687
|
+
function rejectReLogin(reason, instance) {
|
|
688
|
+
getActiveRequestRuntime(instance).reLogin.reject?.(reason);
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
// src/axios/plugins/response.ts
|
|
692
|
+
function setupResponsePlugin(instance) {
|
|
693
|
+
instance.interceptors.response.use(
|
|
694
|
+
(response) => {
|
|
695
|
+
cleanupAbortContext(response.config);
|
|
696
|
+
return response;
|
|
697
|
+
},
|
|
698
|
+
(error) => {
|
|
699
|
+
cleanupAbortContext(
|
|
700
|
+
error?.config
|
|
701
|
+
);
|
|
702
|
+
return Promise.reject(error);
|
|
703
|
+
}
|
|
704
|
+
);
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
// src/axios/plugins/retry.ts
|
|
708
|
+
var DEFAULT_RETRY_CONFIG = {
|
|
709
|
+
enabled: false,
|
|
710
|
+
count: 3,
|
|
711
|
+
delay: 1e3,
|
|
712
|
+
exponentialBackoff: true,
|
|
713
|
+
jitter: true,
|
|
714
|
+
retryableStatusCodes: [408, 429, 500, 502, 503, 504],
|
|
715
|
+
retryableMethods: ["GET", "HEAD", "OPTIONS", "PUT", "DELETE"],
|
|
716
|
+
maxDelay: 3e4,
|
|
717
|
+
maxElapsedMs: 0,
|
|
718
|
+
respectRetryAfter: true,
|
|
719
|
+
retryUnsafeBody: false
|
|
720
|
+
};
|
|
721
|
+
function resolveRetryConfig(config, runtime) {
|
|
722
|
+
const inherited = normalizeConfig(
|
|
723
|
+
runtime.defaults.retry,
|
|
724
|
+
DEFAULT_RETRY_CONFIG
|
|
725
|
+
);
|
|
726
|
+
const resolved = normalizeConfig(config.retry, inherited);
|
|
727
|
+
for (const [name, value] of [
|
|
728
|
+
["count", resolved.count],
|
|
729
|
+
["delay", resolved.delay],
|
|
730
|
+
["maxDelay", resolved.maxDelay],
|
|
731
|
+
["maxElapsedMs", resolved.maxElapsedMs]
|
|
732
|
+
]) {
|
|
733
|
+
if (!Number.isFinite(value) || value < 0 || name === "count" && !Number.isInteger(value)) {
|
|
734
|
+
throw new RangeError(`Retry ${name} has an invalid value.`);
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
return resolved;
|
|
738
|
+
}
|
|
739
|
+
function isCanceled(error) {
|
|
740
|
+
const candidate = error;
|
|
741
|
+
return candidate?.name === "CanceledError" || candidate?.name === "AbortError" || candidate?.code === "ERR_CANCELED";
|
|
742
|
+
}
|
|
743
|
+
function hasUnsafeBody(data) {
|
|
744
|
+
if (!data || typeof data !== "object") return false;
|
|
745
|
+
const candidate = data;
|
|
746
|
+
return typeof candidate.pipe === "function" || typeof candidate.getReader === "function" || candidate.locked === true;
|
|
747
|
+
}
|
|
748
|
+
async function shouldRetry(error, retry) {
|
|
749
|
+
const config = error.config;
|
|
750
|
+
if (!retry.enabled || !config || isCanceled(error)) return false;
|
|
751
|
+
const attempt = config.__retryCount ?? 0;
|
|
752
|
+
if (attempt >= retry.count) return false;
|
|
753
|
+
if (hasUnsafeBody(config.data) && !retry.retryUnsafeBody) return false;
|
|
754
|
+
const method = (config.method ?? "get").toUpperCase();
|
|
755
|
+
if (!retry.retryableMethods.some((item) => item.toUpperCase() === method)) return false;
|
|
756
|
+
if (retry.shouldRetry) return retry.shouldRetry(error, attempt + 1);
|
|
757
|
+
if (isNetworkError(error) || isTimeoutError(error)) return true;
|
|
758
|
+
return error.response?.status ? isRetryableStatus(error.response.status, retry.retryableStatusCodes) : false;
|
|
759
|
+
}
|
|
760
|
+
function retryAfterDelay(error) {
|
|
761
|
+
const raw = error.response?.headers?.["retry-after"];
|
|
762
|
+
if (raw == null) return null;
|
|
763
|
+
const seconds = Number(raw);
|
|
764
|
+
if (Number.isFinite(seconds)) return Math.max(0, seconds * 1e3);
|
|
765
|
+
const timestamp = Date.parse(String(raw));
|
|
766
|
+
return Number.isFinite(timestamp) ? Math.max(0, timestamp - Date.now()) : null;
|
|
767
|
+
}
|
|
768
|
+
function calculateDelay(error, attempt, retry) {
|
|
769
|
+
let calculated = retry.exponentialBackoff ? retry.delay * 2 ** (attempt - 1) : retry.delay;
|
|
770
|
+
if (retry.jitter) calculated = Math.round(calculated * (0.75 + Math.random() * 0.5));
|
|
771
|
+
if (retry.respectRetryAfter && [429, 503].includes(error.response?.status ?? 0)) {
|
|
772
|
+
calculated = Math.max(calculated, retryAfterDelay(error) ?? 0);
|
|
773
|
+
}
|
|
774
|
+
return Math.min(Math.max(0, calculated), retry.maxDelay);
|
|
775
|
+
}
|
|
776
|
+
function setupRetryPlugin(instance, runtime) {
|
|
777
|
+
instance.interceptors.response.use(void 0, async (error) => {
|
|
778
|
+
const config = error.config;
|
|
779
|
+
if (!config) return Promise.reject(error);
|
|
780
|
+
const retry = resolveRetryConfig(config, runtime);
|
|
781
|
+
if (!await shouldRetry(error, retry)) return Promise.reject(error);
|
|
782
|
+
const startedAt = config.__retryStartedAt ?? Date.now();
|
|
783
|
+
config.__retryStartedAt = startedAt;
|
|
784
|
+
const attempt = (config.__retryCount ?? 0) + 1;
|
|
785
|
+
const wait = calculateDelay(error, attempt, retry);
|
|
786
|
+
const elapsed = Date.now() - startedAt;
|
|
787
|
+
if (retry.maxElapsedMs > 0 && elapsed + wait > retry.maxElapsedMs) {
|
|
788
|
+
return Promise.reject(error);
|
|
789
|
+
}
|
|
790
|
+
config.__retryCount = attempt;
|
|
791
|
+
await retry.onRetry?.({ attempt, delay: wait, elapsed, error, config });
|
|
792
|
+
try {
|
|
793
|
+
await delay(wait, config.signal);
|
|
794
|
+
} catch (cause) {
|
|
795
|
+
return Promise.reject(
|
|
796
|
+
Object.assign(new Error("canceled"), {
|
|
797
|
+
name: "CanceledError",
|
|
798
|
+
code: "ERR_CANCELED",
|
|
799
|
+
config,
|
|
800
|
+
cause
|
|
801
|
+
})
|
|
802
|
+
);
|
|
803
|
+
}
|
|
804
|
+
const next = { ...config };
|
|
805
|
+
delete next.__cancelId;
|
|
806
|
+
delete next.__requestKey;
|
|
807
|
+
return instance.request(next);
|
|
808
|
+
});
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
// src/axios/plugins/index.ts
|
|
812
|
+
function setupPlugins(instance, runtime) {
|
|
813
|
+
setupCachePlugin(instance, runtime);
|
|
814
|
+
setupCancelPlugin(instance, runtime);
|
|
815
|
+
setupDedupePlugin(instance, runtime);
|
|
816
|
+
setupRetryPlugin(instance, runtime);
|
|
817
|
+
setupResponsePlugin(instance);
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
// src/axios/service.ts
|
|
821
|
+
function setGlobalAxiosInstance(instance) {
|
|
822
|
+
setDefaultAxiosInstance(instance);
|
|
823
|
+
}
|
|
824
|
+
function getGlobalAxiosInstance() {
|
|
825
|
+
const instance = getDefaultAxiosInstance();
|
|
826
|
+
if (!instance) {
|
|
827
|
+
throw new Error(
|
|
828
|
+
"Axios instance not initialized. Please call createRequestCore() or setGlobalAxiosInstance() first."
|
|
829
|
+
);
|
|
830
|
+
}
|
|
831
|
+
return instance;
|
|
832
|
+
}
|
|
833
|
+
new Proxy({}, {
|
|
834
|
+
get(_target, prop) {
|
|
835
|
+
return getGlobalAxiosInstance()[prop];
|
|
836
|
+
}
|
|
837
|
+
});
|
|
838
|
+
async function getData(url, config) {
|
|
839
|
+
const response = await getGlobalAxiosInstance().get(url, config);
|
|
840
|
+
return response.data;
|
|
841
|
+
}
|
|
842
|
+
async function postData(url, data, config) {
|
|
843
|
+
const response = await getGlobalAxiosInstance().post(url, data, config);
|
|
844
|
+
return response.data;
|
|
845
|
+
}
|
|
846
|
+
async function putData(url, data, config) {
|
|
847
|
+
const response = await getGlobalAxiosInstance().put(url, data, config);
|
|
848
|
+
return response.data;
|
|
849
|
+
}
|
|
850
|
+
async function patchData(url, data, config) {
|
|
851
|
+
const response = await getGlobalAxiosInstance().patch(url, data, config);
|
|
852
|
+
return response.data;
|
|
853
|
+
}
|
|
854
|
+
async function deleteData(url, config) {
|
|
855
|
+
const response = await getGlobalAxiosInstance().delete(url, config);
|
|
856
|
+
return response.data;
|
|
857
|
+
}
|
|
858
|
+
function createAxiosInstance(config = {}, runtimeOptions = {}) {
|
|
859
|
+
const instance = axios2.create({
|
|
860
|
+
timeout: 5e3,
|
|
861
|
+
headers: { "Content-Type": "application/json" },
|
|
862
|
+
...config
|
|
863
|
+
});
|
|
864
|
+
const runtime = createRequestRuntime(runtimeOptions);
|
|
865
|
+
attachRequestRuntime(instance, runtime);
|
|
866
|
+
setupPlugins(instance, runtime);
|
|
867
|
+
return instance;
|
|
868
|
+
}
|
|
869
|
+
var onReLoginSuccess = (instance) => {
|
|
870
|
+
resolveReLogin(instance);
|
|
871
|
+
};
|
|
872
|
+
var onReLoginCancel = (instance) => {
|
|
873
|
+
rejectReLogin(new Error("\u91CD\u65B0\u767B\u5F55\u5DF2\u53D6\u6D88"), instance);
|
|
874
|
+
};
|
|
875
|
+
|
|
876
|
+
// src/core.ts
|
|
877
|
+
function getGlobalConfig() {
|
|
878
|
+
return getGlobalRuntimeConfig();
|
|
879
|
+
}
|
|
880
|
+
function createRequestCore(config = {}) {
|
|
881
|
+
const {
|
|
882
|
+
request = {},
|
|
883
|
+
interceptors = {},
|
|
884
|
+
successCodes,
|
|
885
|
+
fieldAliases,
|
|
886
|
+
defaults,
|
|
887
|
+
cacheStore
|
|
888
|
+
} = config;
|
|
889
|
+
setGlobalRuntimeConfig({ successCodes, fieldAliases });
|
|
890
|
+
const axiosInstance = createAxiosInstance(request, {
|
|
891
|
+
cache: cacheStore,
|
|
892
|
+
defaults
|
|
893
|
+
});
|
|
894
|
+
setGlobalAxiosInstance(axiosInstance);
|
|
895
|
+
if (interceptors.request || interceptors.requestError) {
|
|
896
|
+
axiosInstance.interceptors.request.use(
|
|
897
|
+
interceptors.request ?? ((requestConfig) => requestConfig),
|
|
898
|
+
interceptors.requestError
|
|
899
|
+
);
|
|
900
|
+
}
|
|
901
|
+
if (interceptors.response || interceptors.responseError) {
|
|
902
|
+
axiosInstance.interceptors.response.use(
|
|
903
|
+
interceptors.response ?? ((response) => response),
|
|
904
|
+
interceptors.responseError
|
|
905
|
+
);
|
|
906
|
+
}
|
|
907
|
+
return {
|
|
908
|
+
install(app) {
|
|
909
|
+
app.config.globalProperties.$axios = axiosInstance;
|
|
910
|
+
},
|
|
911
|
+
axiosInstance
|
|
912
|
+
};
|
|
913
|
+
}
|
|
914
|
+
var RequestError = class extends Error {
|
|
915
|
+
constructor(options) {
|
|
916
|
+
super(options.message);
|
|
917
|
+
this.name = "RequestError";
|
|
918
|
+
this.kind = options.kind;
|
|
919
|
+
this.code = options.code;
|
|
920
|
+
this.status = options.status;
|
|
921
|
+
this.data = options.data;
|
|
922
|
+
this.requestId = options.requestId;
|
|
923
|
+
this.retryable = options.retryable ?? false;
|
|
924
|
+
this.config = options.config;
|
|
925
|
+
this.cause = options.cause;
|
|
926
|
+
}
|
|
927
|
+
};
|
|
928
|
+
function isRequestError(error) {
|
|
929
|
+
return error instanceof RequestError;
|
|
930
|
+
}
|
|
931
|
+
function isCanceledError(error) {
|
|
932
|
+
return error instanceof RequestError && error.kind === "canceled" || axios2.isCancel(error) || error?.code === "ERR_CANCELED" || error?.name === "AbortError";
|
|
933
|
+
}
|
|
934
|
+
function normalizeRequestError(error) {
|
|
935
|
+
if (error instanceof RequestError) return error;
|
|
936
|
+
if (isCanceledError(error)) {
|
|
937
|
+
return new RequestError({
|
|
938
|
+
kind: "canceled",
|
|
939
|
+
message: "Request canceled.",
|
|
940
|
+
code: "ERR_CANCELED",
|
|
941
|
+
cause: error
|
|
942
|
+
});
|
|
943
|
+
}
|
|
944
|
+
if (axios2.isAxiosError(error)) {
|
|
945
|
+
const status = error.response?.status;
|
|
946
|
+
const timeout = error.code === "ECONNABORTED" || error.code === "ETIMEDOUT";
|
|
947
|
+
const kind = timeout ? "timeout" : status ? "http" : "network";
|
|
948
|
+
return new RequestError({
|
|
949
|
+
kind,
|
|
950
|
+
message: error.message || "Request failed.",
|
|
951
|
+
code: error.code,
|
|
952
|
+
status,
|
|
953
|
+
data: error.response?.data,
|
|
954
|
+
requestId: error.response?.headers?.["x-request-id"] ?? error.response?.headers?.["x-correlation-id"],
|
|
955
|
+
retryable: timeout || !status || status === 408 || status === 429 || status >= 500,
|
|
956
|
+
config: error.config,
|
|
957
|
+
cause: error
|
|
958
|
+
});
|
|
959
|
+
}
|
|
960
|
+
return new RequestError({
|
|
961
|
+
kind: "unknown",
|
|
962
|
+
message: error instanceof Error ? error.message : "Unknown request error.",
|
|
963
|
+
cause: error
|
|
964
|
+
});
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
export { MemoryCache, RequestError, cancelAllPendingRequests, cancelAllRequests, cancelRequestScope, cleanupExpiredCache, clearAllCache, clearCache, clearCacheByPrefix, clearCacheByTag, createAxiosInstance, createRequestCore, deleteData, generateRequestKey, getCacheSize, getCancelableRequestCount, getData, getGlobalAxiosInstance, getGlobalConfig, getPendingRequestCount, getReLoginPromise, getRequestRuntime, isCanceledError, isRequestError, normalizeRequestError, onReLoginCancel, onReLoginSuccess, patchData, postData, putData, setGlobalAxiosInstance, setGlobalRuntimeConfig, waitForReLogin };
|
|
968
|
+
//# sourceMappingURL=chunk-OAZ5BUBU.js.map
|
|
969
|
+
//# sourceMappingURL=chunk-OAZ5BUBU.js.map
|