Skip to content

Commit 45c7255

Browse files
damusixMarsup
andauthored
fix: πŸ› allow typed server methods access cache methods (#4515)
* fix: πŸ› allow typed server methods access cache methods also fix pre typing issue fix: πŸ› reflect reality of Hapi methods in types * Apply suggestions from code review --------- Co-authored-by: Nicolas Morel <[email protected]>
1 parent 30d043e commit 45c7255

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

β€Žlib/types/server/methods.d.tsβ€Ž

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
import { PolicyOptions } from "@hapi/catbox";
1+
import { CacheStatisticsObject, PolicyOptions } from "@hapi/catbox";
2+
3+
type AnyMethod = (...args: any[]) => any;
4+
5+
export type CachedServerMethod<T extends AnyMethod> = T & {
6+
cache?: {
7+
drop(...args: Parameters<T>): Promise<void>;
8+
stats: CacheStatisticsObject
9+
}
10+
};
211

312
/**
413
* The method function with a signature async function(...args, [flags]) where:
@@ -7,7 +16,7 @@ import { PolicyOptions } from "@hapi/catbox";
716
* * * ttl - 0 if result is valid but cannot be cached. Defaults to cache policy.
817
* For reference [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-servermethodname-method-options)
918
*/
10-
export type ServerMethod = (...args: any[]) => any;
19+
export type ServerMethod = AnyMethod;
1120

1221
/**
1322
* The same cache configuration used in server.cache().
@@ -71,8 +80,16 @@ export interface ServerMethodConfigurationObject {
7180
options?: ServerMethodOptions | undefined;
7281
}
7382

83+
interface BaseServerMethods {
84+
[name: string]: (
85+
ServerMethod |
86+
CachedServerMethod<ServerMethod> |
87+
BaseServerMethods
88+
);
89+
}
90+
7491
/**
7592
* An empty interface to allow typings of custom server.methods.
7693
*/
77-
export interface ServerMethods extends Record<string, ServerMethod> {
94+
export interface ServerMethods extends BaseServerMethods {
7895
}

β€Žtest/types/index.tsβ€Ž

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import {
1111
ServerRoute,
1212
server as createServer,
1313
ServerRegisterPluginObject,
14-
Lifecycle
14+
Lifecycle,
15+
CachedServerMethod
1516
} from '../..';
1617

1718
const { expect: check } = lab;
@@ -115,6 +116,14 @@ server.cache.provision({
115116
}
116117
})
117118

119+
declare module '../..' {
120+
interface ServerMethods {
121+
test: {
122+
add: CachedServerMethod<((a: number, b: number) => number)>;
123+
}
124+
}
125+
}
126+
118127
server.method('test.add', (a: number, b: number) => a + b, {
119128
bind: server,
120129
cache: {
@@ -126,6 +135,9 @@ server.method('test.add', (a: number, b: number) => a + b, {
126135
generateKey: (a: number, b: number) => `${a}${b}`
127136
});
128137

138+
139+
server.methods.test.add.cache?.drop(1, 2);
140+
129141
declare module '../..' {
130142
interface Request {
131143
obj1: {

0 commit comments

Comments
Β (0)