Support named route groups; canonical namespaces

This commit is contained in:
2021-03-21 21:10:42 -05:00
parent a93cd6d192
commit e8fdb04ae8
2 changed files with 60 additions and 2 deletions

View File

@@ -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