@@ -66,6 +66,10 @@ class App {
6666 return this . routes . some ( route => route . should ( request ) ) ;
6767 }
6868
69+ when ( predicate : FilterFunction ) : AppRoute {
70+ return this . _top . when ( predicate ) ;
71+ }
72+
6973 /* Route methods */
7074 path ( expr : string ) : AppRoute {
7175 return this . _top . path ( expr ) ;
@@ -123,6 +127,7 @@ interface AppRouteContext {
123127 path ?: string ;
124128 method ?: string ;
125129 domain ?: string ;
130+ when ?: FilterFunction ;
126131}
127132
128133class AppRoute {
@@ -141,6 +146,10 @@ class AppRoute {
141146 } ) ;
142147 }
143148
149+ when ( predicate : FilterFunction ) : AppRoute {
150+ return this . _sub ( { when : predicate } ) ;
151+ }
152+
144153 path ( expr : string ) : AppRoute {
145154 return this . _sub ( { path : expr } ) ;
146155 }
@@ -207,7 +216,7 @@ class AppRoute {
207216}
208217
209218function filterFromCtx ( ctx : AppRouteContext ) : FilterFunction {
210- const { root, path, method, domain } = ctx ;
219+ const { root, path, method, domain, when } = ctx ;
211220
212221 // Pattern matchers
213222 const pathMatcher = path ? matcher ( path ) : null ;
@@ -224,9 +233,10 @@ function filterFromCtx(ctx: AppRouteContext): FilterFunction {
224233 const d = domainMatcher ? domainMatcher ( url . hostname ) : true ;
225234 const p = pathMatcher ? pathMatcher ( finalPath ) : true ;
226235 const m = method ? method === req . method : true ;
236+ const w = when ? when ( req ) : true ;
227237
228238 // All must match for filter to be true
229- return ( r && d && p && m ) ;
239+ return ( r && d && p && m && w ) ;
230240 }
231241}
232242
0 commit comments