UNPKG

@nova-ts/core

Version:

A serverside framework used to build scalable application

66 lines (64 loc) 2.19 kB
import { ConvertPath } from "./chunk-SNUFADFR.js"; import { NovaControllerInvoker } from "./chunk-CUJD4MC2.js"; import { NovaControllerResolver } from "./chunk-3W35Z6ZM.js"; import { NovaFilterExecutor } from "./chunk-46BS5H2W.js"; import { ControllerRegistry } from "./chunk-A45SEU4H.js"; import { NovaConstant } from "./chunk-INEUQFI4.js"; // src/Resolver/NovaRouteBinder.ts import { ApplicationContext } from "@nova-ts/context"; var NovaRouteBinder = class { constructor(app) { this.app = app; } resolver = new NovaControllerResolver(); /** * Binds all controllers from the `ControllerRegistry` to the Express app. * * This method: * - Retrieves controller classes and metadata. * - Resolves controller instances from `ApplicationContext`. * - Constructs the full route path. * - Sets up a `NovaFilterExecutor` and `NovaControllerInvoker` for each route. * - Registers the route handler with Express using the appropriate HTTP method. * * This is the main entry point for bootstrapping route registration in the Nova framework. */ bindAllControllers() { for (const [clazz, definition] of ControllerRegistry) { const instance = ApplicationContext.get(clazz.name); const basePath = Reflect.getMetadata(NovaConstant.Controller, clazz); for (const route of definition.routes) { const fullPath = ConvertPath(`${basePath}${route.path}`); const handler = instance[route.handlerName].bind(instance); this.app[route.method](fullPath, async (req, res, next) => { const Request = req; const Response = res; try { const controllerInvoker = new NovaControllerInvoker(instance, route, handler, this.resolver); await new NovaFilterExecutor().execute(Request, Response, async () => controllerInvoker.invoke(Request, Response, next)); Response.json(Response.body); } catch (error) { console.error(`[ERROR] in ${route.method.toUpperCase()} ${fullPath}:`, error); next(error); } }); } } } }; export { NovaRouteBinder }; //# sourceMappingURL=chunk-476H377A.js.map