@@ -42,7 +42,7 @@ abstract class BaseRouter {
42
42
43
43
protected readonly routeRegistry : RouteHandlerRegistry ;
44
44
protected readonly errorHandlerRegistry : ErrorHandlerRegistry ;
45
- protected readonly middlwares : Middleware [ ] = [ ] ;
45
+ protected readonly middleware : Middleware [ ] = [ ] ;
46
46
47
47
/**
48
48
* A logger instance to be used for logging debug, warning, and error messages.
@@ -170,7 +170,7 @@ abstract class BaseRouter {
170
170
* ```
171
171
*/
172
172
public use ( middleware : Middleware ) : void {
173
- this . middlwares . push ( middleware ) ;
173
+ this . middleware . push ( middleware ) ;
174
174
}
175
175
176
176
/**
@@ -223,7 +223,10 @@ abstract class BaseRouter {
223
223
? route . handler . bind ( options . scope )
224
224
: route . handler ;
225
225
226
- const middleware = composeMiddleware ( [ ...this . middlwares ] ) ;
226
+ const middleware = composeMiddleware ( [
227
+ ...this . middleware ,
228
+ ...route . middleware ,
229
+ ] ) ;
227
230
228
231
const result = await middleware (
229
232
route . params ,
@@ -255,11 +258,11 @@ abstract class BaseRouter {
255
258
}
256
259
257
260
public route ( handler : RouteHandler , options : RouteOptions ) : void {
258
- const { method, path } = options ;
261
+ const { method, path, middleware = [ ] } = options ;
259
262
const methods = Array . isArray ( method ) ? method : [ method ] ;
260
263
261
264
for ( const method of methods ) {
262
- this . routeRegistry . register ( new Route ( method , path , handler ) ) ;
265
+ this . routeRegistry . register ( new Route ( method , path , handler , middleware ) ) ;
263
266
}
264
267
}
265
268
@@ -333,10 +336,26 @@ abstract class BaseRouter {
333
336
#handleHttpMethod(
334
337
method : HttpMethod ,
335
338
path : Path ,
339
+ middlewareOrHandler ?: Middleware [ ] | RouteHandler ,
336
340
handler ?: RouteHandler
337
341
) : MethodDecorator | undefined {
338
- if ( handler && typeof handler === 'function' ) {
339
- this . route ( handler , { method, path } ) ;
342
+ if ( Array . isArray ( middlewareOrHandler ) ) {
343
+ if ( handler && typeof handler === 'function' ) {
344
+ this . route ( handler , { method, path, middleware : middlewareOrHandler } ) ;
345
+ return ;
346
+ }
347
+ return ( _target , _propertyKey , descriptor : PropertyDescriptor ) => {
348
+ this . route ( descriptor . value , {
349
+ method,
350
+ path,
351
+ middleware : middlewareOrHandler ,
352
+ } ) ;
353
+ return descriptor ;
354
+ } ;
355
+ }
356
+
357
+ if ( middlewareOrHandler && typeof middlewareOrHandler === 'function' ) {
358
+ this . route ( middlewareOrHandler , { method, path } ) ;
340
359
return ;
341
360
}
342
361
@@ -347,54 +366,142 @@ abstract class BaseRouter {
347
366
}
348
367
349
368
public get ( path : Path , handler : RouteHandler ) : void ;
369
+ public get ( path : Path , middleware : Middleware [ ] , handler : RouteHandler ) : void ;
350
370
public get ( path : Path ) : MethodDecorator ;
351
- public get ( path : Path , handler ?: RouteHandler ) : MethodDecorator | undefined {
352
- return this . #handleHttpMethod( HttpVerbs . GET , path , handler ) ;
371
+ public get ( path : Path , middleware : Middleware [ ] ) : MethodDecorator ;
372
+ public get (
373
+ path : Path ,
374
+ middlewareOrHandler ?: Middleware [ ] | RouteHandler ,
375
+ handler ?: RouteHandler
376
+ ) : MethodDecorator | undefined {
377
+ return this . #handleHttpMethod(
378
+ HttpVerbs . GET ,
379
+ path ,
380
+ middlewareOrHandler ,
381
+ handler
382
+ ) ;
353
383
}
354
384
355
385
public post ( path : Path , handler : RouteHandler ) : void ;
386
+ public post (
387
+ path : Path ,
388
+ middleware : Middleware [ ] ,
389
+ handler : RouteHandler
390
+ ) : void ;
356
391
public post ( path : Path ) : MethodDecorator ;
357
- public post ( path : Path , handler ?: RouteHandler ) : MethodDecorator | undefined {
358
- return this . #handleHttpMethod( HttpVerbs . POST , path , handler ) ;
392
+ public post ( path : Path , middleware : Middleware [ ] ) : MethodDecorator ;
393
+ public post (
394
+ path : Path ,
395
+ middlewareOrHandler ?: Middleware [ ] | RouteHandler ,
396
+ handler ?: RouteHandler
397
+ ) : MethodDecorator | undefined {
398
+ return this . #handleHttpMethod(
399
+ HttpVerbs . POST ,
400
+ path ,
401
+ middlewareOrHandler ,
402
+ handler
403
+ ) ;
359
404
}
360
405
361
406
public put ( path : Path , handler : RouteHandler ) : void ;
407
+ public put ( path : Path , middleware : Middleware [ ] , handler : RouteHandler ) : void ;
362
408
public put ( path : Path ) : MethodDecorator ;
363
- public put ( path : Path , handler ?: RouteHandler ) : MethodDecorator | undefined {
364
- return this . #handleHttpMethod( HttpVerbs . PUT , path , handler ) ;
409
+ public put ( path : Path , middleware : Middleware [ ] ) : MethodDecorator ;
410
+ public put (
411
+ path : Path ,
412
+ middlewareOrHandler ?: Middleware [ ] | RouteHandler ,
413
+ handler ?: RouteHandler
414
+ ) : MethodDecorator | undefined {
415
+ return this . #handleHttpMethod(
416
+ HttpVerbs . PUT ,
417
+ path ,
418
+ middlewareOrHandler ,
419
+ handler
420
+ ) ;
365
421
}
366
422
367
423
public patch ( path : Path , handler : RouteHandler ) : void ;
424
+ public patch (
425
+ path : Path ,
426
+ middleware : Middleware [ ] ,
427
+ handler : RouteHandler
428
+ ) : void ;
368
429
public patch ( path : Path ) : MethodDecorator ;
430
+ public patch ( path : Path , middleware : Middleware [ ] ) : MethodDecorator ;
369
431
public patch (
370
432
path : Path ,
433
+ middlewareOrHandler ?: Middleware [ ] | RouteHandler ,
371
434
handler ?: RouteHandler
372
435
) : MethodDecorator | undefined {
373
- return this . #handleHttpMethod( HttpVerbs . PATCH , path , handler ) ;
436
+ return this . #handleHttpMethod(
437
+ HttpVerbs . PATCH ,
438
+ path ,
439
+ middlewareOrHandler ,
440
+ handler
441
+ ) ;
374
442
}
375
443
376
444
public delete ( path : Path , handler : RouteHandler ) : void ;
445
+ public delete (
446
+ path : Path ,
447
+ middleware : Middleware [ ] ,
448
+ handler : RouteHandler
449
+ ) : void ;
377
450
public delete ( path : Path ) : MethodDecorator ;
451
+ public delete ( path : Path , middleware : Middleware [ ] ) : MethodDecorator ;
378
452
public delete (
379
453
path : Path ,
454
+ middlewareOrHandler ?: Middleware [ ] | RouteHandler ,
380
455
handler ?: RouteHandler
381
456
) : MethodDecorator | undefined {
382
- return this . #handleHttpMethod( HttpVerbs . DELETE , path , handler ) ;
457
+ return this . #handleHttpMethod(
458
+ HttpVerbs . DELETE ,
459
+ path ,
460
+ middlewareOrHandler ,
461
+ handler
462
+ ) ;
383
463
}
384
464
385
465
public head ( path : Path , handler : RouteHandler ) : void ;
466
+ public head (
467
+ path : Path ,
468
+ middleware : Middleware [ ] ,
469
+ handler : RouteHandler
470
+ ) : void ;
386
471
public head ( path : Path ) : MethodDecorator ;
387
- public head ( path : Path , handler ?: RouteHandler ) : MethodDecorator | undefined {
388
- return this . #handleHttpMethod( HttpVerbs . HEAD , path , handler ) ;
472
+ public head ( path : Path , middleware : Middleware [ ] ) : MethodDecorator ;
473
+ public head (
474
+ path : Path ,
475
+ middlewareOrHandler ?: Middleware [ ] | RouteHandler ,
476
+ handler ?: RouteHandler
477
+ ) : MethodDecorator | undefined {
478
+ return this . #handleHttpMethod(
479
+ HttpVerbs . HEAD ,
480
+ path ,
481
+ middlewareOrHandler ,
482
+ handler
483
+ ) ;
389
484
}
390
485
391
486
public options ( path : Path , handler : RouteHandler ) : void ;
487
+ public options (
488
+ path : Path ,
489
+ middleware : Middleware [ ] ,
490
+ handler : RouteHandler
491
+ ) : void ;
392
492
public options ( path : Path ) : MethodDecorator ;
493
+ public options ( path : Path , middleware : Middleware [ ] ) : MethodDecorator ;
393
494
public options (
394
495
path : Path ,
496
+ middlewareOrHandler ?: Middleware [ ] | RouteHandler ,
395
497
handler ?: RouteHandler
396
498
) : MethodDecorator | undefined {
397
- return this . #handleHttpMethod( HttpVerbs . OPTIONS , path , handler ) ;
499
+ return this . #handleHttpMethod(
500
+ HttpVerbs . OPTIONS ,
501
+ path ,
502
+ middlewareOrHandler ,
503
+ handler
504
+ ) ;
398
505
}
399
506
}
400
507
0 commit comments