Skip to content

Commit

Permalink
fix: 🐛 loosen pre typings to allow for better programability
Browse files Browse the repository at this point in the history
  • Loading branch information
Danilo Alonso authored and Danilo Alonso committed Mar 15, 2024
1 parent 5ded9f7 commit 56204bd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
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
12 changes: 10 additions & 2 deletions test/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,24 @@ const typedHandler: Lifecycle.Method<{ Payload: { q: string; p: string } }> = (r
return new URLSearchParams(request.payload).toString();
};

const typedPre: Lifecycle.Method<{ Payload: { q: string } }> = (request, h) => {
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]
pre: [
typedPre,
untypePre
]
}
} satisfies ServerRoute;

0 comments on commit 56204bd

Please sign in to comment.