Files
plane/silo/src/lib/controller.ts
guru_sainath b8f78807d2 chore: updated the dynamic base path in silo (#1835)
* fix: silo base path improvements

* chore: added health check controller

---------

Co-authored-by: sriram veeraghanta <veeraghanta.sriram@gmail.com>
2024-11-29 13:02:38 +05:30

29 lines
1.1 KiB
TypeScript

import { RequestHandler, Router } from "express";
type HttpMethod = "get" | "post" | "put" | "delete" | "patch" | "options" | "head";
export function registerControllers(router: Router, Controller: any) {
const instance = new Controller();
const baseRoute = Reflect.getMetadata("baseRoute", Controller) as string;
Object.getOwnPropertyNames(Controller.prototype).forEach((methodName) => {
if (methodName === "constructor") return; // Skip the constructor
const method = Reflect.getMetadata("method", instance, methodName) as HttpMethod;
const route = Reflect.getMetadata("route", instance, methodName) as string;
const middlewares = (Reflect.getMetadata("middlewares", instance, methodName) as RequestHandler[]) || [];
if (method && route) {
const handler = instance[methodName as keyof typeof instance] as unknown;
if (typeof handler === "function") {
(router[method] as (path: string, ...handlers: RequestHandler[]) => void)(
`${baseRoute}${route}`,
...middlewares,
handler.bind(instance)
);
}
}
});
}