@sebspark/promise-cache 3.4.0 → 3.6.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/README.md CHANGED
@@ -173,7 +173,7 @@ The basic functionality of this new implementation is:
173
173
  3. You call the cache's wrap function to wrap your original function(s)
174
174
  4. You call the wrapped function(s) instead of the originals
175
175
 
176
- ** Why the change? **
176
+ **Why the change?**
177
177
 
178
178
  When trying to add support for caching _to_ a specified time in addition to _for_ a specified time, it tuyrned out to be hard given the original implementation. Changing the implementation proved hard without breaking changes. So a new implementation is is.
179
179
 
@@ -276,6 +276,18 @@ const cachedGetById = cache.wrap(myApiCall, {
276
276
  key: (id) => `getById:${id}`,
277
277
  expires: time.seconds(100),
278
278
  })
279
+
280
+ // Fixed at today 20:00:00 UTC
281
+ const cachedGetById = cache.wrap(myApiCall, {
282
+ key: (id) => `getById:${id}`,
283
+ expires: time.today(20),
284
+ })
285
+
286
+ // Fixed at tomorrow 00:00:00
287
+ const cachedGetById = cache.wrap(myApiCall, {
288
+ key: (id) => `getById:${id}`,
289
+ expires: time.tomorrow(),
290
+ })
279
291
  ```
280
292
 
281
293
  If expiry is not set or yields an unusable value, it will default to 1 sec.
package/dist/index.d.mts CHANGED
@@ -2,7 +2,7 @@ import { RedisClientOptions, createClient, SetOptions } from 'redis';
2
2
  export { RedisClientOptions } from 'redis';
3
3
  import { Logger } from 'winston';
4
4
  import { UUID } from 'node:crypto';
5
- import { add } from 'date-fns';
5
+ import { add, sub } from 'date-fns';
6
6
 
7
7
  type GetType<T> = {
8
8
  value: T;
@@ -304,6 +304,8 @@ declare const minutes: (num: number) => number;
304
304
  declare const hours: (num: number) => number;
305
305
  declare const days: (num: number) => number;
306
306
  declare const weeks: (num: number) => number;
307
+ declare const today: (hours?: number, minutes?: number, seconds?: number) => Date;
308
+ declare const tomorrow: (hours?: number, minutes?: number, seconds?: number) => Date;
307
309
 
308
310
  declare const time_DAY: typeof DAY;
309
311
  declare const time_HOUR: typeof HOUR;
@@ -315,9 +317,21 @@ declare const time_days: typeof days;
315
317
  declare const time_hours: typeof hours;
316
318
  declare const time_minutes: typeof minutes;
317
319
  declare const time_seconds: typeof seconds;
320
+ declare const time_sub: typeof sub;
321
+ declare const time_today: typeof today;
322
+ declare const time_tomorrow: typeof tomorrow;
318
323
  declare const time_weeks: typeof weeks;
319
324
  declare namespace time {
320
- export { time_DAY as DAY, time_HOUR as HOUR, time_MINUTE as MINUTE, time_SECOND as SECOND, time_WEEK as WEEK, time_add as add, time_days as days, time_hours as hours, time_minutes as minutes, time_seconds as seconds, time_weeks as weeks };
325
+ export { time_DAY as DAY, time_HOUR as HOUR, time_MINUTE as MINUTE, time_SECOND as SECOND, time_WEEK as WEEK, time_add as add, time_days as days, time_hours as hours, time_minutes as minutes, time_seconds as seconds, time_sub as sub, time_today as today, time_tomorrow as tomorrow, time_weeks as weeks };
321
326
  }
322
327
 
323
- export { type Cache, type CachingOptions, type IPersistor, InMemoryPersistor, Persistor, type PersistorConstructorType, PromiseCache, type PromiseCacheOptions, createCache, time };
328
+ declare const serialize: <T>(data: T) => string;
329
+ declare const deserialize: <T>(serialized: string) => T;
330
+
331
+ declare const serializer_deserialize: typeof deserialize;
332
+ declare const serializer_serialize: typeof serialize;
333
+ declare namespace serializer {
334
+ export { serializer_deserialize as deserialize, serializer_serialize as serialize };
335
+ }
336
+
337
+ export { type Cache, type CachingOptions, type IPersistor, InMemoryPersistor, Persistor, type PersistorConstructorType, PromiseCache, type PromiseCacheOptions, createCache, serializer, time };
package/dist/index.d.ts CHANGED
@@ -2,7 +2,7 @@ import { RedisClientOptions, createClient, SetOptions } from 'redis';
2
2
  export { RedisClientOptions } from 'redis';
3
3
  import { Logger } from 'winston';
4
4
  import { UUID } from 'node:crypto';
5
- import { add } from 'date-fns';
5
+ import { add, sub } from 'date-fns';
6
6
 
7
7
  type GetType<T> = {
8
8
  value: T;
@@ -304,6 +304,8 @@ declare const minutes: (num: number) => number;
304
304
  declare const hours: (num: number) => number;
305
305
  declare const days: (num: number) => number;
306
306
  declare const weeks: (num: number) => number;
307
+ declare const today: (hours?: number, minutes?: number, seconds?: number) => Date;
308
+ declare const tomorrow: (hours?: number, minutes?: number, seconds?: number) => Date;
307
309
 
308
310
  declare const time_DAY: typeof DAY;
309
311
  declare const time_HOUR: typeof HOUR;
@@ -315,9 +317,21 @@ declare const time_days: typeof days;
315
317
  declare const time_hours: typeof hours;
316
318
  declare const time_minutes: typeof minutes;
317
319
  declare const time_seconds: typeof seconds;
320
+ declare const time_sub: typeof sub;
321
+ declare const time_today: typeof today;
322
+ declare const time_tomorrow: typeof tomorrow;
318
323
  declare const time_weeks: typeof weeks;
319
324
  declare namespace time {
320
- export { time_DAY as DAY, time_HOUR as HOUR, time_MINUTE as MINUTE, time_SECOND as SECOND, time_WEEK as WEEK, time_add as add, time_days as days, time_hours as hours, time_minutes as minutes, time_seconds as seconds, time_weeks as weeks };
325
+ export { time_DAY as DAY, time_HOUR as HOUR, time_MINUTE as MINUTE, time_SECOND as SECOND, time_WEEK as WEEK, time_add as add, time_days as days, time_hours as hours, time_minutes as minutes, time_seconds as seconds, time_sub as sub, time_today as today, time_tomorrow as tomorrow, time_weeks as weeks };
321
326
  }
322
327
 
323
- export { type Cache, type CachingOptions, type IPersistor, InMemoryPersistor, Persistor, type PersistorConstructorType, PromiseCache, type PromiseCacheOptions, createCache, time };
328
+ declare const serialize: <T>(data: T) => string;
329
+ declare const deserialize: <T>(serialized: string) => T;
330
+
331
+ declare const serializer_deserialize: typeof deserialize;
332
+ declare const serializer_serialize: typeof serialize;
333
+ declare namespace serializer {
334
+ export { serializer_deserialize as deserialize, serializer_serialize as serialize };
335
+ }
336
+
337
+ export { type Cache, type CachingOptions, type IPersistor, InMemoryPersistor, Persistor, type PersistorConstructorType, PromiseCache, type PromiseCacheOptions, createCache, serializer, time };
package/dist/index.js CHANGED
@@ -24,6 +24,7 @@ __export(index_exports, {
24
24
  Persistor: () => Persistor,
25
25
  PromiseCache: () => PromiseCache,
26
26
  createCache: () => createCache,
27
+ serializer: () => serializer_exports,
27
28
  time: () => time_exports
28
29
  });
29
30
  module.exports = __toCommonJS(index_exports);
@@ -356,6 +357,11 @@ var PromiseCache = class {
356
357
  };
357
358
 
358
359
  // src/serializer.ts
360
+ var serializer_exports = {};
361
+ __export(serializer_exports, {
362
+ deserialize: () => deserialize,
363
+ serialize: () => serialize
364
+ });
359
365
  var fixESM2 = require("fix-esm");
360
366
  var superjson2 = fixESM2.require("superjson");
361
367
  var serialize = (data) => {
@@ -590,6 +596,9 @@ __export(time_exports, {
590
596
  hours: () => hours,
591
597
  minutes: () => minutes,
592
598
  seconds: () => seconds,
599
+ sub: () => import_date_fns.sub,
600
+ today: () => today,
601
+ tomorrow: () => tomorrow,
593
602
  weeks: () => weeks
594
603
  });
595
604
  var import_date_fns = require("date-fns");
@@ -603,11 +612,25 @@ var minutes = (num) => num * MINUTE;
603
612
  var hours = (num) => num * HOUR;
604
613
  var days = (num) => num * DAY;
605
614
  var weeks = (num) => num * WEEK;
615
+ var today = (hours2 = 0, minutes2 = 0, seconds2 = 0) => {
616
+ const local = /* @__PURE__ */ new Date();
617
+ const utc = Date.UTC(
618
+ local.getUTCFullYear(),
619
+ local.getUTCMonth(),
620
+ local.getUTCDate(),
621
+ hours2,
622
+ minutes2,
623
+ seconds2
624
+ );
625
+ return new Date(utc);
626
+ };
627
+ var tomorrow = (hours2 = 0, minutes2 = 0, seconds2 = 0) => (0, import_date_fns.add)(today(hours2, minutes2, seconds2), { days: 1 });
606
628
  // Annotate the CommonJS export names for ESM import in node:
607
629
  0 && (module.exports = {
608
630
  InMemoryPersistor,
609
631
  Persistor,
610
632
  PromiseCache,
611
633
  createCache,
634
+ serializer,
612
635
  time
613
636
  });
package/dist/index.mjs CHANGED
@@ -338,6 +338,11 @@ var PromiseCache = class {
338
338
  };
339
339
 
340
340
  // src/serializer.ts
341
+ var serializer_exports = {};
342
+ __export(serializer_exports, {
343
+ deserialize: () => deserialize,
344
+ serialize: () => serialize
345
+ });
341
346
  var fixESM2 = __require("fix-esm");
342
347
  var superjson2 = fixESM2.require("superjson");
343
348
  var serialize = (data) => {
@@ -572,9 +577,12 @@ __export(time_exports, {
572
577
  hours: () => hours,
573
578
  minutes: () => minutes,
574
579
  seconds: () => seconds,
580
+ sub: () => sub,
581
+ today: () => today,
582
+ tomorrow: () => tomorrow,
575
583
  weeks: () => weeks
576
584
  });
577
- import { add } from "date-fns";
585
+ import { add, sub } from "date-fns";
578
586
  var SECOND = 1e3;
579
587
  var MINUTE = 60 * SECOND;
580
588
  var HOUR = 60 * MINUTE;
@@ -585,10 +593,24 @@ var minutes = (num) => num * MINUTE;
585
593
  var hours = (num) => num * HOUR;
586
594
  var days = (num) => num * DAY;
587
595
  var weeks = (num) => num * WEEK;
596
+ var today = (hours2 = 0, minutes2 = 0, seconds2 = 0) => {
597
+ const local = /* @__PURE__ */ new Date();
598
+ const utc = Date.UTC(
599
+ local.getUTCFullYear(),
600
+ local.getUTCMonth(),
601
+ local.getUTCDate(),
602
+ hours2,
603
+ minutes2,
604
+ seconds2
605
+ );
606
+ return new Date(utc);
607
+ };
608
+ var tomorrow = (hours2 = 0, minutes2 = 0, seconds2 = 0) => add(today(hours2, minutes2, seconds2), { days: 1 });
588
609
  export {
589
610
  InMemoryPersistor,
590
611
  Persistor,
591
612
  PromiseCache,
592
613
  createCache,
614
+ serializer_exports as serializer,
593
615
  time_exports as time
594
616
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sebspark/promise-cache",
3
- "version": "3.4.0",
3
+ "version": "3.6.0",
4
4
  "license": "Apache-2.0",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",