Skip to content

Commit

Permalink
fix: remove ttl usage from getOrSetForever
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-R44 committed Oct 1, 2024
1 parent 7267974 commit 32943e2
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 6 deletions.
6 changes: 4 additions & 2 deletions packages/bentocache/src/bento_cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ import type {
HasOptions,
ClearOptions,
GetSetFactory,
GetOrSetForeverPojoOptions,
GetOrSetPojoOptions,
GetPojoOptions,
SetPojoOptions,
HasPojoOptions,
DeletePojoOptions,
DeleteManyPojoOptions,
GetOrSetForeverOptions,
} from './types/main.js'

export class BentoCache<KnownCaches extends Record<string, BentoStore>> implements CacheProvider {
Expand Down Expand Up @@ -197,9 +199,9 @@ export class BentoCache<KnownCaches extends Record<string, BentoStore>> implemen
* provided by the factory forever and return it
*/
getOrSetForever<T>(
key: string | GetOrSetPojoOptions<T>,
key: string | GetOrSetForeverPojoOptions<T>,
cb?: GetSetFactory<T>,
opts?: GetOrSetOptions,
opts?: GetOrSetForeverOptions,
): Promise<T> {
if (typeof key === 'string') {
return this.use().getOrSetForever(key, cb!, opts)
Expand Down
6 changes: 4 additions & 2 deletions packages/bentocache/src/cache/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import type {
HasPojoOptions,
DeletePojoOptions,
DeleteManyPojoOptions,
GetOrSetForeverPojoOptions,
GetOrSetForeverOptions,
} from '../types/main.js'

export class Cache implements CacheProvider {
Expand Down Expand Up @@ -153,9 +155,9 @@ export class Cache implements CacheProvider {
* provided by the factory forever and return it
*/
async getOrSetForever<T>(
keyOrOptions: string | GetOrSetPojoOptions<T>,
keyOrOptions: string | GetOrSetForeverPojoOptions<T>,
factory?: GetSetFactory<T>,
options?: GetOrSetOptions,
options?: GetOrSetForeverOptions,
): Promise<T> {
if (typeof keyOrOptions === 'string') {
const cacheOptions = this.#stack.defaultOptions.cloneWith({ ttl: null, ...options })
Expand Down
16 changes: 16 additions & 0 deletions packages/bentocache/src/types/options/methods_options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ export type GetOrSetOptions = Pick<
*/
export type GetOrSetPojoOptions<T> = { key: string; factory: GetSetFactory<T> } & 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<T> = {
key: string
factory: GetSetFactory<T>
} & GetOrSetForeverOptions

/**
* Options accepted by the `set` method
*/
Expand Down
6 changes: 4 additions & 2 deletions packages/bentocache/src/types/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import type {
DeleteOptions,
DeletePojoOptions,
GetOptions,
GetOrSetForeverOptions,
GetOrSetForeverPojoOptions,
GetOrSetOptions,
GetOrSetPojoOptions,
GetPojoOptions,
Expand Down Expand Up @@ -53,8 +55,8 @@ export interface CacheProvider {
/**
* Get or set a value in the cache forever
*/
getOrSetForever<T>(options: GetOrSetPojoOptions<T>): Promise<T>
getOrSetForever<T>(key: string, cb: GetSetFactory<T>, opts?: GetOrSetOptions): Promise<T>
getOrSetForever<T>(options: GetOrSetForeverPojoOptions<T>): Promise<T>
getOrSetForever<T>(key: string, cb: GetSetFactory<T>, opts?: GetOrSetForeverOptions): Promise<T>

/**
* Check if a key exists in the cache
Expand Down
14 changes: 14 additions & 0 deletions packages/bentocache/tests/typings.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
})
})

0 comments on commit 32943e2

Please sign in to comment.