From 4a4188b23c56d1acd82127ec39b6e2f18abed025 Mon Sep 17 00:00:00 2001 From: Danilo Alonso Date: Tue, 2 Jul 2024 00:15:02 -0400 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20=F0=9F=90=9B=20allow=20typed=20serve?= =?UTF-8?q?r=20methods=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 From e63d51eff81478815cbba406cc2f79645de3188a Mon Sep 17 00:00:00 2001 From: Danilo Alonso Date: Tue, 2 Jul 2024 10:27:54 -0400 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20=F0=9F=90=9B=20reflect=20reality=20o?= =?UTF-8?q?f=20Hapi=20methods=20in=20types?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/types/route.d.ts | 2 +- lib/types/server/methods.d.ts | 23 ++++++++++++++++++++--- lib/types/server/server.d.ts | 9 +-------- test/types/index.ts | 11 ++++++----- 4 files changed, 28 insertions(+), 17 deletions(-) diff --git a/lib/types/route.d.ts b/lib/types/route.d.ts index 0c7bbfbcd..be2416850 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/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 4e710bada..5bfe21c6b 100644 --- a/lib/types/server/server.d.ts +++ b/lib/types/server/server.d.ts @@ -204,14 +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: { - [K in keyof ServerMethods]: ServerMethods[K] & { - cache?: { - drop(...args: Parameters): Promise; - stats: CacheStatisticsObject - } - } - }; + 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 bf083db6f..b7bc8ed24 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; @@ -139,7 +140,9 @@ server.cache.provision({ declare module '../..' { interface ServerMethods { - 'test.add'(a: number, b: number): number; + test: { + add: CachedServerMethod<((a: number, b: number) => number)>; + } } } @@ -154,6 +157,4 @@ 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); \ No newline at end of file +server.methods.test.add.cache?.drop(1, 2); \ No newline at end of file