Skip to content

Commit

Permalink
fix: 🐛 reflect reality of Hapi methods in types
Browse files Browse the repository at this point in the history
  • Loading branch information
damusix authored and Danilo Alonso committed Jul 29, 2024
1 parent 4a4188b commit e63d51e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 17 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<any>;
method: Lifecycle.Method<Refs>;
/**
* key name used to assign the response of the method to in request.pre and request.preResponses.
*/
Expand Down
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 {
}
9 changes: 1 addition & 8 deletions lib/types/server/server.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +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: {
[K in keyof ServerMethods]: ServerMethods[K] & {
cache?: {
drop(...args: Parameters<ServerMethods[K]>): Promise<void>;
stats: CacheStatisticsObject
}
}
};
readonly methods: ServerMethods

/**
* Provides access to the server MIME database used for setting content-type information. The object must not be
Expand Down
11 changes: 6 additions & 5 deletions 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 @@ -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)>;
}
}
}

Expand All @@ -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);
server.methods.test.add.cache?.drop(1, 2);

0 comments on commit e63d51e

Please sign in to comment.