Support named route groups; canonical namespaces
This commit is contained in:
@@ -1,16 +1,39 @@
|
||||
import {Collection} from "@extollo/util"
|
||||
import {Collection, ErrorWithContext} from "@extollo/util"
|
||||
import {AppClass} from "../../lifecycle/AppClass"
|
||||
import {RouteHandler} from "./Route"
|
||||
import {Container} from "@extollo/di"
|
||||
import {Logging} from "../../service/Logging";
|
||||
|
||||
export class RouteGroup extends AppClass {
|
||||
private static currentGroupNesting: RouteGroup[] = []
|
||||
|
||||
protected static namedGroups: {[key: string]: () => void } = {}
|
||||
|
||||
protected middlewares: Collection<{ stage: 'pre' | 'post', handler: RouteHandler }> = new Collection<{stage: "pre" | "post"; handler: RouteHandler}>()
|
||||
|
||||
public static getCurrentGroupHierarchy(): RouteGroup[] {
|
||||
return [...this.currentGroupNesting]
|
||||
}
|
||||
|
||||
public static named(name: string, define: () => void) {
|
||||
if ( this.namedGroups[name] ) {
|
||||
Container.getContainer()
|
||||
.make<Logging>(Logging)
|
||||
.warn(`Replacing named route group: ${name}`)
|
||||
}
|
||||
|
||||
this.namedGroups[name] = define
|
||||
}
|
||||
|
||||
public static include(name: string) {
|
||||
if (!this.namedGroups[name]) {
|
||||
throw new ErrorWithContext(`No route group exists with name: ${name}`, {name})
|
||||
}
|
||||
|
||||
this.namedGroups[name]()
|
||||
}
|
||||
|
||||
|
||||
constructor(
|
||||
public readonly group: () => void | Promise<void>,
|
||||
public readonly prefix: string
|
||||
|
||||
Reference in New Issue
Block a user