@robot-admin/request-core 0.1.2 → 0.2.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 +455 -426
- package/dist/axios.cjs +816 -0
- package/dist/axios.cjs.map +1 -0
- package/dist/axios.d.cts +232 -0
- package/dist/axios.d.ts +232 -0
- package/dist/axios.js +792 -0
- package/dist/axios.js.map +1 -0
- package/dist/crud.cjs +491 -0
- package/dist/crud.cjs.map +1 -0
- package/dist/crud.d.cts +271 -0
- package/dist/crud.d.ts +271 -0
- package/dist/crud.js +489 -0
- package/dist/crud.js.map +1 -0
- package/dist/index.cjs +303 -123
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +20 -499
- package/dist/index.d.ts +20 -499
- package/dist/index.js +302 -124
- package/dist/index.js.map +1 -1
- package/package.json +26 -7
package/dist/index.cjs
CHANGED
|
@@ -10,37 +10,156 @@ var axios__default = /*#__PURE__*/_interopDefault(axios);
|
|
|
10
10
|
|
|
11
11
|
// src/axios/request.ts
|
|
12
12
|
|
|
13
|
+
// src/axios/plugins/request.ts
|
|
14
|
+
var reLoginPromise = null;
|
|
15
|
+
var reLoginResolve = null;
|
|
16
|
+
var reLoginReject = null;
|
|
17
|
+
function waitForReLogin() {
|
|
18
|
+
if (!reLoginPromise) {
|
|
19
|
+
reLoginPromise = new Promise((resolve, reject) => {
|
|
20
|
+
reLoginResolve = resolve;
|
|
21
|
+
reLoginReject = reject;
|
|
22
|
+
}).finally(() => {
|
|
23
|
+
reLoginPromise = null;
|
|
24
|
+
reLoginResolve = null;
|
|
25
|
+
reLoginReject = null;
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
return reLoginPromise;
|
|
29
|
+
}
|
|
30
|
+
function getReLoginPromise() {
|
|
31
|
+
return reLoginPromise;
|
|
32
|
+
}
|
|
33
|
+
function resolveReLogin() {
|
|
34
|
+
if (reLoginResolve) {
|
|
35
|
+
reLoginResolve();
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
function rejectReLogin(reason) {
|
|
39
|
+
if (reLoginReject) {
|
|
40
|
+
reLoginReject(reason);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// src/axios/utils/abort.ts
|
|
45
|
+
function ensureSharedAbortController(config) {
|
|
46
|
+
if (typeof AbortController === "undefined") return null;
|
|
47
|
+
const existing = config.__abortController;
|
|
48
|
+
if (existing) {
|
|
49
|
+
existing._startTime ?? (existing._startTime = Date.now());
|
|
50
|
+
config.signal = existing.signal;
|
|
51
|
+
return existing;
|
|
52
|
+
}
|
|
53
|
+
const externalSignal = config.signal;
|
|
54
|
+
const controller = new AbortController();
|
|
55
|
+
controller._startTime = Date.now();
|
|
56
|
+
if (externalSignal && externalSignal !== controller.signal) {
|
|
57
|
+
config.__externalSignal = externalSignal;
|
|
58
|
+
const forwardAbort = () => {
|
|
59
|
+
if (!controller.signal.aborted) {
|
|
60
|
+
controller.abort(externalSignal.reason);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
if (externalSignal.aborted) {
|
|
64
|
+
forwardAbort();
|
|
65
|
+
} else {
|
|
66
|
+
externalSignal.addEventListener?.("abort", forwardAbort, { once: true });
|
|
67
|
+
config.__abortCleanup = () => {
|
|
68
|
+
externalSignal.removeEventListener?.("abort", forwardAbort);
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
config.__abortController = controller;
|
|
73
|
+
config.signal = controller.signal;
|
|
74
|
+
return controller;
|
|
75
|
+
}
|
|
76
|
+
function cleanupAbortContext(config) {
|
|
77
|
+
if (!config) return;
|
|
78
|
+
config.__abortCleanup?.();
|
|
79
|
+
if (config.__externalSignal) {
|
|
80
|
+
config.signal = config.__externalSignal;
|
|
81
|
+
} else if (config.signal === config.__abortController?.signal) {
|
|
82
|
+
delete config.signal;
|
|
83
|
+
}
|
|
84
|
+
delete config.__abortCleanup;
|
|
85
|
+
delete config.__externalSignal;
|
|
86
|
+
delete config.__abortController;
|
|
87
|
+
}
|
|
88
|
+
|
|
13
89
|
// src/axios/utils/helpers.ts
|
|
90
|
+
var binaryObjectIds = /* @__PURE__ */ new WeakMap();
|
|
91
|
+
var nextBinaryObjectId = 0;
|
|
92
|
+
function getBinaryObjectId(value) {
|
|
93
|
+
let id = binaryObjectIds.get(value);
|
|
94
|
+
if (id === void 0) {
|
|
95
|
+
id = ++nextBinaryObjectId;
|
|
96
|
+
binaryObjectIds.set(value, id);
|
|
97
|
+
}
|
|
98
|
+
return id;
|
|
99
|
+
}
|
|
14
100
|
function sortedStringify(obj, seen = /* @__PURE__ */ new WeakSet()) {
|
|
15
101
|
if (obj === null || obj === void 0) {
|
|
16
102
|
return "";
|
|
17
103
|
}
|
|
104
|
+
if (typeof obj === "bigint") return `bigint:${obj.toString()}`;
|
|
18
105
|
if (typeof obj !== "object") {
|
|
19
|
-
return String(obj)
|
|
106
|
+
return `${typeof obj}:${String(obj)}`;
|
|
20
107
|
}
|
|
21
108
|
if (seen.has(obj)) {
|
|
22
109
|
throw new Error("\u68C0\u6D4B\u5230\u5FAA\u73AF\u5F15\u7528\uFF0C\u65E0\u6CD5\u751F\u6210\u7A33\u5B9A\u7684\u7F13\u5B58\u952E");
|
|
23
110
|
}
|
|
24
111
|
seen.add(obj);
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
112
|
+
try {
|
|
113
|
+
if (Array.isArray(obj)) {
|
|
114
|
+
return `[${obj.map((item) => sortedStringify(item, seen)).join(",")}]`;
|
|
115
|
+
}
|
|
116
|
+
if (obj instanceof Date) return `date:${obj.toISOString()}`;
|
|
117
|
+
if (typeof URLSearchParams !== "undefined" && obj instanceof URLSearchParams) {
|
|
118
|
+
return `url-search:${JSON.stringify(Array.from(obj.entries()).sort())}`;
|
|
119
|
+
}
|
|
120
|
+
if (typeof FormData !== "undefined" && obj instanceof FormData) {
|
|
121
|
+
const entries = Array.from(obj.entries()).map(([key, value]) => [
|
|
122
|
+
key,
|
|
123
|
+
typeof value === "string" ? `string:${value}` : `binary:${getBinaryObjectId(value)}:${value.name}:${value.size}:${value.type}`
|
|
124
|
+
]);
|
|
125
|
+
return `form-data:${JSON.stringify(entries)}`;
|
|
126
|
+
}
|
|
127
|
+
if (typeof Blob !== "undefined" && obj instanceof Blob || obj instanceof ArrayBuffer || ArrayBuffer.isView(obj)) {
|
|
128
|
+
return `binary:${getBinaryObjectId(obj)}`;
|
|
129
|
+
}
|
|
130
|
+
const source = typeof obj.toJSON === "function" && obj.constructor?.name === "AxiosHeaders" ? obj.toJSON() : obj;
|
|
131
|
+
const sortedKeys = Object.keys(source).sort();
|
|
132
|
+
return `{${sortedKeys.map((key) => `${JSON.stringify(key)}:${sortedStringify(source[key], seen)}`).join(",")}}`;
|
|
133
|
+
} finally {
|
|
134
|
+
seen.delete(obj);
|
|
32
135
|
}
|
|
33
|
-
return JSON.stringify(sortedObj);
|
|
34
136
|
}
|
|
35
137
|
function generateRequestKey(config) {
|
|
36
|
-
const { method = "get", url = "", params, data } = config;
|
|
138
|
+
const { method = "get", url = "", params, data, headers } = config;
|
|
37
139
|
const parts = [method.toUpperCase(), url];
|
|
38
|
-
if (params
|
|
140
|
+
if (params != null) {
|
|
39
141
|
parts.push(sortedStringify(params));
|
|
40
142
|
}
|
|
41
|
-
if (data
|
|
143
|
+
if (data != null) {
|
|
42
144
|
parts.push(sortedStringify(data));
|
|
43
145
|
}
|
|
146
|
+
if (headers) {
|
|
147
|
+
const authHeaders = {};
|
|
148
|
+
const authKeys = ["authorization", "x-tenant-id", "x-user-id"];
|
|
149
|
+
const lowerHeaders = {};
|
|
150
|
+
const headerSource = typeof headers.toJSON === "function" ? headers.toJSON() : headers;
|
|
151
|
+
for (const [k, v] of Object.entries(headerSource)) {
|
|
152
|
+
lowerHeaders[String(k).toLowerCase()] = v;
|
|
153
|
+
}
|
|
154
|
+
for (const key of authKeys) {
|
|
155
|
+
if (lowerHeaders[key] != null) {
|
|
156
|
+
authHeaders[key] = String(lowerHeaders[key]);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
if (Object.keys(authHeaders).length > 0) {
|
|
160
|
+
parts.push(sortedStringify(authHeaders));
|
|
161
|
+
}
|
|
162
|
+
}
|
|
44
163
|
return parts.join("|");
|
|
45
164
|
}
|
|
46
165
|
var MemoryCache = class {
|
|
@@ -70,6 +189,10 @@ var MemoryCache = class {
|
|
|
70
189
|
* 设置缓存
|
|
71
190
|
*/
|
|
72
191
|
set(key, data, ttl) {
|
|
192
|
+
if (!Number.isFinite(ttl) || ttl < 0) {
|
|
193
|
+
throw new RangeError("\u7F13\u5B58 TTL \u5FC5\u987B\u662F\u5927\u4E8E\u6216\u7B49\u4E8E 0 \u7684\u6709\u9650\u6570\u503C");
|
|
194
|
+
}
|
|
195
|
+
if (this.maxSize === 0) return;
|
|
73
196
|
if (this.cache.size >= this.maxSize && !this.cache.has(key)) {
|
|
74
197
|
this.evictOldest();
|
|
75
198
|
}
|
|
@@ -118,6 +241,9 @@ var MemoryCache = class {
|
|
|
118
241
|
* 设置最大缓存大小
|
|
119
242
|
*/
|
|
120
243
|
setMaxSize(size) {
|
|
244
|
+
if (!Number.isInteger(size) || size < 0) {
|
|
245
|
+
throw new RangeError("\u7F13\u5B58\u6700\u5927\u5BB9\u91CF\u5FC5\u987B\u662F\u5927\u4E8E\u6216\u7B49\u4E8E 0 \u7684\u6574\u6570");
|
|
246
|
+
}
|
|
121
247
|
this.maxSize = size;
|
|
122
248
|
while (this.cache.size > this.maxSize) {
|
|
123
249
|
this.evictOldest();
|
|
@@ -135,14 +261,36 @@ var MemoryCache = class {
|
|
|
135
261
|
}
|
|
136
262
|
};
|
|
137
263
|
var globalCache = new MemoryCache();
|
|
138
|
-
function delay(ms) {
|
|
139
|
-
|
|
264
|
+
function delay(ms, signal) {
|
|
265
|
+
if (signal?.aborted) {
|
|
266
|
+
return Promise.reject(createAbortError(signal.reason));
|
|
267
|
+
}
|
|
268
|
+
return new Promise((resolve, reject) => {
|
|
269
|
+
const timer = setTimeout(() => {
|
|
270
|
+
signal?.removeEventListener?.("abort", onAbort);
|
|
271
|
+
resolve();
|
|
272
|
+
}, Math.max(0, Number.isFinite(ms) ? ms : 0));
|
|
273
|
+
const onAbort = () => {
|
|
274
|
+
clearTimeout(timer);
|
|
275
|
+
signal?.removeEventListener?.("abort", onAbort);
|
|
276
|
+
reject(createAbortError(signal?.reason));
|
|
277
|
+
};
|
|
278
|
+
signal?.addEventListener?.("abort", onAbort, { once: true });
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
function createAbortError(reason) {
|
|
282
|
+
if (reason instanceof Error) return reason;
|
|
283
|
+
return Object.assign(new Error("canceled"), {
|
|
284
|
+
name: "AbortError",
|
|
285
|
+
code: "ERR_CANCELED",
|
|
286
|
+
reason
|
|
287
|
+
});
|
|
140
288
|
}
|
|
141
289
|
function isNetworkError(error) {
|
|
142
290
|
return !error.response && Boolean(error.code) && error.code !== "ECONNABORTED" && error.code !== "ERR_CANCELED" && error.message !== "canceled" && error.message !== "Request aborted" && error.message !== "Request cancelled";
|
|
143
291
|
}
|
|
144
292
|
function isTimeoutError(error) {
|
|
145
|
-
return error
|
|
293
|
+
return error?.code === "ECONNABORTED" || error?.code === "ETIMEDOUT";
|
|
146
294
|
}
|
|
147
295
|
function isRetryableStatus(status, retryableStatusCodes) {
|
|
148
296
|
return retryableStatusCodes.includes(status);
|
|
@@ -154,8 +302,11 @@ function normalizeConfig(config, defaults) {
|
|
|
154
302
|
if (config === false) {
|
|
155
303
|
return { ...defaults, enabled: false };
|
|
156
304
|
}
|
|
157
|
-
if (typeof config === "object") {
|
|
158
|
-
|
|
305
|
+
if (config && typeof config === "object") {
|
|
306
|
+
const definedEntries = Object.entries(config).filter(
|
|
307
|
+
([, value]) => value !== void 0
|
|
308
|
+
);
|
|
309
|
+
return { ...defaults, ...Object.fromEntries(definedEntries) };
|
|
159
310
|
}
|
|
160
311
|
return defaults;
|
|
161
312
|
}
|
|
@@ -166,12 +317,14 @@ var DEFAULT_DEDUPE_CONFIG = {
|
|
|
166
317
|
keyGenerator: generateRequestKey
|
|
167
318
|
};
|
|
168
319
|
var pendingRequests = /* @__PURE__ */ new Map();
|
|
320
|
+
var CLEANUP_INTERVAL = 3e4;
|
|
321
|
+
var REQUEST_TIMEOUT = 5 * 60 * 1e3;
|
|
322
|
+
var cleanupTimer = null;
|
|
169
323
|
function cleanupExpiredRequests() {
|
|
170
324
|
const now = Date.now();
|
|
171
|
-
const TIMEOUT = 5 * 60 * 1e3;
|
|
172
325
|
Array.from(pendingRequests.entries()).forEach(([key, controller]) => {
|
|
173
326
|
const startTime = controller._startTime || now;
|
|
174
|
-
if (now - startTime >
|
|
327
|
+
if (now - startTime > REQUEST_TIMEOUT) {
|
|
175
328
|
try {
|
|
176
329
|
controller.abort();
|
|
177
330
|
} catch (error) {
|
|
@@ -180,9 +333,26 @@ function cleanupExpiredRequests() {
|
|
|
180
333
|
pendingRequests.delete(key);
|
|
181
334
|
}
|
|
182
335
|
});
|
|
336
|
+
stopCleanupTimerIfIdle();
|
|
337
|
+
}
|
|
338
|
+
function startCleanupTimer() {
|
|
339
|
+
if (cleanupTimer) return;
|
|
340
|
+
cleanupTimer = setInterval(cleanupExpiredRequests, CLEANUP_INTERVAL);
|
|
341
|
+
cleanupTimer.unref?.();
|
|
342
|
+
}
|
|
343
|
+
function stopCleanupTimerIfIdle() {
|
|
344
|
+
if (pendingRequests.size === 0 && cleanupTimer) {
|
|
345
|
+
clearInterval(cleanupTimer);
|
|
346
|
+
cleanupTimer = null;
|
|
347
|
+
}
|
|
183
348
|
}
|
|
184
|
-
|
|
185
|
-
|
|
349
|
+
function removePendingRequest(config) {
|
|
350
|
+
const requestKey = config.__requestKey;
|
|
351
|
+
const controller = config.__abortController;
|
|
352
|
+
if (requestKey && controller && pendingRequests.get(requestKey) === controller) {
|
|
353
|
+
pendingRequests.delete(requestKey);
|
|
354
|
+
}
|
|
355
|
+
stopCleanupTimerIfIdle();
|
|
186
356
|
}
|
|
187
357
|
function onRequest(config) {
|
|
188
358
|
const enhancedConfig = config;
|
|
@@ -207,40 +377,22 @@ function onRequest(config) {
|
|
|
207
377
|
}
|
|
208
378
|
pendingRequests.delete(requestKey);
|
|
209
379
|
}
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
const controller = new AbortController();
|
|
214
|
-
controller._startTime = Date.now();
|
|
215
|
-
config.signal = controller.signal;
|
|
380
|
+
const controller = ensureSharedAbortController(enhancedConfig);
|
|
381
|
+
if (!controller) return config;
|
|
382
|
+
enhancedConfig.__requestKey = requestKey;
|
|
216
383
|
pendingRequests.set(requestKey, controller);
|
|
384
|
+
startCleanupTimer();
|
|
217
385
|
return config;
|
|
218
386
|
}
|
|
219
387
|
function onResponse(response) {
|
|
220
388
|
const config = response.config;
|
|
221
|
-
|
|
222
|
-
config.dedupe,
|
|
223
|
-
DEFAULT_DEDUPE_CONFIG
|
|
224
|
-
);
|
|
225
|
-
if (dedupeConfig.enabled) {
|
|
226
|
-
const keyGenerator = dedupeConfig.keyGenerator || DEFAULT_DEDUPE_CONFIG.keyGenerator;
|
|
227
|
-
const requestKey = keyGenerator(config);
|
|
228
|
-
pendingRequests.delete(requestKey);
|
|
229
|
-
}
|
|
389
|
+
removePendingRequest(config);
|
|
230
390
|
return response;
|
|
231
391
|
}
|
|
232
392
|
function onResponseError(error) {
|
|
233
393
|
const config = error.config;
|
|
234
394
|
if (config) {
|
|
235
|
-
|
|
236
|
-
config.dedupe,
|
|
237
|
-
DEFAULT_DEDUPE_CONFIG
|
|
238
|
-
);
|
|
239
|
-
if (dedupeConfig.enabled) {
|
|
240
|
-
const keyGenerator = dedupeConfig.keyGenerator || DEFAULT_DEDUPE_CONFIG.keyGenerator;
|
|
241
|
-
const requestKey = keyGenerator(config);
|
|
242
|
-
pendingRequests.delete(requestKey);
|
|
243
|
-
}
|
|
395
|
+
removePendingRequest(config);
|
|
244
396
|
}
|
|
245
397
|
return Promise.reject(error);
|
|
246
398
|
}
|
|
@@ -257,11 +409,7 @@ function cancelAllPendingRequests() {
|
|
|
257
409
|
}
|
|
258
410
|
});
|
|
259
411
|
pendingRequests.clear();
|
|
260
|
-
|
|
261
|
-
if (typeof window !== "undefined") {
|
|
262
|
-
window.addEventListener("beforeunload", () => {
|
|
263
|
-
cancelAllPendingRequests();
|
|
264
|
-
});
|
|
412
|
+
stopCleanupTimerIfIdle();
|
|
265
413
|
}
|
|
266
414
|
function getPendingRequestCount() {
|
|
267
415
|
return pendingRequests.size;
|
|
@@ -378,10 +526,13 @@ var DEFAULT_RETRY_CONFIG = {
|
|
|
378
526
|
count: 3,
|
|
379
527
|
delay: 1e3,
|
|
380
528
|
exponentialBackoff: true,
|
|
381
|
-
|
|
529
|
+
jitter: true,
|
|
530
|
+
retryableStatusCodes: [408, 429, 500, 502, 503, 504],
|
|
531
|
+
// 默认仅重试幂等方法,避免对 POST 等非幂等请求重复执行造成副作用(重复扣款/重复创建)
|
|
532
|
+
retryableMethods: ["GET", "HEAD", "OPTIONS", "PUT", "DELETE"]
|
|
382
533
|
};
|
|
383
534
|
function isCancelError(error) {
|
|
384
|
-
return error
|
|
535
|
+
return error?.name === "CanceledError" || error?.name === "AbortError" || error?.code === "ERR_CANCELED";
|
|
385
536
|
}
|
|
386
537
|
function shouldRetry(error, retryConfig) {
|
|
387
538
|
if (!retryConfig.enabled) {
|
|
@@ -395,6 +546,12 @@ function shouldRetry(error, retryConfig) {
|
|
|
395
546
|
if (isCancelError(error)) {
|
|
396
547
|
return false;
|
|
397
548
|
}
|
|
549
|
+
const method = (config.method || "get").toUpperCase();
|
|
550
|
+
if (!retryConfig.retryableMethods.some(
|
|
551
|
+
(retryableMethod) => retryableMethod.toUpperCase() === method
|
|
552
|
+
)) {
|
|
553
|
+
return false;
|
|
554
|
+
}
|
|
398
555
|
if (isNetworkError(error)) {
|
|
399
556
|
return true;
|
|
400
557
|
}
|
|
@@ -410,11 +567,17 @@ function shouldRetry(error, retryConfig) {
|
|
|
410
567
|
return false;
|
|
411
568
|
}
|
|
412
569
|
function getRetryDelay(retryCount, retryConfig) {
|
|
570
|
+
let calculated;
|
|
413
571
|
if (!retryConfig.exponentialBackoff) {
|
|
414
|
-
|
|
572
|
+
calculated = retryConfig.delay;
|
|
573
|
+
} else {
|
|
574
|
+
calculated = retryConfig.delay * Math.pow(2, retryCount - 1);
|
|
575
|
+
}
|
|
576
|
+
if (retryConfig.jitter) {
|
|
577
|
+
const factor = 0.75 + Math.random() * 0.5;
|
|
578
|
+
calculated = Math.round(calculated * factor);
|
|
415
579
|
}
|
|
416
|
-
|
|
417
|
-
return Math.min(calculatedDelay, 3e4);
|
|
580
|
+
return Math.min(Math.max(0, calculated), 3e4);
|
|
418
581
|
}
|
|
419
582
|
function setupRetryPlugin(instance) {
|
|
420
583
|
const onResponseError4 = async (error) => {
|
|
@@ -431,24 +594,39 @@ function setupRetryPlugin(instance) {
|
|
|
431
594
|
}
|
|
432
595
|
config.__retryCount = (config.__retryCount ?? 0) + 1;
|
|
433
596
|
const retryDelay = getRetryDelay(config.__retryCount, retryConfig);
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
597
|
+
try {
|
|
598
|
+
await delay(retryDelay, config.signal);
|
|
599
|
+
} catch (abortError) {
|
|
600
|
+
const error_ = Object.assign(new Error("canceled"), {
|
|
601
|
+
name: "CanceledError",
|
|
602
|
+
code: "ERR_CANCELED",
|
|
603
|
+
config,
|
|
604
|
+
cause: abortError
|
|
441
605
|
});
|
|
606
|
+
return Promise.reject(error_);
|
|
442
607
|
}
|
|
443
|
-
retryConfig_
|
|
608
|
+
const retryConfig_ = { ...config };
|
|
444
609
|
delete retryConfig_.__cancelId;
|
|
445
|
-
delete retryConfig_.
|
|
446
|
-
delete retryConfig_.__handling401;
|
|
610
|
+
delete retryConfig_.__requestKey;
|
|
447
611
|
return instance.request(retryConfig_);
|
|
448
612
|
};
|
|
449
613
|
instance.interceptors.response.use(void 0, onResponseError4);
|
|
450
614
|
}
|
|
451
615
|
|
|
616
|
+
// src/axios/plugins/response.ts
|
|
617
|
+
function setupResponsePlugin(instance) {
|
|
618
|
+
instance.interceptors.response.use(
|
|
619
|
+
(response) => {
|
|
620
|
+
cleanupAbortContext(response.config);
|
|
621
|
+
return response;
|
|
622
|
+
},
|
|
623
|
+
(error) => {
|
|
624
|
+
cleanupAbortContext(error?.config);
|
|
625
|
+
return Promise.reject(error);
|
|
626
|
+
}
|
|
627
|
+
);
|
|
628
|
+
}
|
|
629
|
+
|
|
452
630
|
// src/axios/plugins/cancel.ts
|
|
453
631
|
var DEFAULT_CANCEL_CONFIG = {
|
|
454
632
|
enabled: true,
|
|
@@ -456,15 +634,15 @@ var DEFAULT_CANCEL_CONFIG = {
|
|
|
456
634
|
};
|
|
457
635
|
var cancelableRequests = /* @__PURE__ */ new Map();
|
|
458
636
|
var requestId = 0;
|
|
459
|
-
var
|
|
460
|
-
var
|
|
461
|
-
var
|
|
637
|
+
var CLEANUP_INTERVAL2 = 3e4;
|
|
638
|
+
var REQUEST_TIMEOUT2 = 3e5;
|
|
639
|
+
var cleanupTimer2 = null;
|
|
462
640
|
function cleanupExpiredRequests2() {
|
|
463
641
|
const now = Date.now();
|
|
464
642
|
const expiredKeys = [];
|
|
465
643
|
for (const [key, controller] of cancelableRequests.entries()) {
|
|
466
644
|
const requestStartTime = controller._startTime || now;
|
|
467
|
-
if (now - requestStartTime >
|
|
645
|
+
if (now - requestStartTime > REQUEST_TIMEOUT2) {
|
|
468
646
|
expiredKeys.push(key);
|
|
469
647
|
try {
|
|
470
648
|
controller.abort();
|
|
@@ -474,27 +652,26 @@ function cleanupExpiredRequests2() {
|
|
|
474
652
|
}
|
|
475
653
|
}
|
|
476
654
|
expiredKeys.forEach((key) => cancelableRequests.delete(key));
|
|
655
|
+
stopCleanupTimerIfIdle2();
|
|
477
656
|
}
|
|
478
|
-
function
|
|
479
|
-
if (
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
cleanupTimer = setInterval(cleanupExpiredRequests2, CLEANUP_INTERVAL);
|
|
657
|
+
function startCleanupTimer2() {
|
|
658
|
+
if (cleanupTimer2) return;
|
|
659
|
+
cleanupTimer2 = setInterval(cleanupExpiredRequests2, CLEANUP_INTERVAL2);
|
|
660
|
+
cleanupTimer2.unref?.();
|
|
483
661
|
}
|
|
484
662
|
function stopCleanupTimer() {
|
|
485
|
-
if (
|
|
486
|
-
clearInterval(
|
|
487
|
-
|
|
663
|
+
if (cleanupTimer2) {
|
|
664
|
+
clearInterval(cleanupTimer2);
|
|
665
|
+
cleanupTimer2 = null;
|
|
488
666
|
}
|
|
489
667
|
}
|
|
668
|
+
function stopCleanupTimerIfIdle2() {
|
|
669
|
+
if (cancelableRequests.size === 0) stopCleanupTimer();
|
|
670
|
+
}
|
|
490
671
|
function isInWhitelist(url, whitelist) {
|
|
491
672
|
return whitelist.some((pattern) => pattern.test(url));
|
|
492
673
|
}
|
|
493
674
|
function onRequest3(config) {
|
|
494
|
-
if (typeof AbortController === "undefined") {
|
|
495
|
-
console.warn("AbortController is not supported in this environment");
|
|
496
|
-
return config;
|
|
497
|
-
}
|
|
498
675
|
const enhancedConfig = config;
|
|
499
676
|
const cancelConfig = normalizeConfig(
|
|
500
677
|
enhancedConfig.cancel,
|
|
@@ -510,12 +687,12 @@ function onRequest3(config) {
|
|
|
510
687
|
if (isInWhitelist(url, cancelConfig.whitelist)) {
|
|
511
688
|
return config;
|
|
512
689
|
}
|
|
513
|
-
const
|
|
690
|
+
const activeController = ensureSharedAbortController(enhancedConfig);
|
|
691
|
+
if (!activeController) return config;
|
|
514
692
|
const id = `request_${++requestId}`;
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
cancelableRequests.set(id, controller);
|
|
693
|
+
enhancedConfig.__cancelId = id;
|
|
694
|
+
cancelableRequests.set(id, activeController);
|
|
695
|
+
startCleanupTimer2();
|
|
519
696
|
return config;
|
|
520
697
|
}
|
|
521
698
|
function onResponse3(response) {
|
|
@@ -523,6 +700,7 @@ function onResponse3(response) {
|
|
|
523
700
|
const cancelId = config.__cancelId;
|
|
524
701
|
if (cancelId) {
|
|
525
702
|
cancelableRequests.delete(cancelId);
|
|
703
|
+
stopCleanupTimerIfIdle2();
|
|
526
704
|
}
|
|
527
705
|
return response;
|
|
528
706
|
}
|
|
@@ -531,6 +709,7 @@ function onResponseError3(error) {
|
|
|
531
709
|
const cancelId = config?.__cancelId;
|
|
532
710
|
if (cancelId) {
|
|
533
711
|
cancelableRequests.delete(cancelId);
|
|
712
|
+
stopCleanupTimerIfIdle2();
|
|
534
713
|
}
|
|
535
714
|
return Promise.reject(error);
|
|
536
715
|
}
|
|
@@ -547,13 +726,7 @@ function cancelAllRequests() {
|
|
|
547
726
|
}
|
|
548
727
|
});
|
|
549
728
|
cancelableRequests.clear();
|
|
550
|
-
|
|
551
|
-
if (typeof window !== "undefined") {
|
|
552
|
-
window.addEventListener("beforeunload", () => {
|
|
553
|
-
cancelAllRequests();
|
|
554
|
-
stopCleanupTimer();
|
|
555
|
-
});
|
|
556
|
-
startCleanupTimer();
|
|
729
|
+
stopCleanupTimer();
|
|
557
730
|
}
|
|
558
731
|
function getCancelableRequestCount() {
|
|
559
732
|
return cancelableRequests.size;
|
|
@@ -565,56 +738,61 @@ function setupPlugins(instance) {
|
|
|
565
738
|
setupCancelPlugin(instance);
|
|
566
739
|
setupDedupePlugin(instance);
|
|
567
740
|
setupRetryPlugin(instance);
|
|
741
|
+
setupResponsePlugin(instance);
|
|
568
742
|
}
|
|
569
743
|
|
|
570
|
-
// src/axios/
|
|
744
|
+
// src/axios/service.ts
|
|
571
745
|
var globalService = null;
|
|
572
|
-
function createAxiosInstance(config = {}) {
|
|
573
|
-
const instance = axios__default.default.create({
|
|
574
|
-
timeout: 5e3,
|
|
575
|
-
headers: {
|
|
576
|
-
"Content-Type": "application/json"
|
|
577
|
-
},
|
|
578
|
-
...config
|
|
579
|
-
});
|
|
580
|
-
setupPlugins(instance);
|
|
581
|
-
return instance;
|
|
582
|
-
}
|
|
583
746
|
function setGlobalAxiosInstance(instance) {
|
|
584
747
|
globalService = instance;
|
|
585
748
|
}
|
|
586
749
|
function getGlobalAxiosInstance() {
|
|
587
750
|
if (!globalService) {
|
|
588
751
|
throw new Error(
|
|
589
|
-
"Axios instance not initialized. Please call createRequestCore() first."
|
|
752
|
+
"Axios instance not initialized. Please call createRequestCore() or setGlobalAxiosInstance() first."
|
|
590
753
|
);
|
|
591
754
|
}
|
|
592
755
|
return globalService;
|
|
593
756
|
}
|
|
594
757
|
new Proxy({}, {
|
|
595
|
-
get(
|
|
758
|
+
get(_target, prop) {
|
|
596
759
|
return getGlobalAxiosInstance()[prop];
|
|
597
760
|
}
|
|
598
761
|
});
|
|
599
|
-
|
|
600
|
-
const
|
|
601
|
-
return
|
|
602
|
-
}
|
|
603
|
-
|
|
604
|
-
const
|
|
605
|
-
return
|
|
606
|
-
}
|
|
607
|
-
|
|
608
|
-
const
|
|
609
|
-
return
|
|
610
|
-
}
|
|
611
|
-
|
|
612
|
-
const
|
|
613
|
-
return
|
|
614
|
-
}
|
|
762
|
+
async function getData(url, config) {
|
|
763
|
+
const response = await getGlobalAxiosInstance().get(url, config);
|
|
764
|
+
return response.data;
|
|
765
|
+
}
|
|
766
|
+
async function postData(url, data, config) {
|
|
767
|
+
const response = await getGlobalAxiosInstance().post(url, data, config);
|
|
768
|
+
return response.data;
|
|
769
|
+
}
|
|
770
|
+
async function putData(url, data, config) {
|
|
771
|
+
const response = await getGlobalAxiosInstance().put(url, data, config);
|
|
772
|
+
return response.data;
|
|
773
|
+
}
|
|
774
|
+
async function deleteData(url, config) {
|
|
775
|
+
const response = await getGlobalAxiosInstance().delete(url, config);
|
|
776
|
+
return response.data;
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
// src/axios/request.ts
|
|
780
|
+
function createAxiosInstance(config = {}) {
|
|
781
|
+
const instance = axios__default.default.create({
|
|
782
|
+
timeout: 5e3,
|
|
783
|
+
headers: {
|
|
784
|
+
"Content-Type": "application/json"
|
|
785
|
+
},
|
|
786
|
+
...config
|
|
787
|
+
});
|
|
788
|
+
setupPlugins(instance);
|
|
789
|
+
return instance;
|
|
790
|
+
}
|
|
615
791
|
var onReLoginSuccess = () => {
|
|
792
|
+
resolveReLogin();
|
|
616
793
|
};
|
|
617
794
|
var onReLoginCancel = () => {
|
|
795
|
+
rejectReLogin(new Error("\u91CD\u65B0\u767B\u5F55\u5DF2\u53D6\u6D88"));
|
|
618
796
|
};
|
|
619
797
|
|
|
620
798
|
// src/core.ts
|
|
@@ -1121,10 +1299,12 @@ exports.getCancelableRequestCount = getCancelableRequestCount;
|
|
|
1121
1299
|
exports.getData = getData;
|
|
1122
1300
|
exports.getGlobalConfig = getGlobalConfig;
|
|
1123
1301
|
exports.getPendingRequestCount = getPendingRequestCount;
|
|
1302
|
+
exports.getReLoginPromise = getReLoginPromise;
|
|
1124
1303
|
exports.onReLoginCancel = onReLoginCancel;
|
|
1125
1304
|
exports.onReLoginSuccess = onReLoginSuccess;
|
|
1126
1305
|
exports.postData = postData;
|
|
1127
1306
|
exports.putData = putData;
|
|
1128
1307
|
exports.useTableCrud = useTableCrud;
|
|
1308
|
+
exports.waitForReLogin = waitForReLogin;
|
|
1129
1309
|
//# sourceMappingURL=index.cjs.map
|
|
1130
1310
|
//# sourceMappingURL=index.cjs.map
|