| 47 | } from '../filter'; |
| 48 | |
| 49 | export class DinoContainer implements IDinoContainer { |
| 50 | private diContainer: IDIContainer; |
| 51 | private app: Express; |
| 52 | private baseUri: string; |
| 53 | private routeTable: IRouteTable; |
| 54 | private enableTaskContext: boolean; |
| 55 | private useRouterCb: IRouterCallBack; |
| 56 | |
| 57 | constructor(config: IDinoContainerConfig) { |
| 58 | this.app = config.app; |
| 59 | this.baseUri = config.baseUri; |
| 60 | this.enableTaskContext = config.enableTaskContext; |
| 61 | this.useRouterCb = config.routerCallback; |
| 62 | this.diContainer = DIContainer.create(config.diContainer, config.diResolveCb); |
| 63 | this.routeTable = RouteTable.create(); |
| 64 | } |
| 65 | |
| 66 | // made public for unit test and not available on interface contract |
| 67 | resolve<T>(middleware: Function, dino: IDinoProperties): T { |
| 68 | let o = this.diContainer.resolve<T>(middleware); |
| 69 | |
| 70 | if (DataUtility.isUndefinedOrNull(dino)) { |
| 71 | return o; |
| 72 | } |
| 73 | |
| 74 | // walk-through the entire object chain and replace IUserIdentity instance References |
| 75 | return this.enableTaskContext ? |
| 76 | ObjectUtility.replaceObjectReferences(o, dino.context, IUserIdentity) : o; |
| 77 | } |
| 78 | |
| 79 | routeNotFoundMiddleware(middleware: any): void { |
| 80 | if (DinoUtility.isSyncRequestStartMiddleware(middleware)) { |
| 81 | // created as singleton object |
| 82 | let mw = new middleware(this.routeTable); |
| 83 | this.app.use(this.baseUri, (req, res, next) => { |
| 84 | mw.invoke(req, res, next); |
| 85 | }); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // create builtin middlewares as singleton objects |
| 90 | // ex: DinoStartMiddleware, TaskContextMiddleware |
| 91 | builtInRequestStartMiddleware(middleware: any): void { |
| 92 | if (DinoUtility.isSyncRequestStartMiddleware(middleware)) { |
| 93 | let mw = new middleware(); |
| 94 | this.app.use(this.baseUri, (req, res, next) => { |
| 95 | mw.invoke(req, res, next); |
| 96 | }); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | // ex: ResponseEndMiddleware |
| 101 | builtInRequestEndMiddleware(middleware: any): void { |
| 102 | if (DinoUtility.isSyncRequestEndMiddleware(middleware)) { |
| 103 | let mw = new middleware(); |
| 104 | this.app.use(this.baseUri, (req, res, next) => { |
| 105 | mw.invoke(req, res, next, res.locals.dino.result); |
| 106 | }); |
nothing calls this directly
no outgoing calls
no test coverage detected