Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,28 @@ type FastifyCaching = FastifyPluginCallback<fastifyCaching.FastifyCachingPluginO
privacy: fastifyCaching.Privacy;
};

type CacheResult<T> = {
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<T = unknown>(
key: string | { id: string; segment: string },
callback?: (error: unknown, result: unknown) => void
callback: (error: unknown, result: CacheResult<T>) => void
): void;
/**
* If AbstractCache is using useAwait = true, then this method-header must be used.
* @param key
*/
get<T = unknown>(
key: string | { id: string; segment: string },
): Promise<CacheResult<T>>;
set(
key: string | { id: string; segment: string },
value: unknown,
Expand Down
17 changes: 17 additions & 0 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,20 @@ const badCachingOptions = {
};

expectError(shouldErrorApp.register(fastifyCaching, badCachingOptions));

fastify.get('/three', async (request, reply) => {
expectAssignable<Promise<unknown>>(
fastify.cache.get('well-known')
);
expectAssignable<Promise<{ item: string; stored: number; ttl: number; } | null>>(
fastify.cache.get<string>('well-known')
);
expectType<void>(
fastify.cache.get<string>('well-known', (err, value) => {
expectType<unknown>(err);
expectAssignable<{ item: string; stored: number; ttl: number; } | null>(value);
})
);

return { message: 'two' };
});