import { Controller } from './models/controller' ; import { GroupConfig } from './models/group_config' ; import { Middleware } from './models/middleware' ; import { RouteConfig } from './models/route_config' ; import Route from './route' ; export default class Router { /**
     * Configuration of this group, will be applied nested when transferred to Expresss
     */ private _config : GroupConfig ; /**
     * List of all groups beneath this one
     */ private _groups : Router [ ] = [ ] ; /**
     * The different routes applicable in this router
     */ private _routes : Route [ ] = [ ] ; param config constructor ( config ) { this . _config = config ; } /**
     * Getter for groups
     */ get groups ( ) : Router [ ] { return this . _groups ; } /**
     * Getter for routes
     */ get routes ( ) : Route [ ] { return this . _routes ; } returns { string | string } get middleware ( ) : Middleware [ ] { return this . _config . middleware || [ ] ; } returns { string | null } get prefix ( ) : string | null { return this . _config . prefix || null ; } returns { any } get context ( ) : any { return this . _config . context ; } param path param config public get ( path , config ) { const route = this . toRoute ( 'get' , path , config ) ; this . _routes . push ( route ) ; return route ; } param path param config public post ( path , config ) { const route = this . toRoute ( 'post' , path , config ) ; this . _routes . push ( route ) ; return route ; } param path param config public put ( path , config ) { const route = this . toRoute ( 'put' , path , config ) ; this . _routes . push ( route ) ; return route ; } param path param config public delete ( path , config ) { const route = this . toRoute ( 'delete' , path , config ) ; this . _routes . push ( route ) ; return route ; } param config param callback public group ( config , callback ) { const router = new Router ( config ) ; callback ( router ) ; this . _groups . push ( router ) ; } param method param path param controller returns private toRoute ( method , path , controller ) { if ( typeof controller === 'function' ) { return new Route ( method , path , ( controller as Controller ) ) ; } const config = ( controller as RouteConfig ) ; const route = new Route ( config . method ? config . method : method , path , config . controller ) ; route . name = config . name ; if ( config . middleware ) { route . middleware = config . middleware ; } if ( config . context ) { route . context = config . context ; } return route ; } } ;