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