@hyper-fetch/core 9.2.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.
- package/__tests__/features/cache/cache.base.spec.ts +48 -0
- package/__tests__/features/cache/cache.replace-equal-deep.spec.ts +125 -0
- package/__tests__/features/dispatcher/dispatcher.events.spec.ts +52 -0
- package/__tests__/features/request/request.setters.spec.ts +7 -0
- package/dist/cache/cache.d.ts.map +1 -1
- package/dist/cache/cache.types.d.ts +3 -1
- package/dist/cache/cache.types.d.ts.map +1 -1
- package/dist/cache/cache.utils.d.ts +14 -0
- package/dist/cache/cache.utils.d.ts.map +1 -1
- package/dist/dispatcher/dispatcher.d.ts +7 -0
- package/dist/dispatcher/dispatcher.d.ts.map +1 -1
- package/dist/dispatcher/dispatcher.events.d.ts +15 -1
- package/dist/dispatcher/dispatcher.events.d.ts.map +1 -1
- package/dist/dispatcher/dispatcher.types.d.ts +4 -0
- package/dist/dispatcher/dispatcher.types.d.ts.map +1 -1
- package/dist/dispatcher/dispatcher.utils.d.ts +2 -0
- package/dist/dispatcher/dispatcher.utils.d.ts.map +1 -1
- package/dist/index.mjs +84 -8
- package/dist/index.mjs.map +1 -1
- package/dist/request/request.d.ts +3 -0
- package/dist/request/request.d.ts.map +1 -1
- package/dist/request/request.types.d.ts +6 -0
- package/dist/request/request.types.d.ts.map +1 -1
- package/dist/sdk/sdk.d.ts +1 -0
- package/dist/sdk/sdk.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/cache/cache.ts +6 -1
- package/src/cache/cache.types.ts +1 -1
- package/src/cache/cache.utils.ts +56 -0
- package/src/dispatcher/dispatcher.events.ts +29 -1
- package/src/dispatcher/dispatcher.ts +19 -6
- package/src/dispatcher/dispatcher.types.ts +5 -0
- package/src/dispatcher/dispatcher.utils.ts +6 -0
- package/src/request/request.ts +9 -0
- package/src/request/request.types.ts +6 -0
- package/src/sdk/sdk.ts +4 -0
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,
|
|
@@ -2726,6 +2734,45 @@ var getCacheByKey = (key) => {
|
|
|
2726
2734
|
var getCacheKey = () => {
|
|
2727
2735
|
return `cache`;
|
|
2728
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
|
+
};
|
|
2729
2776
|
//#endregion
|
|
2730
2777
|
//#region src/cache/cache.events.ts
|
|
2731
2778
|
/** Create the cache event emitters and listeners for data changes, invalidation, and deletion. */
|
|
@@ -2791,6 +2838,10 @@ var getDispatcherEvents = (emitter) => ({
|
|
|
2791
2838
|
emitter.emit(getDispatcherChangeKey(), values, isTriggeredExternally);
|
|
2792
2839
|
emitter.emit(getDispatcherChangeByKey(values.queryKey), values, isTriggeredExternally);
|
|
2793
2840
|
},
|
|
2841
|
+
emitRunningRequestsChanged: (values, isTriggeredExternally) => {
|
|
2842
|
+
emitter.emit(getDispatcherRunningKey(), values, isTriggeredExternally);
|
|
2843
|
+
emitter.emit(getDispatcherRunningByKey(values.queryKey), values, isTriggeredExternally);
|
|
2844
|
+
},
|
|
2794
2845
|
onDrained: (callback) => {
|
|
2795
2846
|
emitter.on(getDispatcherDrainedKey(), callback);
|
|
2796
2847
|
return () => emitter.removeListener(getDispatcherDrainedKey(), callback);
|
|
@@ -2814,6 +2865,14 @@ var getDispatcherEvents = (emitter) => ({
|
|
|
2814
2865
|
onQueueChangeByKey: (queryKey, callback) => {
|
|
2815
2866
|
emitter.on(getDispatcherChangeByKey(queryKey), callback);
|
|
2816
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);
|
|
2817
2876
|
}
|
|
2818
2877
|
});
|
|
2819
2878
|
//#endregion
|
|
@@ -3027,6 +3086,7 @@ var Dispatcher = class {
|
|
|
3027
3086
|
*/
|
|
3028
3087
|
clear = () => {
|
|
3029
3088
|
this.getQueuesKeys().forEach((queryKey) => this.cancelRunningRequests(queryKey));
|
|
3089
|
+
[...this.runningRequests.keys()].forEach((queryKey) => this.setRunningRequests(queryKey, []));
|
|
3030
3090
|
this.runningRequests.clear();
|
|
3031
3091
|
this.storage.clear();
|
|
3032
3092
|
this.client.triggerPlugins("onDispatcherCleared", { dispatcher: this });
|
|
@@ -3076,6 +3136,17 @@ var Dispatcher = class {
|
|
|
3076
3136
|
return this.getRunningRequests(queryKey).find((req) => req.requestId === requestId);
|
|
3077
3137
|
};
|
|
3078
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
|
+
/**
|
|
3079
3150
|
* Add request to the running requests list
|
|
3080
3151
|
*/
|
|
3081
3152
|
addRunningRequest = (queryKey, requestId, request) => {
|
|
@@ -3084,9 +3155,7 @@ var Dispatcher = class {
|
|
|
3084
3155
|
request,
|
|
3085
3156
|
timestamp: Date.now()
|
|
3086
3157
|
};
|
|
3087
|
-
|
|
3088
|
-
runningRequests.push(newRunningRequest);
|
|
3089
|
-
this.runningRequests.set(queryKey, runningRequests);
|
|
3158
|
+
this.setRunningRequests(queryKey, [...this.getRunningRequests(queryKey), newRunningRequest]);
|
|
3090
3159
|
return newRunningRequest;
|
|
3091
3160
|
};
|
|
3092
3161
|
/**
|
|
@@ -3123,20 +3192,20 @@ var Dispatcher = class {
|
|
|
3123
3192
|
}
|
|
3124
3193
|
return true;
|
|
3125
3194
|
});
|
|
3126
|
-
this.
|
|
3195
|
+
this.setRunningRequests(queryKey, requests);
|
|
3127
3196
|
};
|
|
3128
3197
|
/**
|
|
3129
3198
|
* Delete all started requests, but do NOT clear it from queue and do NOT cancel them
|
|
3130
3199
|
*/
|
|
3131
3200
|
deleteRunningRequests = (queryKey) => {
|
|
3132
|
-
this.
|
|
3201
|
+
this.setRunningRequests(queryKey, []);
|
|
3133
3202
|
};
|
|
3134
3203
|
/**
|
|
3135
3204
|
* Delete request by id, but do NOT clear it from queue and do NOT cancel them
|
|
3136
3205
|
*/
|
|
3137
3206
|
deleteRunningRequest = (queryKey, requestId) => {
|
|
3138
3207
|
const runningRequests = this.getRunningRequests(queryKey);
|
|
3139
|
-
this.
|
|
3208
|
+
this.setRunningRequests(queryKey, runningRequests.filter((req) => req.requestId !== requestId));
|
|
3140
3209
|
};
|
|
3141
3210
|
/**
|
|
3142
3211
|
* Get count of requests from the same queryKey
|
|
@@ -3456,6 +3525,12 @@ var getDispatcherChangeKey = () => {
|
|
|
3456
3525
|
var getDispatcherChangeByKey = (key) => {
|
|
3457
3526
|
return `${key}-change-event`;
|
|
3458
3527
|
};
|
|
3528
|
+
var getDispatcherRunningKey = () => {
|
|
3529
|
+
return `running-event`;
|
|
3530
|
+
};
|
|
3531
|
+
var getDispatcherRunningByKey = (key) => {
|
|
3532
|
+
return `${key}-running-event`;
|
|
3533
|
+
};
|
|
3459
3534
|
var getIsEqualTimestamp = (currentTimestamp, threshold, queueTimestamp) => {
|
|
3460
3535
|
if (!queueTimestamp) return false;
|
|
3461
3536
|
return queueTimestamp - currentTimestamp <= threshold;
|
|
@@ -4118,6 +4193,7 @@ var applyObjectDefaults = (request, config) => {
|
|
|
4118
4193
|
if (config.offline !== void 0) result = result.setOffline(config.offline);
|
|
4119
4194
|
if (config.deduplicate !== void 0) result = result.setDeduplicate(config.deduplicate);
|
|
4120
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);
|
|
4121
4197
|
return result;
|
|
4122
4198
|
};
|
|
4123
4199
|
var applyDefaults = (request, context) => {
|
|
@@ -4223,6 +4299,6 @@ var createConfiguration = () => {
|
|
|
4223
4299
|
return (defaults) => defaults;
|
|
4224
4300
|
};
|
|
4225
4301
|
//#endregion
|
|
4226
|
-
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 };
|
|
4227
4303
|
|
|
4228
4304
|
//# sourceMappingURL=index.mjs.map
|