@hyper-fetch/core 1.6.0 → 1.7.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hyper-fetch/core",
3
- "version": "1.6.0",
3
+ "version": "1.7.0",
4
4
  "private": false,
5
5
  "description": "Cache, Queue and Persist your requests no matter if you are online or offline!",
6
6
  "author": "Maciej Pyrc <m.pyrc@interia.pl>",
@@ -21,7 +21,7 @@
21
21
  "lint": "eslint . --ext .js,.jsx,.tsx,.ts --fix",
22
22
  "format": "prettier --write .",
23
23
  "precommit": "yarn lint-staged",
24
- "release": "yarn semantic-release --extends ../../.releaserc.json -t 'hyper-fetch-v${version}'"
24
+ "release": "yarn semantic-release --extends ./release.config.js -t 'hyper-fetch-v${version}'"
25
25
  },
26
26
  "publishConfig": {
27
27
  "access": "public",
@@ -0,0 +1,8 @@
1
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
2
+ const config = require("../../release.config");
3
+
4
+ // We want to publish only single release notes
5
+ const newConfig = { ...config, publish: undefined, success: undefined, fail: undefined };
6
+ newConfig.release.plugins.push(["@semantic-release/git"]);
7
+
8
+ module.exports = newConfig;
@@ -30,6 +30,7 @@ import { interceptRequest, interceptResponse } from "./builder.utils";
30
30
  * **Builder** is a class that allows you to configure the connection with the server and then use it to create
31
31
  * commands which, when called using the appropriate method, will cause the server to be queried for the endpoint and
32
32
  * method specified in the command.
33
+ * @position 1
33
34
  */
34
35
  export class Builder<GlobalErrorType extends BuilderErrorType = Error, RequestConfigType = ClientDefaultOptionsType> {
35
36
  readonly baseUrl: string;
@@ -64,14 +64,14 @@ export class Cache {
64
64
  details: CommandResponseDetails,
65
65
  ): void => {
66
66
  this.logger.debug("Processing cache response", { command, response, details });
67
- const { cacheKey, cache, cacheTime } = command;
67
+ const { cacheKey, cache, cacheTime, garbageCollection } = command;
68
68
  const cachedData = this.storage.get<Response, Error>(cacheKey);
69
69
 
70
70
  // Once refresh error occurs we don't want to override already valid data in our cache with the thrown error
71
71
  // We need to check it against cache and return last valid data we have
72
72
  const data = getCacheData(cachedData?.data, response);
73
73
 
74
- const newCacheData: CacheValueType = { data, details, cacheTime, clearKey: this.clearKey };
74
+ const newCacheData: CacheValueType = { data, details, cacheTime, clearKey: this.clearKey, garbageCollection };
75
75
 
76
76
  this.events.emitCacheData<Response, Error>(cacheKey, newCacheData);
77
77
  this.logger.debug("Emitting cache response", { command, response, details });
@@ -193,20 +193,23 @@ export class Cache {
193
193
  // We need to make sure that all of the values will be removed, also that we have the proper data
194
194
  const cacheData = await this.getLazyResource(cacheKey);
195
195
 
196
- if (cacheData) {
196
+ // Clear running garbage collectors for given key
197
+ clearTimeout(this.garbageCollectors.get(cacheKey));
198
+
199
+ if (cacheData && cacheData.garbageCollection) {
197
200
  const timeLeft = cacheData.cacheTime + cacheData.details.timestamp - +new Date();
198
201
  if (timeLeft >= 0) {
199
- clearTimeout(this.garbageCollectors.get(cacheKey));
200
202
  this.garbageCollectors.set(
201
203
  cacheKey,
202
204
  setTimeout(() => {
203
- if (this.builder.appManager.isOnline) {
205
+ if (this.builder.appManager.isOnline && cacheData.garbageCollection) {
204
206
  this.logger.info("Garbage collecting cache element", { cacheKey });
205
207
  this.delete(cacheKey);
206
208
  }
207
209
  }, timeLeft),
208
210
  );
209
- } else if (this.builder.appManager.isOnline) {
211
+ } else if (this.builder.appManager.isOnline && cacheData.garbageCollection) {
212
+ this.logger.info("Garbage collecting cache element", { cacheKey });
210
213
  this.delete(cacheKey);
211
214
  }
212
215
  }
@@ -35,6 +35,7 @@ export type CacheValueType<Response = any, Error = any> = {
35
35
  details: CommandResponseDetails;
36
36
  cacheTime: number;
37
37
  clearKey: string;
38
+ garbageCollection: boolean;
38
39
  };
39
40
 
40
41
  // Storage
@@ -53,6 +53,7 @@ export class Command<
53
53
  cancelable: boolean;
54
54
  retry: number;
55
55
  retryTime: number;
56
+ garbageCollection: boolean;
56
57
  cache: boolean;
57
58
  cacheTime: number;
58
59
  queued: boolean;
@@ -95,6 +96,7 @@ export class Command<
95
96
  cancelable = false,
96
97
  retry = 0,
97
98
  retryTime = 500,
99
+ garbageCollection = true,
98
100
  cache = true,
99
101
  cacheTime = DateInterval.minute * 5,
100
102
  queued = false,
@@ -118,6 +120,7 @@ export class Command<
118
120
  this.cancelable = commandDump?.cancelable ?? cancelable;
119
121
  this.retry = commandDump?.retry ?? retry;
120
122
  this.retryTime = commandDump?.retryTime ?? retryTime;
123
+ this.garbageCollection = commandDump?.garbageCollection ?? garbageCollection;
121
124
  this.cache = commandDump?.cache ?? cache;
122
125
  this.cacheTime = commandDump?.cacheTime ?? cacheTime;
123
126
  this.queued = commandDump?.queued ?? queued;
@@ -175,6 +178,12 @@ export class Command<
175
178
  return this.clone({ retryTime });
176
179
  };
177
180
 
181
+ public setGarbageCollection = (
182
+ garbageCollection: CommandConfig<EndpointType, ClientOptions>["garbageCollection"],
183
+ ) => {
184
+ return this.clone({ garbageCollection });
185
+ };
186
+
178
187
  public setCache = (cache: CommandConfig<EndpointType, ClientOptions>["cache"]) => {
179
188
  return this.clone({ cache });
180
189
  };
@@ -273,6 +282,7 @@ export class Command<
273
282
  cancelable: this.cancelable,
274
283
  retry: this.retry,
275
284
  retryTime: this.retryTime,
285
+ garbageCollection: this.garbageCollection,
276
286
  cache: this.cache,
277
287
  cacheTime: this.cacheTime,
278
288
  queued: this.queued,
@@ -373,7 +383,11 @@ export class Command<
373
383
  /**
374
384
  * Method to use the command WITHOUT adding it to cache and queues. This mean it will make simple request without queue side effects.
375
385
  * @param options
386
+ * @disableReturns
376
387
  * @returns
388
+ * ```tsx
389
+ * Promise<[Data | null, Error | null, HttpStatus]>
390
+ * ```
377
391
  */
378
392
  public exec: FetchMethodType<
379
393
  Command<
@@ -418,6 +432,11 @@ export class Command<
418
432
  * Method used to perform requests with usage of cache and queues
419
433
  * @param options
420
434
  * @param requestCallback
435
+ * @disableReturns
436
+ * @returns
437
+ * ```tsx
438
+ * Promise<[Data | null, Error | null, HttpStatus]>
439
+ * ```
421
440
  */
422
441
  public send: FetchMethodType<
423
442
  Command<
@@ -45,6 +45,7 @@ export type CommandDump<
45
45
  cancelable: boolean;
46
46
  retry: number;
47
47
  retryTime: number;
48
+ garbageCollection: boolean;
48
49
  cache: boolean;
49
50
  cacheTime: number;
50
51
  queued: boolean;
@@ -102,6 +103,10 @@ export type CommandConfig<GenericEndpoint extends string, ClientOptions> = {
102
103
  * Retry time delay between retries
103
104
  */
104
105
  retryTime?: number;
106
+ /**
107
+ * Should we trigger garbage collection or leave data in memory
108
+ */
109
+ garbageCollection?: boolean;
105
110
  /**
106
111
  * Should we save the response to cache
107
112
  */