import { Application } from 'express' ; import transfer from './connect/transfer' ; import { Middleware } from './models/middleware' ; import Route from './route' ; import Router from './router' ; import { constrain } from './util/constrain_path' ; import { trim } from './util/trim' ; export default function connect ( app , router , previouxPrefix = [ ] , previousMiddleware = [ ] , currentContext = undefined ) { let routes = [ ] ; let prefix = [ ... previouxPrefix ] ; if ( router . prefix !== null ) { prefix . push ( trim ( router . prefix ) ) ; } router . routes . forEach ( ( route ) => { let path = '' ; if ( prefix . length > 0 ) { path += '/' + prefix . join ( '/' ) ; } if ( trim ( route . path ) !== '' ) { path += '/' + trim ( route . path ) ; } const constraints = route . getConstraints ( ) ; if ( constraints !== undefined ) { path = constrain ( path , constraints ) ; } const context = route . context || currentContext ; transfer ( app , route . method , path , [ ... previousMiddleware , ... router . middleware , ... route . middleware ] , context ? route . controller . bind ( context ) : route . controller ) ; const aggregatedRoute = new Route ( route . method , path , route . controller ) ; aggregatedRoute . middleware = [ ... previousMiddleware , ... router . middleware , ... route . middleware ] ; aggregatedRoute . name = route . name ; routes . push ( aggregatedRoute ) ; } ) ; router . groups . forEach ( ( group ) => { routes = [ ... routes , ... connect ( app , group , prefix , [ ... previousMiddleware , ... router . middleware ] , group . context || currentContext ) ] ; } ) ; return routes ; } ;