Skip to content
Merged
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 {
}
14 changes: 13 additions & 1 deletion test/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
ServerRoute,
server as createServer,
ServerRegisterPluginObject,
Lifecycle
Lifecycle,
CachedServerMethod
} from '../..';

const { expect: check } = lab;
Expand Down Expand Up @@ -115,6 +116,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 @@ -126,6 +135,9 @@ 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);

declare module '../..' {
interface Request {
obj1: {
Expand Down