@sebspark/promise-cache 2.1.0 → 2.1.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/dist/index.d.mts CHANGED
@@ -75,10 +75,10 @@ declare class PromiseCache<U> {
75
75
  * @param key Cache key.
76
76
  * @param delegate The function to execute if the key is not in the cache.
77
77
  * @param ttlInSeconds Time to live in seconds.
78
- * @param ttlKey The key in the response object that contains the TTL.
78
+ * @param ttlKeyInSeconds The key in the response object that contains the TTL.
79
79
  * @returns The result of the delegate function.
80
80
  */
81
- wrap(key: string, delegate: () => Promise<U>, ttlInSeconds?: number, ttlKey?: string): Promise<U>;
81
+ wrap(key: string, delegate: () => Promise<U>, ttlInSeconds?: number, ttlKeyInSeconds?: string): Promise<U>;
82
82
  }
83
83
 
84
84
  declare class LocalStorage {
package/dist/index.d.ts CHANGED
@@ -75,10 +75,10 @@ declare class PromiseCache<U> {
75
75
  * @param key Cache key.
76
76
  * @param delegate The function to execute if the key is not in the cache.
77
77
  * @param ttlInSeconds Time to live in seconds.
78
- * @param ttlKey The key in the response object that contains the TTL.
78
+ * @param ttlKeyInSeconds The key in the response object that contains the TTL.
79
79
  * @returns The result of the delegate function.
80
80
  */
81
- wrap(key: string, delegate: () => Promise<U>, ttlInSeconds?: number, ttlKey?: string): Promise<U>;
81
+ wrap(key: string, delegate: () => Promise<U>, ttlInSeconds?: number, ttlKeyInSeconds?: string): Promise<U>;
82
82
  }
83
83
 
84
84
  declare class LocalStorage {
package/dist/index.js CHANGED
@@ -283,16 +283,16 @@ var PromiseCache = class {
283
283
  * @param key Cache key.
284
284
  * @param delegate The function to execute if the key is not in the cache.
285
285
  * @param ttlInSeconds Time to live in seconds.
286
- * @param ttlKey The key in the response object that contains the TTL.
286
+ * @param ttlKeyInSeconds The key in the response object that contains the TTL.
287
287
  * @returns The result of the delegate function.
288
288
  */
289
- async wrap(key, delegate, ttlInSeconds, ttlKey) {
289
+ async wrap(key, delegate, ttlInSeconds, ttlKeyInSeconds) {
290
290
  const now = Date.now();
291
291
  const effectiveKey = this.caseSensitive ? key : key.toLowerCase();
292
292
  let effectiveTTL = ttlInSeconds !== void 0 ? ttlInSeconds * 1e3 : this.ttl;
293
293
  const cached = await this.persistor.get(effectiveKey);
294
294
  if (cached) {
295
- if (!ttlKey && cached.ttl !== effectiveTTL) {
295
+ if (!ttlKeyInSeconds && cached.ttl !== effectiveTTL) {
296
296
  console.error(
297
297
  `WARNING: TTL mismatch for key: ${effectiveKey}. It is recommended to use the same TTL for the same key.`
298
298
  );
@@ -300,9 +300,10 @@ var PromiseCache = class {
300
300
  return cached.value;
301
301
  }
302
302
  const response = await delegate();
303
- if (ttlKey) {
303
+ if (ttlKeyInSeconds) {
304
304
  const responseDict = response;
305
- effectiveTTL = Number(responseDict[ttlKey]) || effectiveTTL;
305
+ const responseTTL = Number(responseDict[ttlKeyInSeconds]) * 1e3;
306
+ effectiveTTL = responseTTL || effectiveTTL;
306
307
  }
307
308
  this.persistor.set(effectiveKey, {
308
309
  value: response,
package/dist/index.mjs CHANGED
@@ -254,16 +254,16 @@ var PromiseCache = class {
254
254
  * @param key Cache key.
255
255
  * @param delegate The function to execute if the key is not in the cache.
256
256
  * @param ttlInSeconds Time to live in seconds.
257
- * @param ttlKey The key in the response object that contains the TTL.
257
+ * @param ttlKeyInSeconds The key in the response object that contains the TTL.
258
258
  * @returns The result of the delegate function.
259
259
  */
260
- async wrap(key, delegate, ttlInSeconds, ttlKey) {
260
+ async wrap(key, delegate, ttlInSeconds, ttlKeyInSeconds) {
261
261
  const now = Date.now();
262
262
  const effectiveKey = this.caseSensitive ? key : key.toLowerCase();
263
263
  let effectiveTTL = ttlInSeconds !== void 0 ? ttlInSeconds * 1e3 : this.ttl;
264
264
  const cached = await this.persistor.get(effectiveKey);
265
265
  if (cached) {
266
- if (!ttlKey && cached.ttl !== effectiveTTL) {
266
+ if (!ttlKeyInSeconds && cached.ttl !== effectiveTTL) {
267
267
  console.error(
268
268
  `WARNING: TTL mismatch for key: ${effectiveKey}. It is recommended to use the same TTL for the same key.`
269
269
  );
@@ -271,9 +271,10 @@ var PromiseCache = class {
271
271
  return cached.value;
272
272
  }
273
273
  const response = await delegate();
274
- if (ttlKey) {
274
+ if (ttlKeyInSeconds) {
275
275
  const responseDict = response;
276
- effectiveTTL = Number(responseDict[ttlKey]) || effectiveTTL;
276
+ const responseTTL = Number(responseDict[ttlKeyInSeconds]) * 1e3;
277
+ effectiveTTL = responseTTL || effectiveTTL;
277
278
  }
278
279
  this.persistor.set(effectiveKey, {
279
280
  value: response,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sebspark/promise-cache",
3
- "version": "2.1.0",
3
+ "version": "2.1.1",
4
4
  "license": "Apache-2.0",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",