11import { setAdditionalStyles } from "./context.ts" ;
22import { HttpError } from "./error.ts" ;
33import { isHandlerByMethod , type PageResponse } from "./handlers.ts" ;
4- import type { MaybeLazyMiddleware , Middleware } from "./middlewares/mod.ts" ;
4+ import {
5+ compileMiddlewares ,
6+ type MaybeLazyMiddleware ,
7+ type Middleware ,
8+ } from "./middlewares/mod.ts" ;
59import { mergePath , type Method , type Router , toRoutePath } from "./router.ts" ;
610import {
711 getOrCreateSegment ,
@@ -202,22 +206,25 @@ export type Command<State> =
202206 | FsRouteCommand < State > ;
203207
204208export function applyCommands < State > (
205- router : Router < MaybeLazyMiddleware < State > > ,
209+ router : Router < Middleware < State > > ,
206210 commands : Command < State > [ ] ,
207211 basePath : string ,
208- ) : { rootMiddlewares : MaybeLazyMiddleware < State > [ ] } {
212+ onError ?: ( err : unknown ) => void ,
213+ ) : { rootHandler : Middleware < State > } {
209214 const root = newSegment < State > ( "" , null ) ;
210215
211- applyCommandsInner ( root , router , commands , basePath ) ;
216+ applyCommandsInner ( root , router , commands , basePath , onError ) ;
212217
213- return { rootMiddlewares : segmentToMiddlewares ( root ) } ;
218+ const rootMiddlewares = segmentToMiddlewares ( root ) ;
219+ return { rootHandler : compileMiddlewares ( rootMiddlewares , onError ) } ;
214220}
215221
216222function applyCommandsInner < State > (
217223 root : Segment < State > ,
218- router : Router < MaybeLazyMiddleware < State > > ,
224+ router : Router < Middleware < State > > ,
219225 commands : Command < State > [ ] ,
220226 basePath : string ,
227+ onError ?: ( err : unknown ) => void ,
221228) {
222229 for ( let i = 0 ; i < commands . length ; i ++ ) {
223230 const cmd = commands [ i ] ;
@@ -290,18 +297,19 @@ function applyCommandsInner<State>(
290297 return renderRoute ( ctx , def ) ;
291298 } ) ;
292299
300+ const compiled = compileMiddlewares ( fns , onError ) ;
293301 if ( config === undefined || config . methods === "ALL" ) {
294- router . add ( "GET" , routePath , fns ) ;
295- router . add ( "DELETE" , routePath , fns ) ;
296- router . add ( "HEAD" , routePath , fns ) ;
297- router . add ( "OPTIONS" , routePath , fns ) ;
298- router . add ( "PATCH" , routePath , fns ) ;
299- router . add ( "POST" , routePath , fns ) ;
300- router . add ( "PUT" , routePath , fns ) ;
302+ router . add ( "GET" , routePath , compiled ) ;
303+ router . add ( "DELETE" , routePath , compiled ) ;
304+ router . add ( "HEAD" , routePath , compiled ) ;
305+ router . add ( "OPTIONS" , routePath , compiled ) ;
306+ router . add ( "PATCH" , routePath , compiled ) ;
307+ router . add ( "POST" , routePath , compiled ) ;
308+ router . add ( "PUT" , routePath , compiled ) ;
301309 } else if ( Array . isArray ( config . methods ) ) {
302310 for ( let i = 0 ; i < config . methods . length ; i ++ ) {
303311 const method = config . methods [ i ] ;
304- router . add ( method , routePath , fns ) ;
312+ router . add ( method , routePath , compiled ) ;
305313 }
306314 }
307315 } else {
@@ -313,17 +321,18 @@ function applyCommandsInner<State>(
313321 false ,
314322 ) ) ;
315323
324+ const compiled = compileMiddlewares ( fns , onError ) ;
316325 if ( typeof route . handler === "function" ) {
317- router . add ( "GET" , routePath , fns ) ;
318- router . add ( "DELETE" , routePath , fns ) ;
319- router . add ( "HEAD" , routePath , fns ) ;
320- router . add ( "OPTIONS" , routePath , fns ) ;
321- router . add ( "PATCH" , routePath , fns ) ;
322- router . add ( "POST" , routePath , fns ) ;
323- router . add ( "PUT" , routePath , fns ) ;
326+ router . add ( "GET" , routePath , compiled ) ;
327+ router . add ( "DELETE" , routePath , compiled ) ;
328+ router . add ( "HEAD" , routePath , compiled ) ;
329+ router . add ( "OPTIONS" , routePath , compiled ) ;
330+ router . add ( "PATCH" , routePath , compiled ) ;
331+ router . add ( "POST" , routePath , compiled ) ;
332+ router . add ( "PUT" , routePath , compiled ) ;
324333 } else if ( isHandlerByMethod ( route . handler ! ) ) {
325334 for ( const method of Object . keys ( route . handler ) ) {
326- router . add ( method as Method , routePath , fns ) ;
335+ router . add ( method as Method , routePath , compiled ) ;
327336 }
328337 }
329338 }
@@ -340,25 +349,26 @@ function applyCommandsInner<State>(
340349
341350 result . push ( ...fns ) ;
342351
352+ const compiled = compileMiddlewares ( result , onError ) ;
343353 const resPath = toRoutePath ( mergePath ( basePath , pattern , false ) ) ;
344354 if ( method === "ALL" ) {
345- router . add ( "GET" , resPath , result ) ;
346- router . add ( "DELETE" , resPath , result ) ;
347- router . add ( "HEAD" , resPath , result ) ;
348- router . add ( "OPTIONS" , resPath , result ) ;
349- router . add ( "PATCH" , resPath , result ) ;
350- router . add ( "POST" , resPath , result ) ;
351- router . add ( "PUT" , resPath , result ) ;
355+ router . add ( "GET" , resPath , compiled ) ;
356+ router . add ( "DELETE" , resPath , compiled ) ;
357+ router . add ( "HEAD" , resPath , compiled ) ;
358+ router . add ( "OPTIONS" , resPath , compiled ) ;
359+ router . add ( "PATCH" , resPath , compiled ) ;
360+ router . add ( "POST" , resPath , compiled ) ;
361+ router . add ( "PUT" , resPath , compiled ) ;
352362 } else {
353- router . add ( method , resPath , result ) ;
363+ router . add ( method , resPath , compiled ) ;
354364 }
355365
356366 break ;
357367 }
358368 case CommandType . FsRoute : {
359369 const items = cmd . getItems ( ) ;
360370 const base = mergePath ( basePath , cmd . pattern , true ) ;
361- applyCommandsInner ( root , router , items , base ) ;
371+ applyCommandsInner ( root , router , items , base , onError ) ;
362372 break ;
363373 }
364374 default :
0 commit comments