Skip to content

Commit

Permalink
fix: 🐛 allow typed server methods access cache methods
Browse files Browse the repository at this point in the history
also fix pre typing issue
  • Loading branch information
damusix committed Jul 2, 2024
1 parent 3020a36 commit 4a4188b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/types/route.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ export interface RouteOptionsPreObject<Refs extends ReqRef = ReqRefDefaults> {
/**
* a lifecycle method.
*/
method: Lifecycle.Method<Refs>;
method: Lifecycle.Method<any>;
/**
* key name used to assign the response of the method to in request.pre and request.preResponses.
*/
Expand Down
10 changes: 9 additions & 1 deletion lib/types/server/server.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`).
Expand Down Expand Up @@ -203,7 +204,14 @@ export class Server<A = ServerApplicationState> {
* 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<ServerMethods[K]>): Promise<void>;
stats: CacheStatisticsObject
}
}
};

/**
* Provides access to the server MIME database used for setting content-type information. The object must not be
Expand Down
39 changes: 36 additions & 3 deletions test/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
Server,
ServerRoute,
server as createServer,
ServerRegisterPluginObject
ServerRegisterPluginObject,
Lifecycle
} from '../..';

const { expect: check } = lab;
Expand All @@ -34,11 +35,20 @@ interface RequestDecorations {
},
RouteApp: {
prefix: string[];
},
Pres: {
some: string,
another: number
}
}

type AppRequest = Request<RequestDecorations>;

const getNum: Lifecycle.Method = (req) => {

return 10;
}

const route: ServerRoute<RequestDecorations> = {
method: 'POST',
path: '/',
Expand All @@ -51,7 +61,20 @@ const route: ServerRoute<RequestDecorations> = {
maxBytes: 1024 * 1024,
output: 'stream',
multipart: true
}
},
pre: [
{
assign: 'some',
method: ((req) => {

return req.app.word;
}) as Lifecycle.Method<RequestDecorations>
},
{
assign: 'another',
method: getNum
}
]
},
handler: (request: AppRequest, h: ResponseToolkit) => {

Expand Down Expand Up @@ -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: {
Expand All @@ -123,4 +152,8 @@ server.method('test.add', (a: number, b: number) => a + b, {
segment: 'test-segment',
},
generateKey: (a: number, b: number) => `${a}${b}`
});
});



server.methods['test.add'].cache?.drop(1, 2);

0 comments on commit 4a4188b

Please sign in to comment.