@hyper-fetch/core 9.1.0 → 9.3.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.
Files changed (41) hide show
  1. package/__tests__/features/cache/cache.base.spec.ts +64 -0
  2. package/__tests__/features/cache/cache.replace-equal-deep.spec.ts +125 -0
  3. package/__tests__/features/client/client.set-url.spec.ts +149 -0
  4. package/__tests__/features/dispatcher/dispatcher.events.spec.ts +52 -0
  5. package/__tests__/features/request/request.setters.spec.ts +7 -0
  6. package/dist/cache/cache.d.ts +3 -2
  7. package/dist/cache/cache.d.ts.map +1 -1
  8. package/dist/cache/cache.types.d.ts +3 -1
  9. package/dist/cache/cache.types.d.ts.map +1 -1
  10. package/dist/cache/cache.utils.d.ts +14 -0
  11. package/dist/cache/cache.utils.d.ts.map +1 -1
  12. package/dist/client/client.d.ts +13 -1
  13. package/dist/client/client.d.ts.map +1 -1
  14. package/dist/dispatcher/dispatcher.d.ts +7 -0
  15. package/dist/dispatcher/dispatcher.d.ts.map +1 -1
  16. package/dist/dispatcher/dispatcher.events.d.ts +15 -1
  17. package/dist/dispatcher/dispatcher.events.d.ts.map +1 -1
  18. package/dist/dispatcher/dispatcher.types.d.ts +4 -0
  19. package/dist/dispatcher/dispatcher.types.d.ts.map +1 -1
  20. package/dist/dispatcher/dispatcher.utils.d.ts +2 -0
  21. package/dist/dispatcher/dispatcher.utils.d.ts.map +1 -1
  22. package/dist/index.mjs +107 -10
  23. package/dist/index.mjs.map +1 -1
  24. package/dist/request/request.d.ts +3 -0
  25. package/dist/request/request.d.ts.map +1 -1
  26. package/dist/request/request.types.d.ts +6 -0
  27. package/dist/request/request.types.d.ts.map +1 -1
  28. package/dist/sdk/sdk.d.ts +1 -0
  29. package/dist/sdk/sdk.d.ts.map +1 -1
  30. package/package.json +1 -1
  31. package/src/cache/cache.ts +10 -4
  32. package/src/cache/cache.types.ts +1 -1
  33. package/src/cache/cache.utils.ts +56 -0
  34. package/src/client/client.ts +36 -2
  35. package/src/dispatcher/dispatcher.events.ts +29 -1
  36. package/src/dispatcher/dispatcher.ts +19 -6
  37. package/src/dispatcher/dispatcher.types.ts +5 -0
  38. package/src/dispatcher/dispatcher.utils.ts +6 -0
  39. package/src/request/request.ts +9 -0
  40. package/src/request/request.types.ts +6 -0
  41. package/src/sdk/sdk.ts +4 -0
@@ -1 +1 @@
1
- {"version":3,"file":"dispatcher.types.d.ts","sourceRoot":"","sources":["../../src/dispatcher/dispatcher.types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAE5D,MAAM,MAAM,qBAAqB,GAAG;IAClC,OAAO,CAAC,EAAE,qBAAqB,CAAC;CACjC,CAAC;AAGF,MAAM,MAAM,aAAa,CAAC,OAAO,SAAS,eAAe,GAAG,eAAe,IAAI;IAC7E,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,OAAO,GAAG,WAAW,CAAC,OAAO,CAAC,CAAC;IACxC,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,OAAO,CAAC;IACjB,wCAAwC;IACxC,QAAQ,EAAE,OAAO,CAAC;CACnB,CAAC;AACF,MAAM,MAAM,aAAa,CAAC,OAAO,SAAS,eAAe,GAAG,eAAe,IAAI;IAC7E,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,aAAa,CAAC,OAAO,CAAC,EAAE,CAAC;IACnC,OAAO,EAAE,OAAO,CAAC;CAClB,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,qBAAqB,CAAC,OAAO,SAAS,eAAe,GAAG,eAAe,IAAI,IAAI,CACzF,aAAa,CAAC,OAAO,CAAC,EACtB,SAAS,CACV,GAAG;IACF,OAAO,EAAE,OAAO,CAAC;CAClB,CAAC;AAEF,MAAM,MAAM,qBAAqB,CAAC,OAAO,SAAS,eAAe,GAAG,eAAe,IAAI;IACrF,Q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AAE,aAAa,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;IACxF,MAAM,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAC;IAC9B,KAAK,EAAE,MAAM,IAAI,CAAC;CACnB,CAAC;AAIF,MAAM,MAAM,uBAAuB,GAAG;IACpC,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,eAAe,CAAC;IACzB,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,yBAAyB,GAAG;IACtC,QAAQ,EAAE,MAAM,CAAC;IACjB,eAAe,EAAE,uBAAuB,EAAE,CAAC;CAC5C,CAAC"}
@@ -6,6 +6,8 @@ export declare const getDispatcherStatusKey: () => string;
6
6
  export declare const getDispatcherStatusByKey: (key: string) => string;
7
7
  export declare const getDispatcherChangeKey: () => string;
8
8
  export declare const getDispatcherChangeByKey: (key: string) => string;
9
+ export declare const getDispatcherRunningKey: () => string;
10
+ export declare const getDispatcherRunningByKey: (key: string) => string;
9
11
  export declare const getIsEqualTimestamp: (currentTimestamp: number, threshold: number, queueTimestamp?: number) => boolean;
10
12
  export declare const canRetryRequest: (currentRetries: number, retry: number | undefined) => boolean;
11
13
  export declare const getRequestType: (request: RequestInstance, latestRequest: ResolvedQueueItemType | undefined) => DispatcherMode;
@@ -1 +1 @@
1
- {"version":3,"file":"dispatcher.utils.d.ts","sourceRoot":"","sources":["../../src/dispatcher/dispatcher.utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AACxD,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAC5C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAI/C,eAAO,MAAM,uBAAuB,QAAO,MAE1C,CAAC;AACF,eAAO,MAAM,yBAAyB,GAAI,KAAK,MAAM,KAAG,MAEvD,CAAC;AACF,eAAO,MAAM,sBAAsB,QAAO,MAEzC,CAAC;AACF,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C;AAEF,eAAO,MAAM,eAAe,GAAI,gBAAgB,MAAM,EAAE,OAAO,MAAM,GAAG,SAAS,YAKhF,CAAC;AASF,eAAO,MAAM,cAAc,GAAI,SAAS,eAAe,EAAE,eAAe,qBAAqB,GAAG,SAAS,mBAoBxG,CAAC"}
package/dist/index.mjs CHANGED
@@ -1988,6 +1988,7 @@ var Request = class Request {
1988
1988
  used;
1989
1989
  deduplicate;
1990
1990
  deduplicateTime;
1991
+ structuralSharing;
1991
1992
  scope;
1992
1993
  /**
1993
1994
  * Instance-level lifecycle hooks. These callbacks fire for every `send()` / `exec()` call
@@ -2029,7 +2030,7 @@ var Request = class Request {
2029
2030
  this.client = client;
2030
2031
  this.requestOptions = requestOptions;
2031
2032
  this.initialRequestConfiguration = initialRequestConfiguration;
2032
- const { endpoint, headers, auth = true, method = client.adapter.defaultMethod, options, cancelable = false, retry = 0, retryTime = 500, cacheTime = Time.MIN * 5, cache = true, staleTime = Time.MIN * 5, queued = false, offline = true, abortKey, cacheKey, queryKey, deduplicate = false, deduplicateTime = null } = {
2033
+ const { endpoint, headers, auth = true, method = client.adapter.defaultMethod, options, cancelable = false, retry = 0, retryTime = 500, cacheTime = Time.MIN * 5, cache = true, staleTime = Time.MIN * 5, queued = false, offline = true, abortKey, cacheKey, queryKey, deduplicate = false, deduplicateTime = null, structuralSharing = true } = {
2033
2034
  ...this.client.adapter.unstable_getRequestDefaults?.(requestOptions),
2034
2035
  ...requestOptions
2035
2036
  };
@@ -2055,6 +2056,7 @@ var Request = class Request {
2055
2056
  this.used = initialRequestConfiguration?.used ?? false;
2056
2057
  this.deduplicate = initialRequestConfiguration?.deduplicate ?? deduplicate;
2057
2058
  this.deduplicateTime = initialRequestConfiguration?.deduplicateTime ?? deduplicateTime;
2059
+ this.structuralSharing = initialRequestConfiguration?.structuralSharing ?? structuralSharing;
2058
2060
  this.scope = initialRequestConfiguration?.scope ?? null;
2059
2061
  this.updatedAbortKey = initialRequestConfiguration?.updatedAbortKey ?? false;
2060
2062
  this.updatedCacheKey = initialRequestConfiguration?.updatedCacheKey ?? false;
@@ -2168,6 +2170,10 @@ var Request = class Request {
2168
2170
  setDeduplicateTime = (deduplicateTime) => {
2169
2171
  return this.clone({ deduplicateTime });
2170
2172
  };
2173
+ /** Enable or disable structural sharing - keeping references of unchanged parts of the response data in cache. */
2174
+ setStructuralSharing = (structuralSharing) => {
2175
+ return this.clone({ structuralSharing });
2176
+ };
2171
2177
  /** Mark the request as used (prevents automatic refetching in hooks). */
2172
2178
  setUsed = (used) => {
2173
2179
  return this.clone({ used });
@@ -2265,6 +2271,7 @@ var Request = class Request {
2265
2271
  updatedQueryKey: this.updatedQueryKey,
2266
2272
  deduplicate: this.deduplicate,
2267
2273
  deduplicateTime: this.deduplicateTime,
2274
+ structuralSharing: this.structuralSharing,
2268
2275
  scope: this.scope,
2269
2276
  isMockerEnabled: this.isMockerEnabled,
2270
2277
  hasMock: !!this.unstable_mock
@@ -2467,6 +2474,7 @@ var Cache = class {
2467
2474
  const previousCacheData = this.storage.get(storageKey);
2468
2475
  const processedResponse = typeof response === "function" ? response(previousCacheData || null) : response;
2469
2476
  const data = getCacheData(previousCacheData, processedResponse);
2477
+ if (request.structuralSharing !== false && previousCacheData) data.data = replaceEqualDeep(previousCacheData.data, data.data);
2470
2478
  const newCacheData = {
2471
2479
  ...data,
2472
2480
  scope,
@@ -2575,9 +2583,10 @@ var Cache = class {
2575
2583
  /**
2576
2584
  * Invalidate cache by cacheKey or partial matching with RegExp
2577
2585
  * It emits invalidation event for each matching cacheKey and sets staleTime to 0 to indicate out of time cache
2578
- * @param key - cacheKey or Request instance or RegExp for partial matching
2586
+ * Called without arguments it invalidates every cached element.
2587
+ * @param cacheKeys - cacheKey or Request instance or RegExp for partial matching (defaults to all elements)
2579
2588
  */
2580
- invalidate = (cacheKeys) => {
2589
+ invalidate = (cacheKeys = /.*/) => {
2581
2590
  this.logger.debug({
2582
2591
  title: "Revalidating cache element",
2583
2592
  type: "system",
@@ -2725,6 +2734,45 @@ var getCacheByKey = (key) => {
2725
2734
  var getCacheKey = () => {
2726
2735
  return `cache`;
2727
2736
  };
2737
+ /**
2738
+ * Check if value is a plain object (object literal or `Object.create(null)`).
2739
+ * Class instances, Dates, Maps, Blobs etc. are not plain objects.
2740
+ */
2741
+ var isPlainObject = (value) => {
2742
+ if (Object.prototype.toString.call(value) !== "[object Object]") return false;
2743
+ const prototype = Object.getPrototypeOf(value);
2744
+ return prototype === null || prototype === Object.prototype;
2745
+ };
2746
+ var isPlainArray = (value) => {
2747
+ return Array.isArray(value) && Object.keys(value).length === value.length;
2748
+ };
2749
+ /**
2750
+ * Structural sharing - returns `next` with every part that is deeply equal to `prev` replaced by the `prev` reference.
2751
+ * When both values are deeply equal the `prev` reference is returned, so consumers can rely on `===` checks.
2752
+ * Only plain objects and arrays are traversed, other values are compared with `Object.is`.
2753
+ * @param prev previous value
2754
+ * @param next incoming value
2755
+ * @returns `next` sharing unchanged references with `prev`
2756
+ */
2757
+ var replaceEqualDeep = (prev, next) => {
2758
+ if (Object.is(prev, next)) return prev;
2759
+ const isArray = isPlainArray(prev) && isPlainArray(next);
2760
+ if (!isArray && !(isPlainObject(prev) && isPlainObject(next))) return next;
2761
+ const prevItems = prev;
2762
+ const nextItems = next;
2763
+ const prevKeys = Object.keys(prevItems);
2764
+ const nextKeys = Object.keys(nextItems);
2765
+ const copy = isArray ? [] : {};
2766
+ let equalItems = 0;
2767
+ nextKeys.forEach((key) => {
2768
+ const prevValue = prevItems[key];
2769
+ const nextValue = nextItems[key];
2770
+ const value = replaceEqualDeep(prevValue, nextValue);
2771
+ copy[key] = value;
2772
+ if (value === prevValue && (value !== void 0 || Object.prototype.hasOwnProperty.call(prevItems, key))) equalItems += 1;
2773
+ });
2774
+ return prevKeys.length === nextKeys.length && equalItems === prevKeys.length ? prev : copy;
2775
+ };
2728
2776
  //#endregion
2729
2777
  //#region src/cache/cache.events.ts
2730
2778
  /** Create the cache event emitters and listeners for data changes, invalidation, and deletion. */
@@ -2790,6 +2838,10 @@ var getDispatcherEvents = (emitter) => ({
2790
2838
  emitter.emit(getDispatcherChangeKey(), values, isTriggeredExternally);
2791
2839
  emitter.emit(getDispatcherChangeByKey(values.queryKey), values, isTriggeredExternally);
2792
2840
  },
2841
+ emitRunningRequestsChanged: (values, isTriggeredExternally) => {
2842
+ emitter.emit(getDispatcherRunningKey(), values, isTriggeredExternally);
2843
+ emitter.emit(getDispatcherRunningByKey(values.queryKey), values, isTriggeredExternally);
2844
+ },
2793
2845
  onDrained: (callback) => {
2794
2846
  emitter.on(getDispatcherDrainedKey(), callback);
2795
2847
  return () => emitter.removeListener(getDispatcherDrainedKey(), callback);
@@ -2813,6 +2865,14 @@ var getDispatcherEvents = (emitter) => ({
2813
2865
  onQueueChangeByKey: (queryKey, callback) => {
2814
2866
  emitter.on(getDispatcherChangeByKey(queryKey), callback);
2815
2867
  return () => emitter.removeListener(getDispatcherChangeByKey(queryKey), callback);
2868
+ },
2869
+ onRunningRequestsChange: (callback) => {
2870
+ emitter.on(getDispatcherRunningKey(), callback);
2871
+ return () => emitter.removeListener(getDispatcherRunningKey(), callback);
2872
+ },
2873
+ onRunningRequestsChangeByKey: (queryKey, callback) => {
2874
+ emitter.on(getDispatcherRunningByKey(queryKey), callback);
2875
+ return () => emitter.removeListener(getDispatcherRunningByKey(queryKey), callback);
2816
2876
  }
2817
2877
  });
2818
2878
  //#endregion
@@ -3026,6 +3086,7 @@ var Dispatcher = class {
3026
3086
  */
3027
3087
  clear = () => {
3028
3088
  this.getQueuesKeys().forEach((queryKey) => this.cancelRunningRequests(queryKey));
3089
+ [...this.runningRequests.keys()].forEach((queryKey) => this.setRunningRequests(queryKey, []));
3029
3090
  this.runningRequests.clear();
3030
3091
  this.storage.clear();
3031
3092
  this.client.triggerPlugins("onDispatcherCleared", { dispatcher: this });
@@ -3075,6 +3136,17 @@ var Dispatcher = class {
3075
3136
  return this.getRunningRequests(queryKey).find((req) => req.requestId === requestId);
3076
3137
  };
3077
3138
  /**
3139
+ * Single place for changing the running requests - notifies about the change
3140
+ */
3141
+ setRunningRequests = (queryKey, runningRequests) => {
3142
+ const previousRequests = this.getRunningRequests(queryKey);
3143
+ this.runningRequests.set(queryKey, runningRequests);
3144
+ if (previousRequests.length !== runningRequests.length || previousRequests.some((req, i) => req !== runningRequests[i])) this.events.emitRunningRequestsChanged({
3145
+ queryKey,
3146
+ runningRequests
3147
+ });
3148
+ };
3149
+ /**
3078
3150
  * Add request to the running requests list
3079
3151
  */
3080
3152
  addRunningRequest = (queryKey, requestId, request) => {
@@ -3083,9 +3155,7 @@ var Dispatcher = class {
3083
3155
  request,
3084
3156
  timestamp: Date.now()
3085
3157
  };
3086
- const runningRequests = this.getRunningRequests(queryKey);
3087
- runningRequests.push(newRunningRequest);
3088
- this.runningRequests.set(queryKey, runningRequests);
3158
+ this.setRunningRequests(queryKey, [...this.getRunningRequests(queryKey), newRunningRequest]);
3089
3159
  return newRunningRequest;
3090
3160
  };
3091
3161
  /**
@@ -3122,20 +3192,20 @@ var Dispatcher = class {
3122
3192
  }
3123
3193
  return true;
3124
3194
  });
3125
- this.runningRequests.set(queryKey, requests);
3195
+ this.setRunningRequests(queryKey, requests);
3126
3196
  };
3127
3197
  /**
3128
3198
  * Delete all started requests, but do NOT clear it from queue and do NOT cancel them
3129
3199
  */
3130
3200
  deleteRunningRequests = (queryKey) => {
3131
- this.runningRequests.set(queryKey, []);
3201
+ this.setRunningRequests(queryKey, []);
3132
3202
  };
3133
3203
  /**
3134
3204
  * Delete request by id, but do NOT clear it from queue and do NOT cancel them
3135
3205
  */
3136
3206
  deleteRunningRequest = (queryKey, requestId) => {
3137
3207
  const runningRequests = this.getRunningRequests(queryKey);
3138
- this.runningRequests.set(queryKey, runningRequests.filter((req) => req.requestId !== requestId));
3208
+ this.setRunningRequests(queryKey, runningRequests.filter((req) => req.requestId !== requestId));
3139
3209
  };
3140
3210
  /**
3141
3211
  * Get count of requests from the same queryKey
@@ -3455,6 +3525,12 @@ var getDispatcherChangeKey = () => {
3455
3525
  var getDispatcherChangeByKey = (key) => {
3456
3526
  return `${key}-change-event`;
3457
3527
  };
3528
+ var getDispatcherRunningKey = () => {
3529
+ return `running-event`;
3530
+ };
3531
+ var getDispatcherRunningByKey = (key) => {
3532
+ return `${key}-running-event`;
3533
+ };
3458
3534
  var getIsEqualTimestamp = (currentTimestamp, threshold, queueTimestamp) => {
3459
3535
  if (!queueTimestamp) return false;
3460
3536
  return queueTimestamp - currentTimestamp <= threshold;
@@ -3787,6 +3863,26 @@ var Client = class {
3787
3863
  this.submitDispatcher.initialize(this);
3788
3864
  }
3789
3865
  /**
3866
+ * Set the base url used by the next requests - e.g. when switching tenants, regions or environments at runtime.
3867
+ * Cache keys don't include the base url, so data cached for the previous url stays in place unless you pass
3868
+ * `invalidate: true`. Pass `abort: true` to cancel the requests that are still in progress for the previous url.
3869
+ * @param url new base url
3870
+ * @param options.abort abort all ongoing requests
3871
+ * @param options.invalidate invalidate the cache (and requests aborted by `abort`), so active consumers refetch
3872
+ */
3873
+ setUrl = (url, options = {}) => {
3874
+ if (url === this.url) return this;
3875
+ const { abort = false, invalidate = false } = options;
3876
+ const runningRequests = this.fetchDispatcher.getAllRunningRequests().map(({ request }) => request);
3877
+ this.url = url;
3878
+ if (abort) this.requestManager.abortAll();
3879
+ if (invalidate) {
3880
+ this.cache.invalidate();
3881
+ if (abort && runningRequests.length > 0) this.cache.invalidate(runningRequests);
3882
+ }
3883
+ return this;
3884
+ };
3885
+ /**
3790
3886
  * This method enables the logger usage and display the logs in console
3791
3887
  */
3792
3888
  setDebug = (enabled) => {
@@ -4097,6 +4193,7 @@ var applyObjectDefaults = (request, config) => {
4097
4193
  if (config.offline !== void 0) result = result.setOffline(config.offline);
4098
4194
  if (config.deduplicate !== void 0) result = result.setDeduplicate(config.deduplicate);
4099
4195
  if (config.deduplicateTime !== void 0 && config.deduplicateTime !== null) result = result.setDeduplicateTime(config.deduplicateTime);
4196
+ if (config.structuralSharing !== void 0) result = result.setStructuralSharing(config.structuralSharing);
4100
4197
  return result;
4101
4198
  };
4102
4199
  var applyDefaults = (request, context) => {
@@ -4202,6 +4299,6 @@ var createConfiguration = () => {
4202
4299
  return (defaults) => defaults;
4203
4300
  };
4204
4301
  //#endregion
4205
- export { AbortError, Adapter, AppEvents, AppManager, Cache, Client, DeletedError, Dispatcher, DispatcherMode, EventEmitter, HttpAdapter, HttpMethods, LoggerManager, Plugin, Request, RequestManager, RequestProcessingError, SAFE_HTTP_METHODS, Time, TimeoutError, UnexpectedError, appManagerInitialOptions, canRetryRequest, createClient, createConfiguration, createRequestHooks, createSdk, defaultMapper, defaultTimeout, getAbortByAbortKey, getAbortByIdKey, getAbortKey, getAdapter, getAdapterBindings, getAdapterHeaders, getAdapterOnError, getAdapterPayload, getAppManagerEvents, getCacheByKey, getCacheData, getCacheEvents, getCacheKey, getDeleteByKey, getDeleteKey, getDispatcherChangeByKey, getDispatcherChangeKey, getDispatcherDrainedByKey, getDispatcherDrainedKey, getDispatcherEvents, getDispatcherStatusByKey, getDispatcherStatusKey, getDownloadProgressByIdKey, getDownloadProgressByQueryKey, getDownloadProgressKey, getErrorMessage, getInvalidateByKey, getInvalidateKey, getIsEqualTimestamp, getLoadingByCacheKey, getLoadingByIdKey, getLoadingByQueryKey, getLoadingKey, getProgressData, getProgressValue, getRemoveByIdKey, getRemoveByQueryKey, getRemoveKey, getRequestDeduplicatedByCacheKey, getRequestDeduplicatedByIdKey, getRequestDeduplicatedByQueryKey, getRequestDeduplicatedKey, getRequestDispatcher, getRequestEta, getRequestKey, getRequestManagerEvents, getRequestStarByQueryKey, getRequestStartByIdKey, getRequestStartKey, getRequestType, getResponseByCacheKey, getResponseByIdKey, getResponseHeaders, getResponseKey, getResponseStartByIdKey, getResponseStartByQueryKey, getResponseStartKey, getSimpleKey, getTime, getUniqueRequestId, getUploadProgressByIdKey, getUploadProgressByQueryKey, getUploadProgressKey, handleResponse, hasDocument, hasWindow, interceptRequest, interceptResponse, logLevelOrder, logger, loggerColors, loggerStyles, mapResponseForSend, mocker, onDocumentEvent, onWindowEvent, parseErrorResponse, parseResponse, resolveClientMode, scopeKey, sendRequest, stringifyDefaultOptions, stringifyKey, stringifyQueryParams, stringifyValue, xhrExtra };
4302
+ export { AbortError, Adapter, AppEvents, AppManager, Cache, Client, DeletedError, Dispatcher, DispatcherMode, EventEmitter, HttpAdapter, HttpMethods, LoggerManager, Plugin, Request, RequestManager, RequestProcessingError, SAFE_HTTP_METHODS, Time, TimeoutError, UnexpectedError, appManagerInitialOptions, canRetryRequest, createClient, createConfiguration, createRequestHooks, createSdk, defaultMapper, defaultTimeout, getAbortByAbortKey, getAbortByIdKey, getAbortKey, getAdapter, getAdapterBindings, getAdapterHeaders, getAdapterOnError, getAdapterPayload, getAppManagerEvents, getCacheByKey, getCacheData, getCacheEvents, getCacheKey, getDeleteByKey, getDeleteKey, getDispatcherChangeByKey, getDispatcherChangeKey, getDispatcherDrainedByKey, getDispatcherDrainedKey, getDispatcherEvents, getDispatcherRunningByKey, getDispatcherRunningKey, getDispatcherStatusByKey, getDispatcherStatusKey, getDownloadProgressByIdKey, getDownloadProgressByQueryKey, getDownloadProgressKey, getErrorMessage, getInvalidateByKey, getInvalidateKey, getIsEqualTimestamp, getLoadingByCacheKey, getLoadingByIdKey, getLoadingByQueryKey, getLoadingKey, getProgressData, getProgressValue, getRemoveByIdKey, getRemoveByQueryKey, getRemoveKey, getRequestDeduplicatedByCacheKey, getRequestDeduplicatedByIdKey, getRequestDeduplicatedByQueryKey, getRequestDeduplicatedKey, getRequestDispatcher, getRequestEta, getRequestKey, getRequestManagerEvents, getRequestStarByQueryKey, getRequestStartByIdKey, getRequestStartKey, getRequestType, getResponseByCacheKey, getResponseByIdKey, getResponseHeaders, getResponseKey, getResponseStartByIdKey, getResponseStartByQueryKey, getResponseStartKey, getSimpleKey, getTime, getUniqueRequestId, getUploadProgressByIdKey, getUploadProgressByQueryKey, getUploadProgressKey, handleResponse, hasDocument, hasWindow, interceptRequest, interceptResponse, isPlainObject, logLevelOrder, logger, loggerColors, loggerStyles, mapResponseForSend, mocker, onDocumentEvent, onWindowEvent, parseErrorResponse, parseResponse, replaceEqualDeep, resolveClientMode, scopeKey, sendRequest, stringifyDefaultOptions, stringifyKey, stringifyQueryParams, stringifyValue, xhrExtra };
4206
4303
 
4207
4304
  //# sourceMappingURL=index.mjs.map