Skip to content

Commit 7ea79e7

Browse files
authored
fix: typings of fastify.cache.get (#133)
* fix: typings of fastify.cache.get * fix * fix * fix * Update index.test-d.ts
1 parent 3733c8c commit 7ea79e7

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

types/index.d.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,28 @@ type FastifyCaching = FastifyPluginCallback<fastifyCaching.FastifyCachingPluginO
3535
privacy: fastifyCaching.Privacy;
3636
};
3737

38+
type CacheResult<T> = {
39+
item: T,
40+
stored: number,
41+
ttl: number,
42+
} | null;
43+
3844
declare namespace fastifyCaching {
3945
/**
4046
* @link [`abstract-cache` protocol documentation](https://github.com/jsumners/abstract-cache#protocol)
4147
*/
4248
export interface AbstractCacheCompliantObject {
43-
get(
49+
get<T = unknown>(
4450
key: string | { id: string; segment: string },
45-
callback?: (error: unknown, result: unknown) => void
51+
callback: (error: unknown, result: CacheResult<T>) => void
4652
): void;
53+
/**
54+
* If AbstractCache is using useAwait = true, then this method-header must be used.
55+
* @param key
56+
*/
57+
get<T = unknown>(
58+
key: string | { id: string; segment: string },
59+
): Promise<CacheResult<T>>;
4760
set(
4861
key: string | { id: string; segment: string },
4962
value: unknown,

types/index.test-d.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,20 @@ const badCachingOptions = {
5050
};
5151

5252
expectError(shouldErrorApp.register(fastifyCaching, badCachingOptions));
53+
54+
fastify.get('/three', async (request, reply) => {
55+
expectAssignable<Promise<unknown>>(
56+
fastify.cache.get('well-known')
57+
);
58+
expectAssignable<Promise<{ item: string; stored: number; ttl: number; } | null>>(
59+
fastify.cache.get<string>('well-known')
60+
);
61+
expectType<void>(
62+
fastify.cache.get<string>('well-known', (err, value) => {
63+
expectType<unknown>(err);
64+
expectAssignable<{ item: string; stored: number; ttl: number; } | null>(value);
65+
})
66+
);
67+
68+
return { message: 'two' };
69+
});

0 commit comments

Comments
 (0)