From d60f5dce53250673c5e609b2fa9cb061a5bff76f Mon Sep 17 00:00:00 2001 From: Danilo Alonso Date: Tue, 2 Jul 2024 00:15:02 -0400 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20allow=20typed=20server=20?= =?UTF-8?q?methods=20access=20cache=20methods?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit also fix pre typing issue fix: 🐛 reflect reality of Hapi methods in types --- lib/types/server/methods.d.ts | 23 ++++++++++++++++++++--- lib/types/server/server.d.ts | 3 ++- test/types/index.ts | 15 +++++++++++++-- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/lib/types/server/methods.d.ts b/lib/types/server/methods.d.ts index dd482f988..39ff50f79 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/lib/types/server/server.d.ts b/lib/types/server/server.d.ts index 0970b7c17..5bfe21c6b 100644 --- a/lib/types/server/server.d.ts +++ b/lib/types/server/server.d.ts @@ -52,6 +52,7 @@ import { } from './methods'; import { ServerOptions } from './options'; import { ServerState, ServerStateCookieOptions } from './state'; +import { CacheStatisticsObject } from '@hapi/catbox'; /** * User-extensible type for application specific state (`server.app`). @@ -203,7 +204,7 @@ export class Server { * server method name is an object property. * [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-servermethods */ - readonly methods: ServerMethods; + readonly methods: ServerMethods /** * Provides access to the server MIME database used for setting content-type information. The object must not be diff --git a/test/types/index.ts b/test/types/index.ts index 988edffb0..e879b50e0 100644 --- a/test/types/index.ts +++ b/test/types/index.ts @@ -10,7 +10,8 @@ import { Server, ServerRoute, server as createServer, - ServerRegisterPluginObject + Lifecycle, + CachedServerMethod } from '../..'; const { expect: check } = lab; @@ -114,6 +115,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: { @@ -123,4 +132,6 @@ server.method('test.add', (a: number, b: number) => a + b, { segment: 'test-segment', }, generateKey: (a: number, b: number) => `${a}${b}` -}); \ No newline at end of file +}); + +server.methods.test.add.cache?.drop(1, 2); \ No newline at end of file