@hyper-fetch/core 6.0.1 → 7.0.1
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/.eslintrc.json +8 -3
- package/README.md +5 -5
- package/dist/browser/index.js +3714 -0
- package/dist/browser/index.js.map +7 -0
- package/dist/index.d.ts +1669 -1091
- package/dist/index.js +3952 -0
- package/dist/index.js.map +7 -0
- package/package.json +16 -18
- package/src/adapter/adapter.bindings.ts +261 -165
- package/src/adapter/adapter.ts +480 -8
- package/src/adapter/adapter.types.ts +111 -139
- package/src/adapter/adapter.utils.ts +71 -25
- package/src/adapter/index.ts +1 -2
- package/src/cache/cache.events.ts +58 -14
- package/src/cache/cache.ts +158 -91
- package/src/cache/cache.types.ts +15 -25
- package/src/cache/cache.utils.ts +17 -10
- package/src/client/client.create.ts +17 -0
- package/src/client/client.ts +247 -266
- package/src/client/client.types.ts +43 -21
- package/src/client/client.utils.ts +7 -168
- package/src/client/index.ts +1 -0
- package/src/constants/http.constants.ts +6 -6
- package/src/constants/time.constants.ts +10 -10
- package/src/dispatcher/dispatcher.constants.ts +5 -5
- package/src/dispatcher/dispatcher.events.ts +85 -20
- package/src/dispatcher/dispatcher.ts +265 -199
- package/src/dispatcher/dispatcher.types.ts +6 -7
- package/src/dispatcher/dispatcher.utils.ts +26 -10
- package/src/http-adapter/http-adapter.browser.ts +101 -0
- package/src/http-adapter/http-adapter.constants.ts +8 -0
- package/src/http-adapter/http-adapter.server.ts +134 -0
- package/src/http-adapter/http-adapter.ts +11 -0
- package/src/http-adapter/http-adapter.types.ts +86 -0
- package/src/http-adapter/http-adapter.utils.ts +177 -0
- package/src/http-adapter/index.ts +4 -0
- package/src/index.ts +2 -1
- package/src/managers/app/app.manager.constants.ts +4 -4
- package/src/managers/app/app.manager.events.ts +12 -12
- package/src/managers/app/app.manager.ts +10 -7
- package/src/managers/app/app.manager.utils.ts +12 -2
- package/src/managers/logger/logger.manager.constants.ts +13 -18
- package/src/managers/logger/logger.manager.ts +37 -49
- package/src/managers/logger/logger.manager.types.ts +32 -21
- package/src/managers/logger/logger.manager.utils.ts +23 -17
- package/src/managers/request/request.manager.events.ts +222 -109
- package/src/managers/request/request.manager.ts +5 -2
- package/src/managers/request/request.manager.types.ts +35 -7
- package/src/managers/request/request.manager.utils.ts +29 -17
- package/src/mocker/mocker.ts +115 -81
- package/src/mocker/mocker.types.ts +29 -25
- package/src/plugin/index.ts +2 -0
- package/src/plugin/plugin.ts +159 -0
- package/src/plugin/plugin.types.ts +181 -0
- package/src/request/request.ts +366 -323
- package/src/request/request.types.ts +144 -148
- package/src/request/request.utils.ts +35 -32
- package/src/types/adapter.types.ts +63 -0
- package/src/types/client.types.ts +3 -3
- package/src/types/helpers.types.ts +16 -3
- package/src/types/hydration.types.ts +16 -0
- package/src/types/index.ts +3 -1
- package/src/types/request.types.ts +73 -0
- package/src/utils/emitter.utils.ts +52 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/uuid.utils.ts +4 -2
- package/tsconfig.base.json +4 -5
- package/tsconfig.json +5 -4
- package/dist/browser/index.cjs.js +0 -2420
- package/dist/browser/index.cjs.js.map +0 -7
- package/dist/browser/index.esm.js +0 -2395
- package/dist/browser/index.esm.js.map +0 -7
- package/dist/index.cjs.js +0 -2579
- package/dist/index.cjs.js.map +0 -7
- package/dist/index.esm.js +0 -2474
- package/dist/index.esm.js.map +0 -7
- package/src/adapter/adapter.browser.ts +0 -100
- package/src/adapter/adapter.constants.ts +0 -8
- package/src/adapter/adapter.server.ts +0 -93
- package/src/effect/index.ts +0 -2
- package/src/effect/request.effect.ts +0 -31
- package/src/effect/request.effect.types.ts +0 -44
- package/src/types/fetch.types.ts +0 -50
package/src/cache/cache.ts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
import { ClientInstance, ExtractAdapterTypeFromClient } from "client";
|
|
5
|
-
import { ResponseDetailsType, LoggerType } from "managers";
|
|
1
|
+
import { AdapterInstance, ResponseType } from "adapter";
|
|
2
|
+
import { ResponseDetailsType, LoggerMethods } from "managers";
|
|
3
|
+
import { ClientInstance } from "client";
|
|
6
4
|
import {
|
|
7
5
|
CacheOptionsType,
|
|
8
6
|
CacheAsyncStorageType,
|
|
@@ -10,10 +8,12 @@ import {
|
|
|
10
8
|
getCacheData,
|
|
11
9
|
getCacheEvents,
|
|
12
10
|
CacheValueType,
|
|
13
|
-
|
|
11
|
+
CacheSetState,
|
|
12
|
+
RequestCacheType,
|
|
14
13
|
} from "cache";
|
|
15
|
-
import {
|
|
14
|
+
import { Request, RequestInstance } from "request";
|
|
16
15
|
import { ExtractAdapterType, ExtractErrorType, ExtractResponseType } from "types";
|
|
16
|
+
import { EventEmitter } from "utils";
|
|
17
17
|
|
|
18
18
|
/**
|
|
19
19
|
* Cache class handles the data exchange with the dispatchers.
|
|
@@ -22,36 +22,41 @@ import { ExtractAdapterType, ExtractErrorType, ExtractResponseType } from "types
|
|
|
22
22
|
* Keys used to save the values are created dynamically on the Request class
|
|
23
23
|
*
|
|
24
24
|
*/
|
|
25
|
-
export class Cache<
|
|
25
|
+
export class Cache<Adapter extends AdapterInstance> {
|
|
26
26
|
public emitter = new EventEmitter();
|
|
27
27
|
public events: ReturnType<typeof getCacheEvents>;
|
|
28
28
|
|
|
29
29
|
public storage: CacheStorageType;
|
|
30
30
|
public lazyStorage?: CacheAsyncStorageType;
|
|
31
|
-
public
|
|
31
|
+
public version: string;
|
|
32
32
|
public garbageCollectors = new Map<string, ReturnType<typeof setTimeout>>();
|
|
33
|
-
private logger:
|
|
33
|
+
private logger: LoggerMethods;
|
|
34
|
+
private client: ClientInstance<{ adapter: Adapter }>;
|
|
35
|
+
|
|
36
|
+
constructor(public options?: CacheOptionsType) {
|
|
37
|
+
const { storage = new Map<string, CacheValueType>(), lazyStorage, version = "0.0.1" } = options ?? {};
|
|
34
38
|
|
|
35
|
-
|
|
36
|
-
this.storage = this.options?.storage || new Map<string, CacheValueType>();
|
|
39
|
+
this.emitter?.setMaxListeners(1000);
|
|
37
40
|
this.events = getCacheEvents(this.emitter);
|
|
38
|
-
this.options?.onInitialization?.(this);
|
|
39
41
|
|
|
40
|
-
this.
|
|
41
|
-
this.
|
|
42
|
-
this.
|
|
42
|
+
this.storage = storage;
|
|
43
|
+
this.version = version;
|
|
44
|
+
this.lazyStorage = lazyStorage;
|
|
45
|
+
}
|
|
43
46
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
+
initialize = (client: ClientInstance<{ adapter: Adapter }>) => {
|
|
48
|
+
this.client = client;
|
|
49
|
+
this.logger = client.loggerManager.initialize(client, "Cache");
|
|
47
50
|
|
|
48
51
|
// Going back from offline should re-trigger garbage collection
|
|
49
|
-
|
|
50
|
-
this.
|
|
51
|
-
keys.forEach(this.scheduleGarbageCollector);
|
|
52
|
-
});
|
|
52
|
+
client.appManager.events.onOnline(() => {
|
|
53
|
+
[...this.storage.keys()].forEach(this.scheduleGarbageCollector);
|
|
53
54
|
});
|
|
54
|
-
|
|
55
|
+
|
|
56
|
+
[...this.storage.keys()].forEach(this.scheduleGarbageCollector);
|
|
57
|
+
|
|
58
|
+
return this;
|
|
59
|
+
};
|
|
55
60
|
|
|
56
61
|
/**
|
|
57
62
|
* Set the cache data to the storage
|
|
@@ -59,16 +64,16 @@ export class Cache<C extends ClientInstance> {
|
|
|
59
64
|
* @param response
|
|
60
65
|
* @returns
|
|
61
66
|
*/
|
|
62
|
-
set = <Request extends RequestInstance
|
|
63
|
-
request:
|
|
64
|
-
response:
|
|
65
|
-
|
|
67
|
+
set = <Request extends RequestCacheType<RequestInstance>>(
|
|
68
|
+
request: Request,
|
|
69
|
+
response: CacheSetState<
|
|
70
|
+
ResponseType<ExtractResponseType<Request>, ExtractErrorType<Request>, ExtractAdapterType<Request>> &
|
|
66
71
|
ResponseDetailsType
|
|
67
|
-
|
|
72
|
+
> & { hydrated?: boolean },
|
|
68
73
|
): void => {
|
|
69
|
-
this.logger.debug("Processing cache response", { request, response });
|
|
70
|
-
const { cacheKey, cache,
|
|
71
|
-
const
|
|
74
|
+
this.logger.debug({ title: "Processing cache response", type: "system", extra: { request, response } });
|
|
75
|
+
const { cacheKey, cache, staleTime, cacheTime } = request;
|
|
76
|
+
const previousCacheData = this.storage.get<
|
|
72
77
|
ExtractResponseType<Request>,
|
|
73
78
|
ExtractErrorType<Request>,
|
|
74
79
|
ExtractAdapterType<Request>
|
|
@@ -76,48 +81,60 @@ export class Cache<C extends ClientInstance> {
|
|
|
76
81
|
|
|
77
82
|
// Once refresh error occurs we don't want to override already valid data in our cache with the thrown error
|
|
78
83
|
// We need to check it against cache and return last valid data we have
|
|
79
|
-
const processedResponse = typeof response === "function" ? response(
|
|
80
|
-
const data = getCacheData(
|
|
81
|
-
|
|
82
|
-
const newCacheData: CacheValueType = { ...data, cacheTime, clearKey: this.clearKey, garbageCollection };
|
|
84
|
+
const processedResponse = typeof response === "function" ? response(previousCacheData || null) : response;
|
|
85
|
+
const data = getCacheData(previousCacheData, processedResponse);
|
|
83
86
|
|
|
84
|
-
|
|
87
|
+
const newCacheData: CacheValueType<any, any, ExtractAdapterType<Request>> = {
|
|
88
|
+
...data,
|
|
89
|
+
staleTime,
|
|
90
|
+
version: this.version,
|
|
85
91
|
cacheKey,
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
// If request should not use cache - just emit response data
|
|
91
|
-
if (!cache) {
|
|
92
|
-
return this.logger.debug("Prevented saving response to cache", { request, data });
|
|
93
|
-
}
|
|
92
|
+
cacheTime,
|
|
93
|
+
cached: !!request.cache,
|
|
94
|
+
};
|
|
94
95
|
|
|
95
96
|
// Only success data is valid for the cache store
|
|
96
|
-
if (processedResponse.success) {
|
|
97
|
-
this.logger.debug("Saving response to cache storage", { request, data });
|
|
98
|
-
this.storage.set<Response, Error,
|
|
99
|
-
this.lazyStorage?.set<Response, Error,
|
|
100
|
-
this.
|
|
97
|
+
if (processedResponse.success && cache) {
|
|
98
|
+
this.logger.debug({ title: "Saving response to cache storage", type: "system", extra: { request, data } });
|
|
99
|
+
this.storage.set<Response, Error, ExtractAdapterType<Request>>(cacheKey, newCacheData);
|
|
100
|
+
this.lazyStorage?.set<Response, Error, ExtractAdapterType<Request>>(cacheKey, newCacheData);
|
|
101
|
+
this.client.triggerPlugins("onCacheItemChange", {
|
|
102
|
+
cache: this,
|
|
103
|
+
cacheKey,
|
|
104
|
+
prevData: (previousCacheData || null) as CacheValueType<any, any, AdapterInstance>,
|
|
105
|
+
newData: newCacheData as CacheValueType<any, any, AdapterInstance>,
|
|
106
|
+
});
|
|
107
|
+
|
|
101
108
|
this.scheduleGarbageCollector(cacheKey);
|
|
109
|
+
} else {
|
|
110
|
+
// If request should not use cache - just emit response data
|
|
111
|
+
this.logger.debug({ title: "Prevented saving response to cache", type: "system", extra: { request, data } });
|
|
102
112
|
}
|
|
113
|
+
|
|
114
|
+
this.logger.debug({ title: "Emitting cache response", type: "system", extra: { request, data } });
|
|
115
|
+
this.events.emitCacheData<ExtractResponseType<Request>, ExtractErrorType<Request>, ExtractAdapterType<Request>>(
|
|
116
|
+
newCacheData,
|
|
117
|
+
);
|
|
103
118
|
};
|
|
104
119
|
|
|
105
120
|
/**
|
|
106
121
|
* Update the cache data with partial response data
|
|
107
122
|
* @param request
|
|
108
123
|
* @param partialResponse
|
|
124
|
+
* @param isTriggeredExtrenally - informs whether an update was triggered due to internal logic or externally, e.g.
|
|
125
|
+
* via plugin.
|
|
109
126
|
* @returns
|
|
110
127
|
*/
|
|
111
|
-
update = <Request extends RequestInstance
|
|
112
|
-
request:
|
|
113
|
-
partialResponse:
|
|
128
|
+
update = <Request extends RequestCacheType<RequestInstance>>(
|
|
129
|
+
request: Request,
|
|
130
|
+
partialResponse: CacheSetState<
|
|
114
131
|
Partial<
|
|
115
|
-
|
|
132
|
+
ResponseType<ExtractResponseType<Request>, ExtractErrorType<Request>, ExtractAdapterType<Request>> &
|
|
116
133
|
ResponseDetailsType
|
|
117
134
|
>
|
|
118
135
|
>,
|
|
119
136
|
): void => {
|
|
120
|
-
this.logger.debug("Processing cache update", { request, partialResponse });
|
|
137
|
+
this.logger.debug({ title: "Processing cache update", type: "system", extra: { request, partialResponse } });
|
|
121
138
|
const { cacheKey } = request;
|
|
122
139
|
const cachedData = this.storage.get<
|
|
123
140
|
ExtractResponseType<Request>,
|
|
@@ -125,7 +142,8 @@ export class Cache<C extends ClientInstance> {
|
|
|
125
142
|
ExtractAdapterType<Request>
|
|
126
143
|
>(cacheKey);
|
|
127
144
|
|
|
128
|
-
const processedResponse =
|
|
145
|
+
const processedResponse =
|
|
146
|
+
typeof partialResponse === "function" ? partialResponse(cachedData || null) : partialResponse;
|
|
129
147
|
if (cachedData) {
|
|
130
148
|
this.set(request, { ...cachedData, ...processedResponse });
|
|
131
149
|
}
|
|
@@ -136,10 +154,8 @@ export class Cache<C extends ClientInstance> {
|
|
|
136
154
|
* @param cacheKey
|
|
137
155
|
* @returns
|
|
138
156
|
*/
|
|
139
|
-
get = <Response, Error, Adapter
|
|
140
|
-
cacheKey
|
|
141
|
-
): CacheValueType<Response, Error, Adapter> | undefined => {
|
|
142
|
-
this.getLazyResource<Response, Error, Adapter>(cacheKey);
|
|
157
|
+
get = <Response, Error>(cacheKey: string): CacheValueType<Response, Error, Adapter> | undefined => {
|
|
158
|
+
this.getLazyResource<Response, Error>(cacheKey);
|
|
143
159
|
const cachedData = this.storage.get<Response, Error, Adapter>(cacheKey);
|
|
144
160
|
return cachedData;
|
|
145
161
|
};
|
|
@@ -159,31 +175,59 @@ export class Cache<C extends ClientInstance> {
|
|
|
159
175
|
* @param cacheKey
|
|
160
176
|
*/
|
|
161
177
|
delete = (cacheKey: string): void => {
|
|
162
|
-
this.logger.debug("Deleting cache element", { cacheKey });
|
|
178
|
+
this.logger.debug({ title: "Deleting cache element", type: "system", extra: { cacheKey } });
|
|
163
179
|
this.storage.delete(cacheKey);
|
|
164
|
-
this.options?.onDelete?.(cacheKey);
|
|
165
180
|
this.lazyStorage?.delete(cacheKey);
|
|
181
|
+
|
|
182
|
+
this.client.triggerPlugins("onCacheItemDelete", {
|
|
183
|
+
cache: this,
|
|
184
|
+
cacheKey,
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
this.events.emitDelete(cacheKey);
|
|
166
188
|
};
|
|
167
189
|
|
|
168
190
|
/**
|
|
169
191
|
* Invalidate cache by cacheKey or partial matching with RegExp
|
|
170
|
-
*
|
|
192
|
+
* It emits invalidation event for each matching cacheKey and sets staleTime to 0 to indicate out of time cache
|
|
193
|
+
* @param key - cacheKey or Request instance or RegExp for partial matching
|
|
171
194
|
*/
|
|
172
|
-
invalidate =
|
|
173
|
-
this.logger.debug("Revalidating cache element", {
|
|
174
|
-
const keys = await this.getLazyKeys();
|
|
195
|
+
invalidate = (cacheKeys: string | RegExp | RequestInstance | Array<string | RegExp | RequestInstance>) => {
|
|
196
|
+
this.logger.debug({ title: "Revalidating cache element", type: "system", extra: { cacheKeys } });
|
|
175
197
|
|
|
176
|
-
|
|
177
|
-
this.
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
if (cacheKey.test(entityKey)) {
|
|
183
|
-
this.events.emitInvalidation(entityKey);
|
|
184
|
-
this.delete(entityKey);
|
|
198
|
+
const onInvalidate = (key: string | RegExp | RequestInstance) => {
|
|
199
|
+
const keys = Array.from(this.storage.keys());
|
|
200
|
+
const handleInvalidation = (cacheKey: string) => {
|
|
201
|
+
const value = this.storage.get(cacheKey);
|
|
202
|
+
if (value) {
|
|
203
|
+
this.storage.set(cacheKey, { ...value, staleTime: 0 });
|
|
185
204
|
}
|
|
205
|
+
|
|
206
|
+
this.client.triggerPlugins("onCacheItemInvalidate", {
|
|
207
|
+
cache: this,
|
|
208
|
+
cacheKey,
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
this.events.emitInvalidation(cacheKey);
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
if (key instanceof Request) {
|
|
215
|
+
handleInvalidation(key.cacheKey);
|
|
216
|
+
} else if (typeof key === "string") {
|
|
217
|
+
handleInvalidation(key);
|
|
218
|
+
} else if (keys.length) {
|
|
219
|
+
keys.forEach((entityKey) => {
|
|
220
|
+
if (key.test(entityKey)) {
|
|
221
|
+
handleInvalidation(entityKey);
|
|
222
|
+
}
|
|
223
|
+
});
|
|
186
224
|
}
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
if (Array.isArray(cacheKeys)) {
|
|
228
|
+
cacheKeys.forEach(onInvalidate.bind(this));
|
|
229
|
+
} else {
|
|
230
|
+
onInvalidate.bind(this)(cacheKeys);
|
|
187
231
|
}
|
|
188
232
|
};
|
|
189
233
|
|
|
@@ -191,7 +235,7 @@ export class Cache<C extends ClientInstance> {
|
|
|
191
235
|
* Used to receive data from lazy storage
|
|
192
236
|
* @param cacheKey
|
|
193
237
|
*/
|
|
194
|
-
getLazyResource = async <Response, Error
|
|
238
|
+
getLazyResource = async <Response, Error>(
|
|
195
239
|
cacheKey: string,
|
|
196
240
|
): Promise<CacheValueType<Response, Error, Adapter> | undefined> => {
|
|
197
241
|
const data = await this.lazyStorage?.get<Response, Error, Adapter>(cacheKey);
|
|
@@ -201,21 +245,21 @@ export class Cache<C extends ClientInstance> {
|
|
|
201
245
|
const hasLazyData = this.lazyStorage && data;
|
|
202
246
|
if (hasLazyData) {
|
|
203
247
|
const now = +new Date();
|
|
204
|
-
const isNewestData = syncData ? syncData.
|
|
205
|
-
const isStaleData = data.
|
|
206
|
-
const isValidLazyData = data.
|
|
248
|
+
const isNewestData = syncData ? syncData.responseTimestamp < data.responseTimestamp : true;
|
|
249
|
+
const isStaleData = data.staleTime <= now - data.responseTimestamp;
|
|
250
|
+
const isValidLazyData = data.version === this.version;
|
|
207
251
|
|
|
208
252
|
if (!isValidLazyData) {
|
|
209
|
-
this.lazyStorage
|
|
253
|
+
this.lazyStorage?.delete(cacheKey);
|
|
210
254
|
}
|
|
211
255
|
if (isNewestData && !isStaleData && isValidLazyData) {
|
|
212
256
|
this.storage.set<Response, Error, Adapter>(cacheKey, data);
|
|
213
|
-
this.events.emitCacheData<Response, Error, Adapter>(cacheKey,
|
|
257
|
+
this.events.emitCacheData<Response, Error, Adapter>({ ...data, cacheKey, cached: true });
|
|
214
258
|
return data;
|
|
215
259
|
}
|
|
216
260
|
}
|
|
217
261
|
|
|
218
|
-
const isValidData = syncData?.
|
|
262
|
+
const isValidData = syncData?.version === this.version;
|
|
219
263
|
if (syncData && !isValidData) {
|
|
220
264
|
this.delete(cacheKey);
|
|
221
265
|
}
|
|
@@ -227,6 +271,16 @@ export class Cache<C extends ClientInstance> {
|
|
|
227
271
|
* @param cacheKey
|
|
228
272
|
*/
|
|
229
273
|
getLazyKeys = async () => {
|
|
274
|
+
const keys = (await this.lazyStorage?.keys()) || [];
|
|
275
|
+
|
|
276
|
+
return [...new Set(keys)];
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Used to receive keys from sync storage and lazy storage
|
|
281
|
+
* @param cacheKey
|
|
282
|
+
*/
|
|
283
|
+
getAllKeys = async () => {
|
|
230
284
|
const keys = await this.lazyStorage?.keys();
|
|
231
285
|
const asyncKeys = Array.from(keys || []);
|
|
232
286
|
const syncKeys = Array.from(this.storage.keys());
|
|
@@ -239,30 +293,42 @@ export class Cache<C extends ClientInstance> {
|
|
|
239
293
|
* @param cacheKey
|
|
240
294
|
* @returns
|
|
241
295
|
*/
|
|
242
|
-
scheduleGarbageCollector =
|
|
296
|
+
scheduleGarbageCollector = (cacheKey: string) => {
|
|
243
297
|
// We need to make sure that all of the values will be removed, also that we have the proper data
|
|
244
|
-
const cacheData =
|
|
298
|
+
const cacheData = this.storage.get(cacheKey);
|
|
245
299
|
|
|
246
300
|
// Clear running garbage collectors for given key
|
|
247
301
|
clearTimeout(this.garbageCollectors.get(cacheKey));
|
|
248
302
|
|
|
249
303
|
// Garbage collect
|
|
250
304
|
if (cacheData) {
|
|
251
|
-
const timeLeft = cacheData.
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
305
|
+
const timeLeft = cacheData.cacheTime + cacheData.responseTimestamp - +new Date();
|
|
306
|
+
// null
|
|
307
|
+
if (cacheData.cacheTime === null) {
|
|
308
|
+
this.logger.debug({ title: "Cache time is null", type: "system", extra: { cacheKey } });
|
|
309
|
+
}
|
|
310
|
+
// Infinity
|
|
311
|
+
else if (
|
|
312
|
+
(cacheData.cacheTime !== null && JSON.stringify(cacheData.cacheTime) === "null") ||
|
|
313
|
+
cacheData.cacheTime === Infinity
|
|
314
|
+
) {
|
|
315
|
+
this.logger.debug({ title: "Cache time is Infinite", type: "system", extra: { cacheKey } });
|
|
316
|
+
}
|
|
317
|
+
// Run garbage collector
|
|
318
|
+
else if (timeLeft >= 0) {
|
|
255
319
|
this.garbageCollectors.set(
|
|
256
320
|
cacheKey,
|
|
257
321
|
setTimeout(() => {
|
|
258
322
|
if (this.client.appManager.isOnline) {
|
|
259
|
-
this.logger.info("Garbage collecting cache
|
|
323
|
+
this.logger.info({ title: "Garbage collecting cache data", type: "system", extra: { cacheKey } });
|
|
260
324
|
this.delete(cacheKey);
|
|
261
325
|
}
|
|
262
326
|
}, timeLeft),
|
|
263
327
|
);
|
|
264
|
-
}
|
|
265
|
-
|
|
328
|
+
}
|
|
329
|
+
// Delete if value is stale and we are online
|
|
330
|
+
else if (this.client.appManager.isOnline) {
|
|
331
|
+
this.logger.info({ title: "Garbage collecting cache data", type: "system", extra: { cacheKey } });
|
|
266
332
|
this.delete(cacheKey);
|
|
267
333
|
}
|
|
268
334
|
}
|
|
@@ -272,6 +338,7 @@ export class Cache<C extends ClientInstance> {
|
|
|
272
338
|
* Clear cache storages
|
|
273
339
|
*/
|
|
274
340
|
clear = async (): Promise<void> => {
|
|
341
|
+
this.garbageCollectors.forEach((timeout) => clearTimeout(timeout));
|
|
275
342
|
this.storage.clear();
|
|
276
343
|
};
|
|
277
344
|
}
|
package/src/cache/cache.types.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { AdapterInstance, ResponseReturnType } from "adapter";
|
|
1
|
+
import { AdapterInstance, ResponseType } from "adapter";
|
|
3
2
|
import { ResponseDetailsType } from "managers";
|
|
4
|
-
import {
|
|
3
|
+
import { RequestInstance } from "request";
|
|
5
4
|
|
|
6
|
-
export type CacheOptionsType
|
|
5
|
+
export type CacheOptionsType = {
|
|
7
6
|
/**
|
|
8
7
|
* Assign your custom sync storage
|
|
9
8
|
*/
|
|
@@ -13,24 +12,10 @@ export type CacheOptionsType<C extends ClientInstance = ClientInstance> = {
|
|
|
13
12
|
*/
|
|
14
13
|
lazyStorage?: CacheAsyncStorageType;
|
|
15
14
|
/**
|
|
16
|
-
* Key to clear lazy storage data
|
|
15
|
+
* Key to clear lazy storage data, often used for versioning
|
|
16
|
+
* If the new key is different from the old one, the cache will be cleared
|
|
17
17
|
*/
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Initialization callback
|
|
21
|
-
*/
|
|
22
|
-
onInitialization?: (cache: Cache<C>) => void;
|
|
23
|
-
/**
|
|
24
|
-
* Callback for every change in the storage
|
|
25
|
-
*/
|
|
26
|
-
onChange?: <Response = any, Error = any, Adapter extends AdapterInstance = AdapterInstance>(
|
|
27
|
-
key: string,
|
|
28
|
-
data: CacheValueType<Response, Error, Adapter>,
|
|
29
|
-
) => void;
|
|
30
|
-
/**
|
|
31
|
-
* Callback for every delete in the storage
|
|
32
|
-
*/
|
|
33
|
-
onDelete?: (key: string) => void;
|
|
18
|
+
version?: string;
|
|
34
19
|
};
|
|
35
20
|
|
|
36
21
|
// Values
|
|
@@ -38,11 +23,14 @@ export type CacheValueType<
|
|
|
38
23
|
Response = any,
|
|
39
24
|
Error = any,
|
|
40
25
|
Adapter extends AdapterInstance = AdapterInstance,
|
|
41
|
-
> =
|
|
26
|
+
> = ResponseType<Response, Error, Adapter> &
|
|
42
27
|
ResponseDetailsType & {
|
|
28
|
+
cacheKey: string;
|
|
29
|
+
staleTime: number;
|
|
30
|
+
version: string;
|
|
43
31
|
cacheTime: number;
|
|
44
|
-
|
|
45
|
-
|
|
32
|
+
cached: boolean;
|
|
33
|
+
hydrated?: boolean;
|
|
46
34
|
};
|
|
47
35
|
|
|
48
36
|
// Storage
|
|
@@ -73,4 +61,6 @@ export type CacheStorageType = {
|
|
|
73
61
|
|
|
74
62
|
export type CacheInitialData = Record<string, CacheValueType>;
|
|
75
63
|
|
|
76
|
-
export type
|
|
64
|
+
export type CacheSetState<CacheData> = CacheData | ((previousData: CacheData | null) => CacheData);
|
|
65
|
+
|
|
66
|
+
export type RequestCacheType<R extends RequestInstance> = Pick<R, "cacheKey" | "cache" | "staleTime" | "cacheTime">;
|
package/src/cache/cache.utils.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { ResponseDetailsType } from "managers";
|
|
2
2
|
import { RequestInstance } from "request";
|
|
3
|
-
import {
|
|
3
|
+
import { ExtractAdapterResolvedType } from "types";
|
|
4
4
|
|
|
5
5
|
export const getCacheData = <T extends RequestInstance>(
|
|
6
|
-
previousResponse:
|
|
7
|
-
response:
|
|
8
|
-
):
|
|
6
|
+
previousResponse: ExtractAdapterResolvedType<T> | undefined,
|
|
7
|
+
response: ExtractAdapterResolvedType<T> & ResponseDetailsType,
|
|
8
|
+
): ExtractAdapterResolvedType<T> & ResponseDetailsType => {
|
|
9
9
|
const { data, success } = response;
|
|
10
10
|
|
|
11
11
|
const previousData = !success && previousResponse ? previousResponse.data : null;
|
|
@@ -14,14 +14,21 @@ export const getCacheData = <T extends RequestInstance>(
|
|
|
14
14
|
return { ...response, data: computedData };
|
|
15
15
|
};
|
|
16
16
|
|
|
17
|
-
export const
|
|
17
|
+
export const getInvalidateKey = (): string => {
|
|
18
|
+
return `invalidate`;
|
|
19
|
+
};
|
|
20
|
+
export const getInvalidateByKey = (key: string): string => {
|
|
18
21
|
return `${key}_invalidate`;
|
|
19
22
|
};
|
|
20
|
-
|
|
21
|
-
|
|
23
|
+
export const getDeleteKey = (): string => {
|
|
24
|
+
return `delete`;
|
|
25
|
+
};
|
|
26
|
+
export const getDeleteByKey = (key: string): string => {
|
|
27
|
+
return `${key}_delete`;
|
|
28
|
+
};
|
|
29
|
+
export const getCacheByKey = (key: string): string => {
|
|
22
30
|
return `${key}_cache`;
|
|
23
31
|
};
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
return `${key}_cache_by_id`;
|
|
32
|
+
export const getCacheKey = (): string => {
|
|
33
|
+
return `cache`;
|
|
27
34
|
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { TypeWithDefaults } from "types";
|
|
2
|
+
import { Client, ClientErrorType, ClientOptionsType } from "client";
|
|
3
|
+
import { HttpAdapterType } from "http-adapter";
|
|
4
|
+
|
|
5
|
+
export type ClientGenericType = {
|
|
6
|
+
error?: ClientErrorType;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export function createClient<
|
|
10
|
+
ClientProperties extends ClientGenericType = {
|
|
11
|
+
error?: ClientErrorType;
|
|
12
|
+
},
|
|
13
|
+
>(options: ClientOptionsType<Client<NonNullable<TypeWithDefaults<ClientProperties, "error", ClientErrorType>>>>) {
|
|
14
|
+
return new Client<NonNullable<TypeWithDefaults<ClientProperties, "error", ClientErrorType>>, HttpAdapterType>(
|
|
15
|
+
options,
|
|
16
|
+
);
|
|
17
|
+
}
|