Add support for regex matching routes

This commit is contained in:
garrettmills
2020-07-30 09:03:29 -05:00
parent 7611c1b2bf
commit 194fa476ca
3 changed files with 63 additions and 4 deletions

View File

@@ -14,6 +14,7 @@ import {Logging} from '../service/logging/Logging.ts'
import {Canon} from './Canon.ts'
import {isBindable} from '../lifecycle/AppClass.ts'
import {DeepmatchRoute} from "../http/routing/DeepmatchRoute.ts";
import {RegExRoute} from "../http/routing/RegExRoute.ts";
export type RouteHandler = (request: Request) => Request | Promise<Request> | ResponseFactory | Promise<ResponseFactory> | void | Promise<void>
export type RouteHandlers = RouteHandler[]
@@ -74,7 +75,7 @@ export default class Routing extends LifecycleUnit {
// @ts-ignore
this.definitions[base][verb] = this.build_handler(handlers)
this.instances.push(this.build_route(base))
this.instances.push(this.build_route(base, key))
}
}
}
@@ -136,8 +137,10 @@ export default class Routing extends LifecycleUnit {
return `/${joined}`.toLowerCase()
}
public build_route(base: string): Route {
if ( !base.includes(':') && !base.includes('*') ) {
public build_route(base: string, key: string): Route {
if ( key.startsWith('rex ') ) {
return new RegExRoute(base.split(key)[0], key)
} else if ( !base.includes(':') && !base.includes('*') ) {
return new SimpleRoute(base)
} else if ( base.includes('**') ) {
return new DeepmatchRoute(base)