@npora/request 1.2.0 → 1.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/dist/index.cjs CHANGED
@@ -23,6 +23,7 @@ __export(index_exports, {
23
23
  Client: () => Client,
24
24
  FetchAdapter: () => FetchAdapter,
25
25
  InterceptorManager: () => InterceptorManager,
26
+ MemoryCacheStore: () => MemoryCacheStore,
26
27
  MockAdapter: () => MockAdapter,
27
28
  PluginError: () => PluginError,
28
29
  RequestError: () => RequestError,
@@ -468,35 +469,370 @@ function defaultValidateStatus(status) {
468
469
  var MockAdapter = class {
469
470
  constructor(options = {}) {
470
471
  this.handlers = /* @__PURE__ */ new Map();
472
+ this.rules = [];
473
+ this.requests = [];
474
+ this.delay = normalizeDelay(options.delay);
471
475
  Object.entries(options.handlers ?? {}).forEach(([key, handler]) => {
472
476
  this.handlers.set(key, handler);
473
477
  });
474
478
  }
479
+ get history() {
480
+ return [...this.requests];
481
+ }
482
+ /**
483
+ * Register a legacy URL-only data handler.
484
+ */
475
485
  on(url, handler) {
476
486
  this.handlers.set(url, handler);
477
487
  return this;
478
488
  }
489
+ onMethod(method, matcher) {
490
+ const normalizedMatcher = normalizeMatcher(matcher);
491
+ const registrar = {
492
+ reply: (once2, replyOrStatus, data, options) => {
493
+ return this.registerReply(
494
+ method,
495
+ normalizedMatcher,
496
+ once2,
497
+ replyOrStatus,
498
+ data,
499
+ options
500
+ );
501
+ },
502
+ error: (once2, code, message) => {
503
+ return this.registerError(
504
+ method,
505
+ normalizedMatcher,
506
+ once2,
507
+ code,
508
+ message
509
+ );
510
+ }
511
+ };
512
+ return new RouteBuilder(registrar);
513
+ }
514
+ onGet(matcher) {
515
+ return this.onMethod("GET", matcher);
516
+ }
517
+ onPost(matcher) {
518
+ return this.onMethod("POST", matcher);
519
+ }
520
+ onPut(matcher) {
521
+ return this.onMethod("PUT", matcher);
522
+ }
523
+ onPatch(matcher) {
524
+ return this.onMethod("PATCH", matcher);
525
+ }
526
+ onDelete(matcher) {
527
+ return this.onMethod("DELETE", matcher);
528
+ }
529
+ onHead(matcher) {
530
+ return this.onMethod("HEAD", matcher);
531
+ }
532
+ onOptions(matcher) {
533
+ return this.onMethod("OPTIONS", matcher);
534
+ }
535
+ resetHistory() {
536
+ this.requests.length = 0;
537
+ return this;
538
+ }
539
+ resetHandlers() {
540
+ this.handlers.clear();
541
+ this.rules.length = 0;
542
+ return this;
543
+ }
544
+ reset() {
545
+ return this.resetHandlers().resetHistory();
546
+ }
479
547
  async request(config) {
548
+ this.requests.push(config);
549
+ const method = config.method ?? "GET";
550
+ const ruleIndex = this.rules.findIndex((rule) => {
551
+ return (rule.method === void 0 || rule.method === method) && matchesRequest(rule.matcher, config);
552
+ });
553
+ if (ruleIndex !== -1) {
554
+ const rule = this.rules[ruleIndex];
555
+ if (rule.once) {
556
+ this.rules.splice(ruleIndex, 1);
557
+ }
558
+ const response = await rule.respond(config);
559
+ return validateMockResponse(response, config);
560
+ }
480
561
  const handler = this.handlers.get(config.url);
481
562
  if (!handler) {
482
- throw new Error(`No mock handler found for ${config.url}`);
563
+ throw new Error(
564
+ `No mock handler found for ${config.url} (${method})`
565
+ );
483
566
  }
567
+ await waitForDelay(this.delay, config);
484
568
  const data = await handler(config);
485
- return {
486
- data,
569
+ return validateMockResponse(createMockResponse(config, {
487
570
  status: 200,
488
571
  statusText: "OK",
489
- headers: new Headers(),
490
- config,
491
- raw: new Response(JSON.stringify(data), {
492
- status: 200,
493
- headers: {
494
- "content-type": "application/json"
495
- }
496
- })
572
+ data
573
+ }), config);
574
+ }
575
+ registerReply(method, matcher, once2, replyOrStatus, data, options = {}) {
576
+ const handler = typeof replyOrStatus === "function" ? replyOrStatus : () => ({
577
+ status: replyOrStatus,
578
+ data,
579
+ ...options
580
+ });
581
+ this.rules.push({
582
+ method,
583
+ matcher,
584
+ once: once2,
585
+ respond: async (config) => {
586
+ const reply = await handler(config);
587
+ await waitForDelay(
588
+ normalizeDelay(reply.delay ?? this.delay),
589
+ config
590
+ );
591
+ return createMockResponse(config, reply);
592
+ }
593
+ });
594
+ return this;
595
+ }
596
+ registerError(method, matcher, once2, code, message) {
597
+ this.rules.push({
598
+ method,
599
+ matcher,
600
+ once: once2,
601
+ respond: async (config) => {
602
+ await waitForDelay(this.delay, config);
603
+ throw new RequestError(message, {
604
+ code,
605
+ config
606
+ });
607
+ }
608
+ });
609
+ return this;
610
+ }
611
+ };
612
+ var RouteBuilder = class {
613
+ constructor(registrar) {
614
+ this.registrar = registrar;
615
+ }
616
+ reply(replyOrStatus, data, options = {}) {
617
+ return this.registrar.reply(
618
+ false,
619
+ replyOrStatus,
620
+ data,
621
+ options
622
+ );
623
+ }
624
+ replyOnce(replyOrStatus, data, options = {}) {
625
+ return this.registrar.reply(
626
+ true,
627
+ replyOrStatus,
628
+ data,
629
+ options
630
+ );
631
+ }
632
+ networkError(message = "Mock network error") {
633
+ return this.registrar.error(
634
+ false,
635
+ "NETWORK_ERROR",
636
+ message
637
+ );
638
+ }
639
+ networkErrorOnce(message = "Mock network error") {
640
+ return this.registrar.error(
641
+ true,
642
+ "NETWORK_ERROR",
643
+ message
644
+ );
645
+ }
646
+ timeout(message = "Mock request timeout") {
647
+ return this.registrar.error(
648
+ false,
649
+ "TIMEOUT_ERROR",
650
+ message
651
+ );
652
+ }
653
+ timeoutOnce(message = "Mock request timeout") {
654
+ return this.registrar.error(
655
+ true,
656
+ "TIMEOUT_ERROR",
657
+ message
658
+ );
659
+ }
660
+ };
661
+ function normalizeMatcher(matcher) {
662
+ if (typeof matcher === "string" || matcher instanceof RegExp) {
663
+ return {
664
+ url: matcher
497
665
  };
498
666
  }
667
+ return matcher;
668
+ }
669
+ function matchesRequest(matcher, config) {
670
+ return matchesURL(matcher.url, config.url) && matchesQuery(matcher.query, config.query) && matchesHeaders(matcher.headers, config.headers);
671
+ }
672
+ function matchesURL(matcher, url) {
673
+ if (typeof matcher === "string") {
674
+ return matcher === url;
675
+ }
676
+ matcher.lastIndex = 0;
677
+ return matcher.test(url);
678
+ }
679
+ function matchesQuery(expected, actual) {
680
+ if (!expected) {
681
+ return true;
682
+ }
683
+ return normalizeQuery(expected) === normalizeQuery(actual);
684
+ }
685
+ function normalizeQuery(query) {
686
+ if (!query) {
687
+ return "[]";
688
+ }
689
+ const entries = [];
690
+ for (const key of Object.keys(query).sort()) {
691
+ const value = query[key];
692
+ const values = Array.isArray(value) ? value : [value];
693
+ for (const item of values) {
694
+ if (item !== null && item !== void 0) {
695
+ entries.push([key, String(item)]);
696
+ }
697
+ }
698
+ }
699
+ return JSON.stringify(entries);
700
+ }
701
+ function matchesHeaders(expected, actual) {
702
+ if (!expected) {
703
+ return true;
704
+ }
705
+ const expectedHeaders = new Headers(expected);
706
+ const actualHeaders = new Headers(actual);
707
+ for (const [name, value] of expectedHeaders.entries()) {
708
+ if (actualHeaders.get(name) !== value) {
709
+ return false;
710
+ }
711
+ }
712
+ return true;
713
+ }
714
+ function createMockResponse(config, reply) {
715
+ const headers = new Headers(reply.headers);
716
+ const rawBody = createRawBody(reply.data, headers, reply.status);
717
+ const statusText = reply.statusText ?? defaultStatusText(reply.status);
718
+ const raw = new Response(rawBody, {
719
+ status: reply.status,
720
+ statusText,
721
+ headers
722
+ });
723
+ return {
724
+ data: reply.data,
725
+ status: reply.status,
726
+ statusText,
727
+ headers: raw.headers,
728
+ config,
729
+ raw
730
+ };
731
+ }
732
+ function validateMockResponse(response, config) {
733
+ const validateStatus = config.validateStatus ?? defaultValidateStatus2;
734
+ if (!validateStatus(response.status)) {
735
+ throw new RequestError(
736
+ response.statusText || "Mock request failed",
737
+ {
738
+ code: "HTTP_ERROR",
739
+ response
740
+ }
741
+ );
742
+ }
743
+ return response;
744
+ }
745
+ function defaultValidateStatus2(status) {
746
+ return status >= 200 && status < 300;
747
+ }
748
+ function defaultStatusText(status) {
749
+ return STATUS_TEXT[status] ?? "";
750
+ }
751
+ var STATUS_TEXT = {
752
+ 200: "OK",
753
+ 201: "Created",
754
+ 202: "Accepted",
755
+ 204: "No Content",
756
+ 400: "Bad Request",
757
+ 401: "Unauthorized",
758
+ 403: "Forbidden",
759
+ 404: "Not Found",
760
+ 408: "Request Timeout",
761
+ 409: "Conflict",
762
+ 422: "Unprocessable Content",
763
+ 429: "Too Many Requests",
764
+ 500: "Internal Server Error",
765
+ 502: "Bad Gateway",
766
+ 503: "Service Unavailable",
767
+ 504: "Gateway Timeout"
499
768
  };
769
+ function createRawBody(data, headers, status) {
770
+ if (data === void 0 || data === null || status === 204 || status === 205 || status === 304) {
771
+ return null;
772
+ }
773
+ if (typeof data === "string" || isBodyInit(data)) {
774
+ return data;
775
+ }
776
+ if (!headers.has("content-type")) {
777
+ headers.set("content-type", "application/json");
778
+ }
779
+ return JSON.stringify(data);
780
+ }
781
+ function isBodyInit(value) {
782
+ return typeof Blob !== "undefined" && value instanceof Blob || typeof FormData !== "undefined" && value instanceof FormData || typeof URLSearchParams !== "undefined" && value instanceof URLSearchParams || typeof ReadableStream !== "undefined" && value instanceof ReadableStream || value instanceof ArrayBuffer || ArrayBuffer.isView(value);
783
+ }
784
+ function normalizeDelay(delay) {
785
+ if (!Number.isFinite(delay)) {
786
+ return delay && delay > 0 ? MAX_TIMER_DELAY : 0;
787
+ }
788
+ return Math.min(Math.max(0, delay ?? 0), MAX_TIMER_DELAY);
789
+ }
790
+ var MAX_TIMER_DELAY = 2147483647;
791
+ function waitForDelay(delay, config) {
792
+ const signal = config.signal;
793
+ if (signal?.aborted) {
794
+ return Promise.reject(createAbortError(signal.reason, config));
795
+ }
796
+ const timeout = config.timeout && config.timeout > 0 ? config.timeout : void 0;
797
+ const timeoutWins = timeout !== void 0 && timeout <= delay;
798
+ const wait = timeoutWins ? timeout : delay;
799
+ if (wait <= 0) {
800
+ return Promise.resolve();
801
+ }
802
+ return new Promise((resolve, reject) => {
803
+ const timer = setTimeout(() => {
804
+ cleanup();
805
+ if (timeoutWins) {
806
+ reject(
807
+ new RequestError(`Request timeout after ${timeout}ms`, {
808
+ code: "TIMEOUT_ERROR",
809
+ config
810
+ })
811
+ );
812
+ } else {
813
+ resolve();
814
+ }
815
+ }, wait);
816
+ const onAbort = () => {
817
+ clearTimeout(timer);
818
+ cleanup();
819
+ reject(createAbortError(signal?.reason, config));
820
+ };
821
+ const cleanup = () => {
822
+ signal?.removeEventListener("abort", onAbort);
823
+ };
824
+ signal?.addEventListener("abort", onAbort, {
825
+ once: true
826
+ });
827
+ });
828
+ }
829
+ function createAbortError(reason, config) {
830
+ return new RequestError("Mock request aborted", {
831
+ code: "ABORT_ERROR",
832
+ config,
833
+ cause: reason
834
+ });
835
+ }
500
836
 
501
837
  // src/core/ConfigMerger.ts
502
838
  var ConfigMerger = class {
@@ -771,6 +1107,12 @@ var Pipeline = class {
771
1107
  }
772
1108
  } finally {
773
1109
  context.endTime = Date.now();
1110
+ if (this.hooks.hasSettledHooks) {
1111
+ try {
1112
+ await this.hooks.runSettled(context);
1113
+ } catch {
1114
+ }
1115
+ }
774
1116
  }
775
1117
  }
776
1118
  async processResponse(context) {
@@ -814,7 +1156,7 @@ var Pipeline = class {
814
1156
  function waitForRetry(milliseconds, config) {
815
1157
  const signal = config.signal;
816
1158
  if (signal?.aborted) {
817
- return Promise.reject(createAbortError(signal.reason, config));
1159
+ return Promise.reject(createAbortError2(signal.reason, config));
818
1160
  }
819
1161
  if (milliseconds <= 0) {
820
1162
  return Promise.resolve();
@@ -827,7 +1169,7 @@ function waitForRetry(milliseconds, config) {
827
1169
  const onAbort = () => {
828
1170
  clearTimeout(timer);
829
1171
  cleanup();
830
- reject(createAbortError(signal?.reason, config));
1172
+ reject(createAbortError2(signal?.reason, config));
831
1173
  };
832
1174
  const cleanup = () => {
833
1175
  signal?.removeEventListener("abort", onAbort);
@@ -837,7 +1179,7 @@ function waitForRetry(milliseconds, config) {
837
1179
  });
838
1180
  });
839
1181
  }
840
- function createAbortError(reason, config) {
1182
+ function createAbortError2(reason, config) {
841
1183
  if (reason instanceof RequestError) {
842
1184
  return new RequestError(reason.message, {
843
1185
  code: reason.code,
@@ -911,6 +1253,7 @@ var PluginHooks = class {
911
1253
  this.requestHooks = new HookRegistry();
912
1254
  this.responseHooks = new HookRegistry();
913
1255
  this.errorHooks = new HookRegistry();
1256
+ this.settledHooks = new HookRegistry();
914
1257
  this.retryHooks = new HookRegistry();
915
1258
  }
916
1259
  get hasRequestHooks() {
@@ -925,6 +1268,9 @@ var PluginHooks = class {
925
1268
  get hasRetryHooks() {
926
1269
  return this.retryHooks.active;
927
1270
  }
1271
+ get hasSettledHooks() {
1272
+ return this.settledHooks.active;
1273
+ }
928
1274
  onRequest(hook, options = {}) {
929
1275
  return this.requestHooks.register(hook, options);
930
1276
  }
@@ -937,6 +1283,9 @@ var PluginHooks = class {
937
1283
  onRetry(hook, options = {}) {
938
1284
  return this.retryHooks.register(hook, options);
939
1285
  }
1286
+ onSettled(hook, options = {}) {
1287
+ return this.settledHooks.register(hook, options);
1288
+ }
940
1289
  async runRequest(context) {
941
1290
  for (const hook of this.requestHooks.values()) {
942
1291
  await hook(context);
@@ -952,6 +1301,14 @@ var PluginHooks = class {
952
1301
  await hook(context);
953
1302
  }
954
1303
  }
1304
+ async runSettled(context) {
1305
+ for (const hook of this.settledHooks.values()) {
1306
+ try {
1307
+ await hook(context);
1308
+ } catch {
1309
+ }
1310
+ }
1311
+ }
955
1312
  async resolveRetry(context, attempt) {
956
1313
  for (const hook of this.retryHooks.values()) {
957
1314
  const decision = await hook(context, attempt);
@@ -1101,6 +1458,12 @@ function createScopedHooks(hooks, pluginPriority, cleanups) {
1101
1458
  cleanups
1102
1459
  );
1103
1460
  },
1461
+ onSettled(hook, options) {
1462
+ return trackHook(
1463
+ hooks.onSettled(hook, withPriority(options, pluginPriority)),
1464
+ cleanups
1465
+ );
1466
+ },
1104
1467
  onRetry(hook, options) {
1105
1468
  return trackHook(
1106
1469
  hooks.onRetry(hook, withPriority(options, pluginPriority)),
@@ -1504,6 +1867,23 @@ function defaultShouldRefresh(error) {
1504
1867
  }
1505
1868
 
1506
1869
  // src/plugins/cachePlugin.ts
1870
+ var MemoryCacheStore = class {
1871
+ constructor() {
1872
+ this.entries = /* @__PURE__ */ new Map();
1873
+ }
1874
+ get(key) {
1875
+ return this.entries.get(key);
1876
+ }
1877
+ set(key, entry) {
1878
+ this.entries.set(key, entry);
1879
+ }
1880
+ delete(key) {
1881
+ this.entries.delete(key);
1882
+ }
1883
+ clear() {
1884
+ this.entries.clear();
1885
+ }
1886
+ };
1507
1887
  var DEFAULT_CACHE_METHODS = [
1508
1888
  "GET",
1509
1889
  "HEAD"
@@ -1515,8 +1895,11 @@ var DEFAULT_VARY_HEADERS = [
1515
1895
  "accept-language"
1516
1896
  ];
1517
1897
  function cachePlugin(options = {}) {
1518
- const cacheStore = /* @__PURE__ */ new Map();
1898
+ const store = options.store ?? new MemoryCacheStore();
1519
1899
  const cacheHits = /* @__PURE__ */ new WeakSet();
1900
+ const leaders = /* @__PURE__ */ new WeakMap();
1901
+ const completedRecords = /* @__PURE__ */ new WeakMap();
1902
+ const inFlight = /* @__PURE__ */ new Map();
1520
1903
  const methods = new Set(
1521
1904
  options.methods ?? DEFAULT_CACHE_METHODS
1522
1905
  );
@@ -1524,10 +1907,12 @@ function cachePlugin(options = {}) {
1524
1907
  const plugin = {
1525
1908
  name: "cache",
1526
1909
  clear() {
1527
- cacheStore.clear();
1910
+ return store.clear();
1528
1911
  },
1529
1912
  install(context) {
1530
- context.hooks.onRequest((requestContext) => {
1913
+ const ownedLeaders = /* @__PURE__ */ new Set();
1914
+ let active = true;
1915
+ context.hooks.onRequest(async (requestContext) => {
1531
1916
  const cache = resolveExtensionConfig(
1532
1917
  requestContext.config,
1533
1918
  "cache"
@@ -1540,25 +1925,48 @@ function cachePlugin(options = {}) {
1540
1925
  cache,
1541
1926
  varyHeaders
1542
1927
  );
1543
- const record = cacheStore.get(key);
1544
- if (!record) {
1928
+ const record = await readStore(store, key);
1929
+ if (!active) {
1545
1930
  return;
1546
1931
  }
1547
- if (Date.now() > record.expiresAt) {
1548
- cacheStore.delete(key);
1932
+ if (record) {
1933
+ if (!Number.isFinite(record.expiresAt) || Date.now() > record.expiresAt) {
1934
+ await deleteStore(store, key);
1935
+ } else {
1936
+ const cachedResponse = restoreCacheEntry(
1937
+ record,
1938
+ requestContext.config
1939
+ );
1940
+ if (cachedResponse) {
1941
+ requestContext.response = cachedResponse;
1942
+ cacheHits.add(requestContext);
1943
+ return;
1944
+ }
1945
+ await deleteStore(store, key);
1946
+ }
1947
+ }
1948
+ if (!(cache.dedupe ?? options.dedupe ?? true)) {
1549
1949
  return;
1550
1950
  }
1551
- requestContext.response = {
1552
- data: cloneCacheValue(record.data),
1553
- status: record.status,
1554
- statusText: record.statusText,
1555
- headers: new Headers(record.headers),
1556
- config: requestContext.config,
1557
- raw: cloneRawResponse(record)
1558
- };
1559
- cacheHits.add(requestContext);
1951
+ const pending = inFlight.get(key);
1952
+ if (pending) {
1953
+ const sharedRecord = await waitForSharedRecord(
1954
+ pending.promise,
1955
+ requestContext.config
1956
+ );
1957
+ requestContext.response = createCachedResponse(
1958
+ sharedRecord,
1959
+ requestContext.config
1960
+ );
1961
+ cacheHits.add(requestContext);
1962
+ return;
1963
+ }
1964
+ const created = createInFlightRequest(requestContext);
1965
+ inFlight.set(key, created);
1966
+ leaders.set(requestContext, key);
1967
+ ownedLeaders.add(requestContext);
1560
1968
  });
1561
- context.hooks.onResponse((requestContext) => {
1969
+ context.hooks.onResponse(async (requestContext) => {
1562
1970
  if (cacheHits.has(requestContext)) {
1563
1971
  return;
1564
1972
  }
@@ -1575,23 +1983,184 @@ function cachePlugin(options = {}) {
1575
1983
  varyHeaders
1576
1984
  );
1577
1985
  const ttl = cache.ttl ?? 3e4;
1986
+ const record = createCacheEntry(
1987
+ requestContext.response,
1988
+ Date.now() + Math.max(0, ttl)
1989
+ );
1990
+ completedRecords.set(requestContext, record);
1578
1991
  if (ttl <= 0) {
1579
- cacheStore.delete(key);
1992
+ await deleteStore(store, key);
1580
1993
  return;
1581
1994
  }
1582
- cacheStore.set(key, {
1583
- data: cloneCacheValue(requestContext.response.data),
1584
- expiresAt: Date.now() + ttl,
1585
- status: requestContext.response.status,
1586
- statusText: requestContext.response.statusText,
1587
- headers: new Headers(requestContext.response.headers),
1588
- raw: cloneResponse(requestContext.response.raw)
1589
- });
1995
+ await writeStore(store, key, record);
1996
+ });
1997
+ context.hooks.onSettled((requestContext) => {
1998
+ const key = leaders.get(requestContext);
1999
+ if (!key) {
2000
+ return;
2001
+ }
2002
+ leaders.delete(requestContext);
2003
+ ownedLeaders.delete(requestContext);
2004
+ const pending = inFlight.get(key);
2005
+ if (!pending || pending.owner !== requestContext) {
2006
+ return;
2007
+ }
2008
+ inFlight.delete(key);
2009
+ const record = completedRecords.get(requestContext);
2010
+ completedRecords.delete(requestContext);
2011
+ if (record && requestContext.response && !requestContext.error) {
2012
+ pending.resolve(record);
2013
+ return;
2014
+ }
2015
+ pending.reject(
2016
+ requestContext.error ?? new RequestError("Shared request failed", {
2017
+ code: "NETWORK_ERROR",
2018
+ config: requestContext.config
2019
+ })
2020
+ );
1590
2021
  });
2022
+ return () => {
2023
+ active = false;
2024
+ for (const owner of ownedLeaders) {
2025
+ const key = leaders.get(owner);
2026
+ leaders.delete(owner);
2027
+ if (!key) {
2028
+ continue;
2029
+ }
2030
+ const pending = inFlight.get(key);
2031
+ if (!pending || pending.owner !== owner) {
2032
+ continue;
2033
+ }
2034
+ inFlight.delete(key);
2035
+ pending.reject(
2036
+ new RequestError("Cache plugin removed during shared request", {
2037
+ code: "ABORT_ERROR"
2038
+ })
2039
+ );
2040
+ }
2041
+ ownedLeaders.clear();
2042
+ };
1591
2043
  }
1592
2044
  };
1593
2045
  return plugin;
1594
2046
  }
2047
+ function createInFlightRequest(owner) {
2048
+ let resolve;
2049
+ let reject;
2050
+ const promise = new Promise((resolvePromise, rejectPromise) => {
2051
+ resolve = resolvePromise;
2052
+ reject = rejectPromise;
2053
+ });
2054
+ void promise.catch(() => {
2055
+ });
2056
+ return {
2057
+ owner,
2058
+ promise,
2059
+ resolve,
2060
+ reject
2061
+ };
2062
+ }
2063
+ async function readStore(store, key) {
2064
+ try {
2065
+ return await store.get(key);
2066
+ } catch {
2067
+ return void 0;
2068
+ }
2069
+ }
2070
+ async function writeStore(store, key, entry) {
2071
+ try {
2072
+ await store.set(key, entry);
2073
+ } catch {
2074
+ }
2075
+ }
2076
+ async function deleteStore(store, key) {
2077
+ try {
2078
+ await store.delete(key);
2079
+ } catch {
2080
+ }
2081
+ }
2082
+ async function waitForSharedRecord(promise, config) {
2083
+ const signal = config.signal;
2084
+ if (!signal) {
2085
+ try {
2086
+ return await promise;
2087
+ } catch (error) {
2088
+ throw cloneSharedError(error, config);
2089
+ }
2090
+ }
2091
+ if (signal.aborted) {
2092
+ throw createSharedAbortError(signal.reason, config);
2093
+ }
2094
+ return new Promise((resolve, reject) => {
2095
+ const cleanup = () => {
2096
+ signal.removeEventListener("abort", onAbort);
2097
+ };
2098
+ const onAbort = () => {
2099
+ cleanup();
2100
+ reject(createSharedAbortError(signal.reason, config));
2101
+ };
2102
+ signal.addEventListener("abort", onAbort, {
2103
+ once: true
2104
+ });
2105
+ promise.then(
2106
+ (entry) => {
2107
+ cleanup();
2108
+ resolve(entry);
2109
+ },
2110
+ (error) => {
2111
+ cleanup();
2112
+ reject(cloneSharedError(error, config));
2113
+ }
2114
+ );
2115
+ });
2116
+ }
2117
+ function createSharedAbortError(reason, config) {
2118
+ return new RequestError("Request aborted while waiting for shared response", {
2119
+ code: "ABORT_ERROR",
2120
+ config,
2121
+ cause: reason
2122
+ });
2123
+ }
2124
+ function cloneSharedError(error, config) {
2125
+ if (!(error instanceof RequestError)) {
2126
+ return error;
2127
+ }
2128
+ return new RequestError(error.message, {
2129
+ code: error.code,
2130
+ status: error.status,
2131
+ data: error.data,
2132
+ response: error.response,
2133
+ config,
2134
+ cause: error
2135
+ });
2136
+ }
2137
+ function createCacheEntry(response, expiresAt) {
2138
+ return {
2139
+ data: cloneCacheValue(response.data),
2140
+ expiresAt,
2141
+ status: response.status,
2142
+ statusText: response.statusText,
2143
+ headers: [...response.headers.entries()],
2144
+ raw: cloneResponse(response.raw)
2145
+ };
2146
+ }
2147
+ function createCachedResponse(record, config) {
2148
+ return {
2149
+ data: cloneCacheValue(record.data),
2150
+ status: record.status,
2151
+ statusText: record.statusText,
2152
+ headers: new Headers(record.headers),
2153
+ config,
2154
+ raw: cloneRawResponse(record)
2155
+ };
2156
+ }
2157
+ function restoreCacheEntry(record, config) {
2158
+ try {
2159
+ return createCachedResponse(record, config);
2160
+ } catch {
2161
+ return void 0;
2162
+ }
2163
+ }
1595
2164
  function isCacheableRequest(config, methods) {
1596
2165
  return methods.has(config.method ?? "GET") && config.responseType !== "stream";
1597
2166
  }
@@ -1604,7 +2173,7 @@ function createCacheKey(config, cache, varyHeaders) {
1604
2173
  method: config.method ?? "GET",
1605
2174
  baseURL: config.baseURL,
1606
2175
  url: config.url,
1607
- query: normalizeQuery(config.query),
2176
+ query: normalizeQuery2(config.query),
1608
2177
  responseType: config.responseType ?? "auto",
1609
2178
  headers: Object.fromEntries(
1610
2179
  varyHeaders.map((name) => [
@@ -1614,7 +2183,7 @@ function createCacheKey(config, cache, varyHeaders) {
1614
2183
  )
1615
2184
  });
1616
2185
  }
1617
- function normalizeQuery(query) {
2186
+ function normalizeQuery2(query) {
1618
2187
  if (!query) {
1619
2188
  return [];
1620
2189
  }
@@ -1716,7 +2285,7 @@ function xhrRequest(config, options = {}) {
1716
2285
  };
1717
2286
  const onSignalAbort = () => {
1718
2287
  abortWith(
1719
- createAbortError2(signal?.reason, config)
2288
+ createAbortError3(signal?.reason, config)
1720
2289
  );
1721
2290
  };
1722
2291
  xhr.onload = () => {
@@ -1735,7 +2304,7 @@ function xhrRequest(config, options = {}) {
1735
2304
  };
1736
2305
  xhr.onabort = () => {
1737
2306
  rejectOnce(
1738
- createAbortError2(signal?.reason, config)
2307
+ createAbortError3(signal?.reason, config)
1739
2308
  );
1740
2309
  };
1741
2310
  xhr.onprogress = createProgressHandler(
@@ -1825,7 +2394,7 @@ async function processResponse(xhr, config) {
1825
2394
  config,
1826
2395
  raw
1827
2396
  };
1828
- const validateStatus = config.validateStatus ?? defaultValidateStatus2;
2397
+ const validateStatus = config.validateStatus ?? defaultValidateStatus3;
1829
2398
  if (!validateStatus(xhr.status)) {
1830
2399
  throw new RequestError(
1831
2400
  xhr.statusText || "Request failed",
@@ -1893,7 +2462,7 @@ function parseHeaders(value) {
1893
2462
  }
1894
2463
  return headers;
1895
2464
  }
1896
- function createAbortError2(reason, config) {
2465
+ function createAbortError3(reason, config) {
1897
2466
  if (reason instanceof RequestError) {
1898
2467
  return new RequestError(reason.message, {
1899
2468
  code: reason.code,
@@ -1923,7 +2492,7 @@ function createConfigError(config, cause) {
1923
2492
  function isNullBodyStatus(status) {
1924
2493
  return status === 204 || status === 205 || status === 304;
1925
2494
  }
1926
- function defaultValidateStatus2(status) {
2495
+ function defaultValidateStatus3(status) {
1927
2496
  return status >= 200 && status < 300;
1928
2497
  }
1929
2498
 
@@ -2396,7 +2965,7 @@ function normalizeRetryOptions(retry, defaults = {}) {
2396
2965
  return {
2397
2966
  retries: normalizeRetries(retry),
2398
2967
  methods: normalizeMethods(defaults.methods),
2399
- delay: normalizeDelay(defaults.delay),
2968
+ delay: normalizeDelay2(defaults.delay),
2400
2969
  respectRetryAfter: defaults.respectRetryAfter ?? true,
2401
2970
  maxDelay: normalizeMaxDelay(defaults.maxDelay),
2402
2971
  jitter: defaults.jitter ?? false,
@@ -2414,7 +2983,7 @@ function normalizeRetryOptions(retry, defaults = {}) {
2414
2983
  methods: normalizeMethods(
2415
2984
  retry?.methods ?? defaults.methods
2416
2985
  ),
2417
- delay: normalizeDelay(
2986
+ delay: normalizeDelay2(
2418
2987
  retry?.delay ?? defaults.delay
2419
2988
  ),
2420
2989
  respectRetryAfter: retry?.respectRetryAfter ?? defaults.respectRetryAfter ?? true,
@@ -2456,7 +3025,7 @@ function normalizeMaxElapsedTime(maxElapsedTime) {
2456
3025
  }
2457
3026
  return Math.max(0, maxElapsedTime);
2458
3027
  }
2459
- function normalizeDelay(delay) {
3028
+ function normalizeDelay2(delay) {
2460
3029
  if (typeof delay === "number") {
2461
3030
  const normalizedDelay = Math.max(0, delay);
2462
3031
  return () => normalizedDelay;
@@ -2590,6 +3159,7 @@ function uploadPlugin() {
2590
3159
  Client,
2591
3160
  FetchAdapter,
2592
3161
  InterceptorManager,
3162
+ MemoryCacheStore,
2593
3163
  MockAdapter,
2594
3164
  PluginError,
2595
3165
  RequestError,