|
|
|
import {AppClass} from "../../lifecycle/AppClass";
|
|
|
|
import {HTTPMethod, Request} from "../lifecycle/Request";
|
|
|
|
import {Application} from "../../lifecycle/Application";
|
|
|
|
import {RouteGroup} from "./RouteGroup";
|
|
|
|
import {ResponseFactory} from "../response/ResponseFactory";
|
|
|
|
import {Response} from "../lifecycle/Response";
|
|
|
|
import {Controllers} from "../../service/Controllers";
|
|
|
|
import {ErrorWithContext} from "@extollo/util";
|
|
|
|
import {Controller} from "../Controller";
|
|
|
|
|
|
|
|
export type ResponseObject = ResponseFactory | string | number | void | any | Promise<ResponseObject>
|
|
|
|
export type RouteHandler = ((request: Request, response: Response) => ResponseObject) | ((request: Request) => ResponseObject) | (() => ResponseObject) | string
|
|
|
|
export type ResolvedRouteHandler = (request: Request) => ResponseObject
|
|
|
|
|
|
|
|
|
|
|
|
// TODO middleware, domains, named routes - support this on groups as well
|
|
|
|
|
|
|
|
export class Route extends AppClass {
|
|
|
|
private static registeredRoutes: Route[] = []
|
|
|
|
private static registeredGroups: RouteGroup[] = []
|
|
|
|
|
|
|
|
private static compiledGroupStack: RouteGroup[] = []
|
|
|
|
|
|
|
|
public static registerGroup(group: RouteGroup) {
|
|
|
|
this.registeredGroups.push(group)
|
|
|
|
}
|
|
|
|
|
|
|
|
public static async compile(): Promise<Route[]> {
|
|
|
|
let registeredRoutes = this.registeredRoutes
|
|
|
|
const registeredGroups = this.registeredGroups
|
|
|
|
|
|
|
|
this.registeredRoutes = []
|
|
|
|
this.registeredGroups = []
|
|
|
|
|
|
|
|
const stack = [...this.compiledGroupStack].reverse()
|
|
|
|
for ( const route of registeredRoutes ) {
|
|
|
|
for ( const group of stack ) {
|
|
|
|
route.prepend(group.prefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
route.resolveHandler() // Try to resolve here to catch any errors at boot-time
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( const group of registeredGroups ) {
|
|
|
|
this.compiledGroupStack.push(group)
|
|
|
|
await group.group()
|
|
|
|
|
|
|
|
const childCompilation = await this.compile()
|
|
|
|
registeredRoutes = registeredRoutes.concat(childCompilation)
|
|
|
|
|
|
|
|
this.compiledGroupStack.pop()
|
|
|
|
}
|
|
|
|
|
|
|
|
return registeredRoutes
|
|
|
|
}
|
|
|
|
|
|
|
|
public static endpoint(method: HTTPMethod | HTTPMethod[], definition: string, handler: RouteHandler) {
|
|
|
|
const route = new Route(method, handler, definition)
|
|
|
|
this.registeredRoutes.push(route)
|
|
|
|
return route
|
|
|
|
}
|
|
|
|
|
|
|
|
public static get(definition: string, handler: RouteHandler) {
|
|
|
|
return this.endpoint('get', definition, handler)
|
|
|
|
}
|
|
|
|
|
|
|
|
public static post(definition: string, handler: RouteHandler) {
|
|
|
|
return this.endpoint('post', definition, handler)
|
|
|
|
}
|
|
|
|
|
|
|
|
public static put(definition: string, handler: RouteHandler) {
|
|
|
|
return this.endpoint('put', definition, handler)
|
|
|
|
}
|
|
|
|
|
|
|
|
public static patch(definition: string, handler: RouteHandler) {
|
|
|
|
return this.endpoint('patch', definition, handler)
|
|
|
|
}
|
|
|
|
|
|
|
|
public static delete(definition: string, handler: RouteHandler) {
|
|
|
|
return this.endpoint('delete', definition, handler)
|
|
|
|
}
|
|
|
|
|
|
|
|
public static any(definition: string, handler: RouteHandler) {
|
|
|
|
return this.endpoint(['get', 'put', 'patch', 'post', 'delete'], definition, handler)
|
|
|
|
}
|
|
|
|
|
|
|
|
public static group(prefix: string, group: () => void | Promise<void>) {
|
|
|
|
const grp = <RouteGroup> Application.getApplication().make(RouteGroup, group, prefix)
|
|
|
|
this.registeredGroups.push(grp)
|
|
|
|
return grp
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
protected method: HTTPMethod | HTTPMethod[],
|
|
|
|
protected readonly handler: RouteHandler,
|
|
|
|
protected route: string
|
|
|
|
) { super() }
|
|
|
|
|
|
|
|
public match(method: HTTPMethod, potential: string): boolean {
|
|
|
|
if ( Array.isArray(this.method) && !this.method.includes(method) ) return false
|
|
|
|
else if ( !Array.isArray(this.method) && this.method !== method ) return false
|
|
|
|
|
|
|
|
return !!this.extract(potential)
|
|
|
|
}
|
|
|
|
|
|
|
|
public extract(potential: string): {[key: string]: string} | undefined {
|
|
|
|
const routeParts = (this.route.startsWith('/') ? this.route.substr(1) : this.route).split('/')
|
|
|
|
const potentialParts = (potential.startsWith('/') ? potential.substr(1) : potential).split('/')
|
|
|
|
|
|
|
|
const params: any = {}
|
|
|
|
let wildcardIdx = 0
|
|
|
|
|
|
|
|
for ( let i = 0; i < routeParts.length; i += 1 ) {
|
|
|
|
const part = routeParts[i]
|
|
|
|
|
|
|
|
if ( part === '**' ) {
|
|
|
|
params[wildcardIdx] = potentialParts.slice(i).join('/')
|
|
|
|
return params
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( (potentialParts.length - 1) < i ) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( part === '*' ) {
|
|
|
|
params[wildcardIdx] = potentialParts[i]
|
|
|
|
wildcardIdx += 1
|
|
|
|
} else if ( part.startsWith(':') ) {
|
|
|
|
params[part.substr(1)] = potentialParts[i]
|
|
|
|
} else if ( potentialParts[i] !== part ) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we got here, we didn't find a **
|
|
|
|
// So, if the lengths are different, fail
|
|
|
|
if ( routeParts.length !== potentialParts.length ) return
|
|
|
|
return params
|
|
|
|
}
|
|
|
|
|
|
|
|
public resolveHandler(): ResolvedRouteHandler {
|
|
|
|
if ( typeof this.handler !== 'string' ) {
|
|
|
|
return (request: Request) => {
|
|
|
|
// @ts-ignore
|
|
|
|
return this.handler(request, request.response)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const parts = this.handler.split('.')
|
|
|
|
if ( parts.length < 1 ) {
|
|
|
|
const e = new ErrorWithContext('Route handler does not specify a method name.')
|
|
|
|
e.context = {
|
|
|
|
handler: this.handler
|
|
|
|
}
|
|
|
|
throw e
|
|
|
|
}
|
|
|
|
|
|
|
|
const [controllerName, methodName] = parts
|
|
|
|
|
|
|
|
const controllersService = <Controllers> this.make(Controllers)
|
|
|
|
const controllerClass = controllersService.get(controllerName)
|
|
|
|
if ( !controllerClass ) {
|
|
|
|
const e = new ErrorWithContext('Controller not found for route handler.')
|
|
|
|
e.context = {
|
|
|
|
handler: this.handler,
|
|
|
|
controllerName,
|
|
|
|
methodName,
|
|
|
|
}
|
|
|
|
throw e
|
|
|
|
}
|
|
|
|
|
|
|
|
return (request: Request) => {
|
|
|
|
// If not a function, then we got a string reference to a controller method
|
|
|
|
// So, we need to use the request container to instantiate the controller
|
|
|
|
// and bind the method
|
|
|
|
const controller = <Controller> request.make(controllerClass, request)
|
|
|
|
const method = controller.getBoundMethod(methodName)
|
|
|
|
return method()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private prepend(prefix: string) {
|
|
|
|
if ( !prefix.endsWith('/') ) prefix = `${prefix}/`
|
|
|
|
if ( this.route.startsWith('/') ) this.route = this.route.substring(1)
|
|
|
|
this.route = `${prefix}${this.route}`
|
|
|
|
}
|
|
|
|
|
|
|
|
toString() {
|
|
|
|
const method = Array.isArray(this.method) ? this.method : [this.method]
|
|
|
|
return `${method.join('|')} -> ${this.route}`
|
|
|
|
}
|
|
|
|
}
|