diff --git a/lib/types/server/methods.d.ts b/lib/types/server/methods.d.ts index dd482f988..a4cb02b94 100644 --- a/lib/types/server/methods.d.ts +++ b/lib/types/server/methods.d.ts @@ -1,4 +1,13 @@ -import { PolicyOptions } from "@hapi/catbox"; +import { CacheStatisticsObject, PolicyOptions } from "@hapi/catbox"; + +type AnyMethod = (...args: any[]) => any; + +export type CachedServerMethod = T & { + cache?: { + drop(...args: Parameters): Promise; + stats: CacheStatisticsObject + } +}; /** * The method function with a signature async function(...args, [flags]) where: @@ -7,7 +16,7 @@ import { PolicyOptions } from "@hapi/catbox"; * * * ttl - 0 if result is valid but cannot be cached. Defaults to cache policy. * For reference [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-servermethodname-method-options) */ -export type ServerMethod = (...args: any[]) => any; +export type ServerMethod = AnyMethod; /** * The same cache configuration used in server.cache(). @@ -71,8 +80,16 @@ export interface ServerMethodConfigurationObject { options?: ServerMethodOptions | undefined; } +interface BaseServerMethods { + [name: string]: ( + ServerMethod | + CachedServerMethod | + BaseServerMethods + ); +} + /** * An empty interface to allow typings of custom server.methods. */ -export interface ServerMethods extends Record { +export interface ServerMethods extends BaseServerMethods { } diff --git a/test/types/index.ts b/test/types/index.ts index 4a438b38d..35fd0a49e 100644 --- a/test/types/index.ts +++ b/test/types/index.ts @@ -11,7 +11,8 @@ import { ServerRoute, server as createServer, ServerRegisterPluginObject, - Lifecycle + Lifecycle, + CachedServerMethod } from '../..'; const { expect: check } = lab; @@ -115,6 +116,14 @@ server.cache.provision({ } }) +declare module '../..' { + interface ServerMethods { + test: { + add: CachedServerMethod<((a: number, b: number) => number)>; + } + } +} + server.method('test.add', (a: number, b: number) => a + b, { bind: server, cache: { @@ -126,6 +135,9 @@ server.method('test.add', (a: number, b: number) => a + b, { generateKey: (a: number, b: number) => `${a}${b}` }); + +server.methods.test.add.cache?.drop(1, 2); + declare module '../..' { interface Request { obj1: {