diff --git a/packages/bentocache/src/bento_cache.ts b/packages/bentocache/src/bento_cache.ts index 0740784..4a6a8fe 100644 --- a/packages/bentocache/src/bento_cache.ts +++ b/packages/bentocache/src/bento_cache.ts @@ -15,12 +15,14 @@ import type { HasOptions, ClearOptions, GetSetFactory, + GetOrSetForeverPojoOptions, GetOrSetPojoOptions, GetPojoOptions, SetPojoOptions, HasPojoOptions, DeletePojoOptions, DeleteManyPojoOptions, + GetOrSetForeverOptions, } from './types/main.js' export class BentoCache> implements CacheProvider { @@ -197,9 +199,9 @@ export class BentoCache> implemen * provided by the factory forever and return it */ getOrSetForever( - key: string | GetOrSetPojoOptions, + key: string | GetOrSetForeverPojoOptions, cb?: GetSetFactory, - opts?: GetOrSetOptions, + opts?: GetOrSetForeverOptions, ): Promise { if (typeof key === 'string') { return this.use().getOrSetForever(key, cb!, opts) diff --git a/packages/bentocache/src/cache/cache.ts b/packages/bentocache/src/cache/cache.ts index 64f3b59..9705d2f 100644 --- a/packages/bentocache/src/cache/cache.ts +++ b/packages/bentocache/src/cache/cache.ts @@ -19,6 +19,8 @@ import type { HasPojoOptions, DeletePojoOptions, DeleteManyPojoOptions, + GetOrSetForeverPojoOptions, + GetOrSetForeverOptions, } from '../types/main.js' export class Cache implements CacheProvider { @@ -153,9 +155,9 @@ export class Cache implements CacheProvider { * provided by the factory forever and return it */ async getOrSetForever( - keyOrOptions: string | GetOrSetPojoOptions, + keyOrOptions: string | GetOrSetForeverPojoOptions, factory?: GetSetFactory, - options?: GetOrSetOptions, + options?: GetOrSetForeverOptions, ): Promise { if (typeof keyOrOptions === 'string') { const cacheOptions = this.#stack.defaultOptions.cloneWith({ ttl: null, ...options }) diff --git a/packages/bentocache/src/types/options/methods_options.ts b/packages/bentocache/src/types/options/methods_options.ts index b944a17..f6714c0 100644 --- a/packages/bentocache/src/types/options/methods_options.ts +++ b/packages/bentocache/src/types/options/methods_options.ts @@ -13,6 +13,22 @@ export type GetOrSetOptions = Pick< */ export type GetOrSetPojoOptions = { key: string; factory: GetSetFactory } & GetOrSetOptions +/** + * Options accepted by the `getOrSetForever` method + */ +export type GetOrSetForeverOptions = Pick< + RawCommonOptions, + 'earlyExpiration' | 'gracePeriod' | 'suppressL2Errors' | 'lockTimeout' | 'timeouts' +> + +/** + * Options accepted by the `getOrSetForever` method when passing an object + */ +export type GetOrSetForeverPojoOptions = { + key: string + factory: GetSetFactory +} & GetOrSetForeverOptions + /** * Options accepted by the `set` method */ diff --git a/packages/bentocache/src/types/provider.ts b/packages/bentocache/src/types/provider.ts index 1e59c0f..750bca2 100644 --- a/packages/bentocache/src/types/provider.ts +++ b/packages/bentocache/src/types/provider.ts @@ -5,6 +5,8 @@ import type { DeleteOptions, DeletePojoOptions, GetOptions, + GetOrSetForeverOptions, + GetOrSetForeverPojoOptions, GetOrSetOptions, GetOrSetPojoOptions, GetPojoOptions, @@ -53,8 +55,8 @@ export interface CacheProvider { /** * Get or set a value in the cache forever */ - getOrSetForever(options: GetOrSetPojoOptions): Promise - getOrSetForever(key: string, cb: GetSetFactory, opts?: GetOrSetOptions): Promise + getOrSetForever(options: GetOrSetForeverPojoOptions): Promise + getOrSetForever(key: string, cb: GetSetFactory, opts?: GetOrSetForeverOptions): Promise /** * Check if a key exists in the cache diff --git a/packages/bentocache/tests/typings.spec.ts b/packages/bentocache/tests/typings.spec.ts index d107f50..8bbe97f 100644 --- a/packages/bentocache/tests/typings.spec.ts +++ b/packages/bentocache/tests/typings.spec.ts @@ -198,4 +198,18 @@ test.group('Typings', () => { test('stores entries should accept raw options', async ({ expectTypeOf }) => { expectTypeOf(bentostore).toBeCallableWith({ gracePeriod: { enabled: true } }) }) + + test('cant pass ttl when using getOrSetForever', async () => { + const { bento } = new BentoCacheFactory().create() + + bento.getOrSetForever({ + key: 'foo', + factory: () => 'bar', + // @ts-expect-error - should not accept ttl + ttl: 100, + }) + + // @ts-expect-error - should not accept ttl + bento.getOrSetForever('foo', () => 'bar', { ttl: 100 }) + }) })