diff --git a/types/index.d.ts b/types/index.d.ts index 244c73d..5e04d88 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -35,15 +35,28 @@ type FastifyCaching = FastifyPluginCallback = { + item: T, + stored: number, + ttl: number, +} | null; + declare namespace fastifyCaching { /** * @link [`abstract-cache` protocol documentation](https://github.com/jsumners/abstract-cache#protocol) */ export interface AbstractCacheCompliantObject { - get( + get( key: string | { id: string; segment: string }, - callback?: (error: unknown, result: unknown) => void + callback: (error: unknown, result: CacheResult) => void ): void; + /** + * If AbstractCache is using useAwait = true, then this method-header must be used. + * @param key + */ + get( + key: string | { id: string; segment: string }, + ): Promise>; set( key: string | { id: string; segment: string }, value: unknown, diff --git a/types/index.test-d.ts b/types/index.test-d.ts index a32601f..0fa7f34 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -50,3 +50,20 @@ const badCachingOptions = { }; expectError(shouldErrorApp.register(fastifyCaching, badCachingOptions)); + +fastify.get('/three', async (request, reply) => { + expectAssignable>( + fastify.cache.get('well-known') + ); + expectAssignable>( + fastify.cache.get('well-known') + ); + expectType( + fastify.cache.get('well-known', (err, value) => { + expectType(err); + expectAssignable<{ item: string; stored: number; ttl: number; } | null>(value); + }) + ); + + return { message: 'two' }; +});