Skip to content
Closed
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
6 changes: 3 additions & 3 deletions lib/types/route.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,12 +339,12 @@ export interface RouteOptionsPayload {
/**
* For context [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-routeoptionspre)
*/
export type RouteOptionsPreArray<Refs extends ReqRef = ReqRefDefaults> = RouteOptionsPreAllOptions<Refs>[];
export type RouteOptionsPreArray = RouteOptionsPreAllOptions[];

/**
* For context [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-routeoptionspre)
*/
export type RouteOptionsPreAllOptions<Refs extends ReqRef = ReqRefDefaults> = RouteOptionsPreObject<Refs> | RouteOptionsPreObject<Refs>[] | Lifecycle.Method<Refs>;
export type RouteOptionsPreAllOptions = RouteOptionsPreObject<any> | RouteOptionsPreObject<any>[] | Lifecycle.Method<any>;

/**
* An object with:
Expand Down Expand Up @@ -817,7 +817,7 @@ export interface CommonRouteProperties<Refs extends ReqRef = ReqRefDefaults> {
* lifecycle methods.
* [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-routeoptionspre)
*/
pre?: RouteOptionsPreArray<Refs> | undefined;
pre?: RouteOptionsPreArray | undefined;

/**
* Processing rules for the outgoing response.
Expand Down
31 changes: 30 additions & 1 deletion test/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { types as lab } from '@hapi/lab';
import { expect } from '@hapi/code';

import {
Lifecycle,
Plugin,
Request,
ResponseToolkit,
Expand Down Expand Up @@ -77,7 +78,7 @@ const plugin: Plugin<TestPluginOptions, TestPluginDecorations> = {
register: function (srv: MyServer, options) {

check.type<TestPluginOptions>(options);

srv.expose({
add: function (a: number, b: number) {

Expand All @@ -92,3 +93,31 @@ const loadedServer = await server.register({ plugin, options: { x: 10 } });
const sum = loadedServer.plugins.test.add(1, 2);
expect(sum).to.equal(130);
check.type<number>(sum);

const typedHandler: Lifecycle.Method<{ Payload: { q: string; p: string } }> = (request, h) => {

check.type<{ q: string; p: string }>(request.payload);
return new URLSearchParams(request.payload).toString();
};

const typedPre: Lifecycle.Method<{ Payload: { q: string; } }> = (request, h) => {

return h.continue;
};

const untypePre: Lifecycle.Method = (request, h) => {

return h.continue;
}

const typedRoute = {
method: 'POST',
path: '/',
options: {
handler: typedHandler,
pre: [
typedPre,
untypePre
]
}
} satisfies ServerRoute;