/** * @autor Olsi Rrjolli * This Class represent the controller registry. */ import MethodNotFoundError from './exceptions/MethodNotFoundError'; import { getMethodName, isMethodExistInController, getControllerMetadata, getMethod, parseArguments, generateMetaKey } from './utils/reflectMetadataHelpers'; import MethodInformation from './models/MethodInformation'; import { IController, IAwsRequestEvent } from './types/index'; export default class ControllerRegistry { private static controllersSet = new Set(); public static register(controller: IController) { this.controllersSet.add(controller); } /** * Scan the controller to find which method can execute for the request resource * @param requestEvent * The aws request event * @returns {Controller} * Get the correct Controller instance to execute the request command */ public static controllerScan(requestEvent: IAwsRequestEvent): MethodInformation { let methodInformation: MethodInformation; this.controllersSet.forEach((controllerClass: any) => { const controller = new controllerClass(); console.log('Registered Controller: ', controller.constructor.name); console.log('Registered Methods: ', getControllerMetadata(controller)); if (isMethodExistInController(requestEvent, controller)) { console.log('Selected Controller: ', controller.constructor.name); const methodName: string = getMethodName(requestEvent, controller); console.log('Selected Method: ', methodName); const toCallMethod = getMethod(controller, methodName); const args = parseArguments(requestEvent, methodName, controller); methodInformation = new MethodInformation(); methodInformation.args = args; methodInformation.toCallMethod = toCallMethod; return; } }); if (methodInformation) { return methodInformation; } const methodName: string = generateMetaKey(requestEvent.httpMethod, requestEvent.resource); console.trace('No method found for the ressource.', methodName); throw new MethodNotFoundError(`No method found for the ressource:${methodName}`); } }