@hyper-fetch/core 9.1.0 → 9.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/__tests__/features/cache/cache.base.spec.ts +16 -0
- package/__tests__/features/client/client.set-url.spec.ts +149 -0
- package/dist/cache/cache.d.ts +3 -2
- package/dist/cache/cache.d.ts.map +1 -1
- package/dist/client/client.d.ts +13 -1
- package/dist/client/client.d.ts.map +1 -1
- package/dist/index.mjs +23 -2
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/cache/cache.ts +4 -3
- package/src/client/client.ts +36 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hyper-fetch/core",
|
|
3
|
-
"version": "9.
|
|
3
|
+
"version": "9.2.0",
|
|
4
4
|
"description": "Cache, Queue and Persist your requests no matter if you are online or offline!",
|
|
5
5
|
"author": "Maciej Pyrc <maciekpyrc@gmail.com>, Kacper Skawina <kacper.skawina@gmail.com>",
|
|
6
6
|
"homepage": "https://hyperfetch.bettertyped.com/",
|
package/src/cache/cache.ts
CHANGED
|
@@ -195,9 +195,10 @@ export class Cache<Adapter extends AdapterInstance> {
|
|
|
195
195
|
/**
|
|
196
196
|
* Invalidate cache by cacheKey or partial matching with RegExp
|
|
197
197
|
* It emits invalidation event for each matching cacheKey and sets staleTime to 0 to indicate out of time cache
|
|
198
|
-
*
|
|
198
|
+
* Called without arguments it invalidates every cached element.
|
|
199
|
+
* @param cacheKeys - cacheKey or Request instance or RegExp for partial matching (defaults to all elements)
|
|
199
200
|
*/
|
|
200
|
-
invalidate = (cacheKeys: string | RegExp | RequestInstance | (string | RegExp | RequestInstance)[]) => {
|
|
201
|
+
invalidate = (cacheKeys: string | RegExp | RequestInstance | (string | RegExp | RequestInstance)[] = /.*/) => {
|
|
201
202
|
this.logger.debug({ title: "Revalidating cache element", type: "system", extra: { cacheKeys } });
|
|
202
203
|
|
|
203
204
|
const onInvalidate = (key: string | RegExp | RequestInstance) => {
|
|
@@ -285,7 +286,7 @@ export class Cache<Adapter extends AdapterInstance> {
|
|
|
285
286
|
*/
|
|
286
287
|
getAllKeys = async () => {
|
|
287
288
|
const keys = await this.lazyStorage?.keys();
|
|
288
|
-
const asyncKeys = [...keys || []];
|
|
289
|
+
const asyncKeys = [...(keys || [])];
|
|
289
290
|
const syncKeys = [...this.storage.keys()];
|
|
290
291
|
|
|
291
292
|
return [...new Set([...asyncKeys, ...syncKeys])];
|
package/src/client/client.ts
CHANGED
|
@@ -43,7 +43,7 @@ export class Client<
|
|
|
43
43
|
GlobalErrorType extends ClientErrorType = Error,
|
|
44
44
|
Adapter extends AdapterInstance = HttpAdapterType,
|
|
45
45
|
> {
|
|
46
|
-
|
|
46
|
+
url: string;
|
|
47
47
|
readonly mode: ClientMode;
|
|
48
48
|
public debug: boolean;
|
|
49
49
|
|
|
@@ -103,6 +103,38 @@ export class Client<
|
|
|
103
103
|
this.submitDispatcher.initialize(this as unknown as ClientInstance<{ adapter: Adapter }>);
|
|
104
104
|
}
|
|
105
105
|
|
|
106
|
+
/**
|
|
107
|
+
* Set the base url used by the next requests - e.g. when switching tenants, regions or environments at runtime.
|
|
108
|
+
* Cache keys don't include the base url, so data cached for the previous url stays in place unless you pass
|
|
109
|
+
* `invalidate: true`. Pass `abort: true` to cancel the requests that are still in progress for the previous url.
|
|
110
|
+
* @param url new base url
|
|
111
|
+
* @param options.abort abort all ongoing requests
|
|
112
|
+
* @param options.invalidate invalidate the cache (and requests aborted by `abort`), so active consumers refetch
|
|
113
|
+
*/
|
|
114
|
+
setUrl = (url: string, options: { abort?: boolean; invalidate?: boolean } = {}): Client<GlobalErrorType, Adapter> => {
|
|
115
|
+
if (url === this.url) {
|
|
116
|
+
return this;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const { abort = false, invalidate = false } = options;
|
|
120
|
+
const runningRequests = this.fetchDispatcher.getAllRunningRequests().map(({ request }) => request);
|
|
121
|
+
|
|
122
|
+
this.url = url;
|
|
123
|
+
|
|
124
|
+
if (abort) {
|
|
125
|
+
this.requestManager.abortAll();
|
|
126
|
+
}
|
|
127
|
+
if (invalidate) {
|
|
128
|
+
this.cache.invalidate();
|
|
129
|
+
// Aborted requests never land in the cache, so the full invalidation above would not notify their consumers
|
|
130
|
+
if (abort && runningRequests.length > 0) {
|
|
131
|
+
this.cache.invalidate(runningRequests);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return this;
|
|
136
|
+
};
|
|
137
|
+
|
|
106
138
|
/**
|
|
107
139
|
* This method enables the logger usage and display the logs in console
|
|
108
140
|
*/
|
|
@@ -409,7 +441,9 @@ export class Client<
|
|
|
409
441
|
options?: Partial<HydrationOptions> | ((item: HydrateDataType) => Partial<HydrationOptions>),
|
|
410
442
|
) => {
|
|
411
443
|
hydrationData?.forEach((item) => {
|
|
412
|
-
if (!item) {
|
|
444
|
+
if (!item) {
|
|
445
|
+
return;
|
|
446
|
+
}
|
|
413
447
|
|
|
414
448
|
const { cacheKey, scope, response, ...fallbackOptions } = item;
|
|
415
449
|
const defaults = {
|