From 4a4188b23c56d1acd82127ec39b6e2f18abed025 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 --- lib/types/route.d.ts | 2 +- lib/types/server/server.d.ts | 10 ++++++++- test/types/index.ts | 39 +++++++++++++++++++++++++++++++++--- 3 files changed, 46 insertions(+), 5 deletions(-) diff --git a/lib/types/route.d.ts b/lib/types/route.d.ts index be2416850..0c7bbfbcd 100644 --- a/lib/types/route.d.ts +++ b/lib/types/route.d.ts @@ -354,7 +354,7 @@ export interface RouteOptionsPreObject { /** * a lifecycle method. */ - method: Lifecycle.Method; + method: Lifecycle.Method; /** * key name used to assign the response of the method to in request.pre and request.preResponses. */ diff --git a/lib/types/server/server.d.ts b/lib/types/server/server.d.ts index 0970b7c17..4e710bada 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,14 @@ 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: { + [K in keyof ServerMethods]: ServerMethods[K] & { + cache?: { + drop(...args: Parameters): Promise; + stats: CacheStatisticsObject + } + } + }; /** * 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..bf083db6f 100644 --- a/test/types/index.ts +++ b/test/types/index.ts @@ -10,7 +10,8 @@ import { Server, ServerRoute, server as createServer, - ServerRegisterPluginObject + ServerRegisterPluginObject, + Lifecycle } from '../..'; const { expect: check } = lab; @@ -34,11 +35,20 @@ interface RequestDecorations { }, RouteApp: { prefix: string[]; + }, + Pres: { + some: string, + another: number } } type AppRequest = Request; +const getNum: Lifecycle.Method = (req) => { + + return 10; +} + const route: ServerRoute = { method: 'POST', path: '/', @@ -51,7 +61,20 @@ const route: ServerRoute = { maxBytes: 1024 * 1024, output: 'stream', multipart: true - } + }, + pre: [ + { + assign: 'some', + method: ((req) => { + + return req.app.word; + }) as Lifecycle.Method + }, + { + assign: 'another', + method: getNum + } + ] }, handler: (request: AppRequest, h: ResponseToolkit) => { @@ -114,6 +137,12 @@ server.cache.provision({ } }) +declare module '../..' { + interface ServerMethods { + 'test.add'(a: number, b: number): number; + } +} + server.method('test.add', (a: number, b: number) => a + b, { bind: server, cache: { @@ -123,4 +152,8 @@ 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