MCPcopy Create free account
hub / github.com/ParallelTask/dinoloop / registerController

Method registerController

src/modules/core/dino.container.ts:302–375  ·  view source on GitHub ↗
(type: Function)

Source from the content-addressed store, hash-verified

300
301 // Registers and binds the controller with express router
302 registerController(type: Function): void {
303
304 if (!DinoUtility.isApiController(type)) return;
305
306 let controller: ApiController = ObjectUtility.create(type.prototype);
307
308 // validate if the controller has @Controller attribute
309 if (Reflector.hasMetadata(Attribute.controller, controller)) {
310
311 let metadata = this.populateControllerMiddlewares(controller);
312 let dinoRoute = DinoRouter.create({
313 enableTaskContext: this.enableTaskContext,
314 routerCb: this.useRouterCb,
315 diContainer: this.diContainer
316 });
317
318 const router = dinoRoute.expressRouter();
319
320 // Register expresswares before anything else gets registered on the router
321 // expresswares are the native express middleware handlers.
322 for (const expressWare of metadata.use) {
323 router.use(expressWare);
324 }
325
326 dinoRoute.registerMiddlewares(metadata.middlewares);
327 dinoRoute.registerBeginActionFilters(metadata.beforeActionFilters);
328
329 const props = DinoUtility.getControllerProperties(controller);
330
331 // loop through all the controller action methods
332 // this also includes the inherited base controllers action methods
333 for (let actionName of props) {
334
335 // loop through every HttpVerb key i.e. get, post ...
336 ObjectUtility.keys(RouteAttribute).forEach(httpAttribute => {
337
338 // Check if the controller action has HttpVerb (get, post ...) attribute
339 if (Reflector.hasMetadata(httpAttribute, controller, actionName)) {
340
341 let action = this.getActionMethodMetadata(httpAttribute, actionName, controller);
342 this.routeTable
343 .add(`${this.baseUri}${metadata.prefix}${action.route}`, action.httpVerb);
344
345 if (action.isAsync) {
346 router[action.httpVerb](action.route, async (req, res, next) => {
347 let ctx =
348 this.setUpDinoController(type, action, res);
349 ctx.patch(req, res, next);
350 ctx.invokeAsync(actionName);
351 });
352 } else {
353 router[action.httpVerb](action.route, (req, res, next) => {
354 let ctx =
355 this.setUpDinoController(type, action, res);
356 ctx.patch(req, res, next);
357 ctx.invoke(actionName);
358 });
359 }

Callers

nothing calls this directly

Calls 14

setUpDinoControllerMethod · 0.95
expressRouterMethod · 0.80
registerMiddlewaresMethod · 0.65
addMethod · 0.65
patchMethod · 0.65
invokeAsyncMethod · 0.65
invokeMethod · 0.65
registerResultFiltersMethod · 0.65

Tested by

no test coverage detected