From 33d1ef644a9d14dfa43f9b1ddb1df6350e8f306a Mon Sep 17 00:00:00 2001 From: Kevin Whitley Date: Tue, 2 Apr 2024 10:29:47 -0500 Subject: [PATCH] expanded middleware example to include second middleware --- examples/types/middleware.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/examples/types/middleware.ts b/examples/types/middleware.ts index 2ad26b4..63538cb 100644 --- a/examples/types/middleware.ts +++ b/examples/types/middleware.ts @@ -5,6 +5,10 @@ type UserRequest = { user: string } & IRequestStrict +type PetRequest = { + pet: string +} & IRequestStrict + // we define a *strict* router for this demo const router = IttyRouter() @@ -13,6 +17,11 @@ const withUser: RequestHandler = (request) => { request.user = 'Kevin' } +// middleware with explicit request-type (generic) +const withPet = (request: PetRequest) => { + request.pet = 'Halsey' +} + router // request will be IRequestStrict here, thus no user property .get('/', (request) => { @@ -20,8 +29,9 @@ router }) // then we add the middleware defined above, allowing the handler chain to inherit the request type - .all('*', withUser, (request) => { - request.user = 'Kevin' + .all('*', withUser, withPet, (request) => { + request.user = 'Kevin' // should work + request.pet = 'Halsey' // should work }) // request will be back to IRequestStrict here, thus no user property