From 42814782dc5f36bc172a5814445f3cf7b3cf87b7 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Thu, 7 Dec 2023 16:23:06 +0100 Subject: [PATCH 1/5] fix: typings of fastify.cache.get --- types/index.d.ts | 7 +++++-- types/index.test-d.ts | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index 244c73d..cdedc34 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -40,10 +40,13 @@ 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: (T | undefined)) => void ): void; + get( + key: string | { id: string; segment: string }, + ): (T | undefined); 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..21032cd 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) => { + expectType( + fastify.cache.get('well-known') + ); + expectType( + fastify.cache.get('well-known') + ); + expectType( + fastify.cache.get('well-known', (err, value) => { + expectType(err); + expectType(value); + }) + ); + + return { message: 'two' }; +}); From 17b0e7c73950ef62b00136093b47632f32b24d5f Mon Sep 17 00:00:00 2001 From: uzlopak Date: Thu, 7 Dec 2023 17:02:33 +0100 Subject: [PATCH 2/5] fix --- types/index.d.ts | 14 ++++++++++++-- types/index.test-d.ts | 4 ++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index cdedc34..5580dc6 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -35,6 +35,12 @@ type FastifyCaching = FastifyPluginCallback = { + item: T | null, + stored: number, + ttl: number, +}; + declare namespace fastifyCaching { /** * @link [`abstract-cache` protocol documentation](https://github.com/jsumners/abstract-cache#protocol) @@ -42,11 +48,15 @@ declare namespace fastifyCaching { export interface AbstractCacheCompliantObject { get( key: string | { id: string; segment: string }, - callback: (error: unknown, result: (T | undefined)) => void + callback: (error: unknown, result: UseAwaitCacheResult) => void ): void; + /** + * If AbstractCache is using useAwait = true, then this method-header must be used. + * @param key + */ get( key: string | { id: string; segment: string }, - ): (T | undefined); + ): 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 21032cd..772bef4 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -55,13 +55,13 @@ fastify.get('/three', async (request, reply) => { expectType( fastify.cache.get('well-known') ); - expectType( + expectAssignable>( fastify.cache.get('well-known') ); expectType( fastify.cache.get('well-known', (err, value) => { expectType(err); - expectType(value); + expectAssignable<{ item: string | null; stored: number; ttl: number; }>(value); }) ); From c5e81827b8ba2061b90ecb7b0b4bfe5a5dbb90d5 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Thu, 7 Dec 2023 17:05:25 +0100 Subject: [PATCH 3/5] fix --- types/index.d.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index 5580dc6..5e04d88 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -35,11 +35,11 @@ type FastifyCaching = FastifyPluginCallback = { - item: T | null, +type CacheResult = { + item: T, stored: number, ttl: number, -}; +} | null; declare namespace fastifyCaching { /** @@ -48,7 +48,7 @@ declare namespace fastifyCaching { export interface AbstractCacheCompliantObject { get( key: string | { id: string; segment: string }, - callback: (error: unknown, result: UseAwaitCacheResult) => void + callback: (error: unknown, result: CacheResult) => void ): void; /** * If AbstractCache is using useAwait = true, then this method-header must be used. @@ -56,7 +56,7 @@ declare namespace fastifyCaching { */ get( key: string | { id: string; segment: string }, - ): Promise>; + ): Promise>; set( key: string | { id: string; segment: string }, value: unknown, From e9024bdfcded3582f248aa7e5cbc06a719a79093 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Thu, 7 Dec 2023 17:07:17 +0100 Subject: [PATCH 4/5] fix --- types/index.test-d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/types/index.test-d.ts b/types/index.test-d.ts index 772bef4..4d4a652 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -55,13 +55,13 @@ fastify.get('/three', async (request, reply) => { expectType( fastify.cache.get('well-known') ); - expectAssignable>( + expectAssignable>( fastify.cache.get('well-known') ); expectType( fastify.cache.get('well-known', (err, value) => { expectType(err); - expectAssignable<{ item: string | null; stored: number; ttl: number; }>(value); + expectAssignable<{ item: string; stored: number; ttl: number; } | null>(value); }) ); From 3b1e4c570c6c5d8d1463d736f4e7e54ade36c744 Mon Sep 17 00:00:00 2001 From: Aras Abbasi Date: Thu, 7 Dec 2023 17:30:26 +0100 Subject: [PATCH 5/5] Update index.test-d.ts --- types/index.test-d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/index.test-d.ts b/types/index.test-d.ts index 4d4a652..0fa7f34 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -52,7 +52,7 @@ const badCachingOptions = { expectError(shouldErrorApp.register(fastifyCaching, badCachingOptions)); fastify.get('/three', async (request, reply) => { - expectType( + expectAssignable>( fastify.cache.get('well-known') ); expectAssignable>(