@@ -82,48 +82,137 @@ expectType<RouteError>(routeError);
8282const methodError = new MethodError ( 'Method not allowed' , 'POST' , '/users' ) ;
8383expectType < MethodError > ( methodError ) ;
8484
85- const getHandler : HandlerFunction < UserResponse > = ( req , res ) => {
86- res . json ( { id :
'1' , name :
'John' , email :
'[email protected] ' } ) ; 87- } ;
88- api . METHOD < UserResponse > ( 'GET' , '/users' , getHandler ) ;
89-
90- const multiHandler : HandlerFunction < UserResponse > = ( req , res ) => {
91- res . json ( { id :
'1' , name :
'John' , email :
'[email protected] ' } ) ; 85+ const authMiddleware : Middleware < UserResponse > = ( req , res , next ) => {
86+ expectType < Response < UserResponse > > ( res ) ;
87+ next ( ) ;
9288} ;
93- api . METHOD < UserResponse > ( [ 'GET' , 'POST' ] , '/users' , multiHandler ) ;
9489
95- const getUserHandler : HandlerFunction < UserResponse > = ( req , res ) => {
96- expectType < Request < APIGatewayContext > > ( req ) ;
97- expectType < APIGatewayContext > ( req . requestContext ) ;
98- res . json ( { id :
'1' , name :
'John' , email :
'[email protected] ' } ) ; 90+ const validationMiddleware : Middleware < UserResponse > = ( req , res , next ) => {
91+ expectType < Response < UserResponse > > ( res ) ;
92+ next ( ) ;
9993} ;
100- api . get < UserResponse > ( '/users' , getUserHandler ) ;
10194
102- const postUserHandler : HandlerFunction <
95+ const albAuthMiddleware : Middleware <
10396 UserResponse ,
10497 ALBContext ,
10598 UserQuery ,
10699 UserParams ,
107100 UserBody
108- > = ( req , res ) => {
101+ > = ( req , res , next ) => {
109102 expectType < ALBContext > ( req . requestContext ) ;
110- expectType < UserQuery > ( req . query ) ;
111- expectType < UserParams > ( req . params ) ;
112- expectType < UserBody > ( req . body ) ;
113- res . json ( { id : '1' , name : req . body . name , email : req . body . email } ) ;
103+ expectType < Response < UserResponse > > ( res ) ;
104+ next ( ) ;
114105} ;
106+
107+ const handler : HandlerFunction < UserResponse > = ( req , res ) => {
108+ res . json ( { id :
'1' , name :
'John' , email :
'[email protected] ' } ) ; 109+ } ;
110+
111+ api . get < UserResponse > (
112+ '/users' ,
113+ authMiddleware ,
114+ validationMiddleware ,
115+ ( req , res ) => {
116+ expectType < Request < APIGatewayContext > > ( req ) ;
117+ res . json ( { id :
'1' , name :
'John' , email :
'[email protected] ' } ) ; 118+ }
119+ ) ;
120+
121+ api . post < UserResponse > (
122+ '/users' ,
123+ authMiddleware ,
124+ validationMiddleware ,
125+ ( req , res ) => {
126+ expectType < Request < APIGatewayContext > > ( req ) ;
127+ res . json ( { id :
'1' , name :
'John' , email :
'[email protected] ' } ) ; 128+ }
129+ ) ;
130+
131+ api . put < UserResponse > (
132+ '/users/:id' ,
133+ authMiddleware ,
134+ validationMiddleware ,
135+ ( req , res ) => {
136+ expectType < Request < APIGatewayContext > > ( req ) ;
137+ res . json ( { id :
'1' , name :
'John' , email :
'[email protected] ' } ) ; 138+ }
139+ ) ;
140+
141+ api . patch < UserResponse > (
142+ '/users/:id' ,
143+ authMiddleware ,
144+ validationMiddleware ,
145+ ( req , res ) => {
146+ expectType < Request < APIGatewayContext > > ( req ) ;
147+ res . json ( { id :
'1' , name :
'John' , email :
'[email protected] ' } ) ; 148+ }
149+ ) ;
150+
151+ api . delete < UserResponse > (
152+ '/users/:id' ,
153+ authMiddleware ,
154+ validationMiddleware ,
155+ ( req , res ) => {
156+ expectType < Request < APIGatewayContext > > ( req ) ;
157+ res . json ( { id :
'1' , name :
'John' , email :
'[email protected] ' } ) ; 158+ }
159+ ) ;
160+
161+ api . options < UserResponse > (
162+ '/users' ,
163+ authMiddleware ,
164+ validationMiddleware ,
165+ ( req , res ) => {
166+ expectType < Request < APIGatewayContext > > ( req ) ;
167+ res . json ( { id :
'1' , name :
'John' , email :
'[email protected] ' } ) ; 168+ }
169+ ) ;
170+
171+ api . head < UserResponse > (
172+ '/users' ,
173+ authMiddleware ,
174+ validationMiddleware ,
175+ ( req , res ) => {
176+ expectType < Request < APIGatewayContext > > ( req ) ;
177+ res . json ( { id :
'1' , name :
'John' , email :
'[email protected] ' } ) ; 178+ }
179+ ) ;
180+
181+ api . any < UserResponse > (
182+ '/users' ,
183+ authMiddleware ,
184+ validationMiddleware ,
185+ ( req , res ) => {
186+ expectType < Request < APIGatewayContext > > ( req ) ;
187+ res . json ( { id :
'1' , name :
'John' , email :
'[email protected] ' } ) ; 188+ }
189+ ) ;
190+
115191api . post < UserResponse , ALBContext , UserQuery , UserParams , UserBody > (
116192 '/users' ,
117- postUserHandler
193+ albAuthMiddleware ,
194+ ( req , res ) => {
195+ expectType < ALBContext > ( req . requestContext ) ;
196+ expectType < UserQuery > ( req . query ) ;
197+ expectType < UserParams > ( req . params ) ;
198+ expectType < UserBody > ( req . body ) ;
199+ res . json ( { id : '1' , name : req . body . name , email : req . body . email } ) ;
200+ }
118201) ;
119202
120- const userMiddleware : Middleware < UserResponse > = ( req , res , next ) => {
121- expectType < Response < UserResponse > > ( res ) ;
122- next ( ) ;
123- } ;
124- api . use < UserResponse > ( userMiddleware ) ;
203+ api . METHOD < UserResponse > (
204+ [ 'GET' , 'POST' ] ,
205+ '/users' ,
206+ authMiddleware ,
207+ validationMiddleware ,
208+ handler
209+ ) ;
210+
211+ // Test middleware without path
212+ api . use < UserResponse > ( authMiddleware ) ;
125213
126- const albMiddleware : Middleware < UserResponse , ALBContext > = (
214+ // Test middleware with path and ALB context
215+ const albRouteMiddleware : Middleware < UserResponse , ALBContext > = (
127216 req ,
128217 res ,
129218 next
@@ -132,13 +221,12 @@ const albMiddleware: Middleware<UserResponse, ALBContext> = (
132221 expectType < Response < UserResponse > > ( res ) ;
133222 next ( ) ;
134223} ;
135- api . use < UserResponse , ALBContext > ( '/users' , albMiddleware ) ;
224+ api . use < UserResponse , ALBContext > ( '/users' , albRouteMiddleware ) ;
136225
137- const finallyHandler : HandlerFunction = ( req , res ) => {
226+ api . finally ( ( req , res ) => {
138227 expectType < Request > ( req ) ;
139228 expectType < Response > ( res ) ;
140- } ;
141- api . finally ( finallyHandler ) ;
229+ } ) ;
142230
143231const result = api . run < UserResponse > ( { } as APIGatewayProxyEvent , { } as Context ) ;
144232expectType < Promise < UserResponse > > ( result ) ;
0 commit comments