import { Express , IRouterCallBack } from '../types' ; import { DinoContainer } from './dino.container' ; import { DataUtility } from '../utility' ; import { DinoStartMiddleware , TaskContextMiddleware , ResponseEndMiddleware , RouteExceptionMiddleware , ActionParamExceptionMiddleware , HttpResponseExceptionMiddleware , HttpResponseMessageMiddleware } from '../builtin/middlewares' ; import { IAppContainer } from '../interfaces' ; export class AppContainer implements IAppContainer { private app : Express ; controllers : Function [ ] = [ ] ; baseUri : string = '' ; startMiddleware : Function [ ] = [ ] ; endMiddleware : Function [ ] = [ ] ; appStartMiddleware : Function [ ] = [ ] ; diContainer : any ; diResolveCallback : any ; errorController : Function ; routeNotFoundMiddleware : Function ; errorMiddleware : Function [ ] = [ ] ; raiseModelError = false ; enableTaskContext = false ; useRouter : IRouterCallBack ; constructor ( app ) { this . app = app ; } build ( ) { let dinoContainer = DinoContainer . create ( { app : this . app , raiseModelError : this . raiseModelError , enableTaskContext : this . enableTaskContext , routerCallback : this . useRouter , baseUri : this . baseUri , diContainer : this . diContainer , diResolveCb : this . diResolveCallback } ) ; dinoContainer . builtInRequestStartMiddleware ( DinoStartMiddleware ) ; if ( this . enableTaskContext ) { dinoContainer . builtInRequestStartMiddleware ( TaskContextMiddleware ) ; } if ( ! DataUtility . isUndefinedOrNull ( this . routeNotFoundMiddleware ) ) { dinoContainer . routeNotFoundMiddleware ( this . routeNotFoundMiddleware ) ; } for ( const middleware of this . startMiddleware ) { dinoContainer . requestStartMiddleware ( middleware ) ; } for ( const controller of this . controllers ) { dinoContainer . registerController ( controller ) ; } for ( const middleware of this . endMiddleware ) { dinoContainer . requestEndMiddleware ( middleware ) ; } dinoContainer . builtInRequestEndMiddleware ( HttpResponseMessageMiddleware ) ; dinoContainer . builtInRequestEndMiddleware ( ResponseEndMiddleware ) ; for ( const middleware of this . errorMiddleware ) { dinoContainer . registerErrorMiddleware ( middleware ) ; } dinoContainer . builtInErrorMiddleware ( RouteExceptionMiddleware ) ; dinoContainer . builtInErrorMiddleware ( HttpResponseExceptionMiddleware ) ; dinoContainer . builtInErrorMiddleware ( ActionParamExceptionMiddleware ) ; if ( ! DataUtility . isUndefinedOrNull ( this . errorController ) ) { dinoContainer . registerErrorController ( this . errorController ) ; } for ( const middleware of this . appStartMiddleware ) { dinoContainer . appStartMiddleware ( middleware ) ; } } static create ( app ) { return new AppContainer ( app ) ; } }