@@ -8,9 +8,11 @@ import {
88import { iterate } from 'iterare' ;
99import * as pathToRegexp from 'path-to-regexp' ;
1010import { uid } from 'uid' ;
11- import { ExcludeRouteMetadata } from '../router/interfaces/exclude-route-metadata.interface' ;
11+ import {
12+ ExcludeRouteMetadata ,
13+ RouteMetadata ,
14+ } from '../router/interfaces/exclude-route-metadata.interface' ;
1215import { isRouteExcluded } from '../router/utils' ;
13-
1416export const mapToExcludeRoute = (
1517 routes : ( string | RouteInfo ) [ ] ,
1618) : ExcludeRouteMetadata [ ] => {
@@ -32,20 +34,28 @@ export const mapToExcludeRoute = (
3234
3335export const filterMiddleware = < T extends Function | Type < any > = any > (
3436 middleware : T [ ] ,
35- routes : RouteInfo [ ] ,
37+ includedRoutes : RouteInfo [ ] ,
38+ excludedRoutes : RouteInfo [ ] ,
3639 httpAdapter : HttpServer ,
3740) => {
38- const excludedRoutes = mapToExcludeRoute ( routes ) ;
3941 return iterate ( [ ] )
4042 . concat ( middleware )
4143 . filter ( isFunction )
42- . map ( ( item : T ) => mapToClass ( item , excludedRoutes , httpAdapter ) )
44+ . map ( ( item : T ) =>
45+ mapToClass (
46+ item ,
47+ mapToExcludeRoute ( includedRoutes ) ,
48+ mapToExcludeRoute ( excludedRoutes ) ,
49+ httpAdapter ,
50+ ) ,
51+ )
4352 . toArray ( ) ;
4453} ;
4554
4655export const mapToClass = < T extends Function | Type < any > > (
4756 middleware : T ,
48- excludedRoutes : ExcludeRouteMetadata [ ] ,
57+ includedRoutes : RouteMetadata [ ] ,
58+ excludedRoutes : RouteMetadata [ ] ,
4959 httpAdapter : HttpServer ,
5060) => {
5161 if ( isMiddlewareClass ( middleware ) ) {
@@ -57,6 +67,7 @@ export const mapToClass = <T extends Function | Type<any>>(
5767 const [ req , _ , next ] = params as [ Record < string , any > , any , Function ] ;
5868 const isExcluded = isMiddlewareRouteExcluded (
5969 req ,
70+ includedRoutes ,
6071 excludedRoutes ,
6172 httpAdapter ,
6273 ) ;
@@ -74,6 +85,7 @@ export const mapToClass = <T extends Function | Type<any>>(
7485 const [ req , _ , next ] = params as [ Record < string , any > , any , Function ] ;
7586 const isExcluded = isMiddlewareRouteExcluded (
7687 req ,
88+ includedRoutes ,
7789 excludedRoutes ,
7890 httpAdapter ,
7991 ) ;
@@ -106,7 +118,8 @@ export function assignToken(metatype: Type<any>, token = uid(21)): Type<any> {
106118
107119export function isMiddlewareRouteExcluded (
108120 req : Record < string , any > ,
109- excludedRoutes : ExcludeRouteMetadata [ ] ,
121+ includedRoutes : RouteMetadata [ ] ,
122+ excludedRoutes : RouteMetadata [ ] ,
110123 httpAdapter : HttpServer ,
111124) : boolean {
112125 if ( excludedRoutes . length <= 0 ) {
@@ -120,5 +133,9 @@ export function isMiddlewareRouteExcluded(
120133 ? originalUrl . slice ( 0 , queryParamsIndex )
121134 : originalUrl ;
122135
123- return isRouteExcluded ( excludedRoutes , pathname , RequestMethod [ reqMethod ] ) ;
136+ if ( includedRoutes . length > 0 ) {
137+ return ! isRouteExcluded ( includedRoutes , pathname , RequestMethod [ reqMethod ] ) ;
138+ } else {
139+ return isRouteExcluded ( excludedRoutes , pathname , RequestMethod [ reqMethod ] ) ;
140+ }
124141}
0 commit comments