Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 🐛 allow typed server methods access cache methods #4515

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions lib/types/server/methods.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { PolicyOptions } from "@hapi/catbox";
import { CacheStatisticsObject, PolicyOptions } from "@hapi/catbox";

type AnyMethod = (...args: any[]) => any;

export type CachedServerMethod<T extends AnyMethod> = T & {
cache?: {
drop(...args: Parameters<T>): Promise<void>;
stats: CacheStatisticsObject
}
}

/**
* The method function with a signature async function(...args, [flags]) where:
Expand All @@ -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().
Expand Down Expand Up @@ -71,8 +80,16 @@ export interface ServerMethodConfigurationObject {
options?: ServerMethodOptions | undefined;
}

interface BaseServerMethods {
[name: string]: (
ServerMethod |
CachedServerMethod<ServerMethod> |
BaseServerMethods
);
}

/**
* An empty interface to allow typings of custom server.methods.
*/
export interface ServerMethods extends Record<string, ServerMethod> {
export interface ServerMethods extends BaseServerMethods {
}
3 changes: 2 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,7 @@ 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: ServerMethods

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

const { expect: check } = lab;
Expand All @@ -34,11 +36,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 +62,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 +138,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: {
Expand All @@ -123,4 +155,6 @@ 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);
Loading