import { Module, Type } from '@nestjs/common'; import { NestjsOemcService, OemcController } from './nestjs-oemc.service'; import { NestjsOemcExecuteService } from './nestjs-oemc-execute.service'; import { NestjsOemcController } from './nestjs-oemc.controller'; import { DiscoveryModule, DiscoveryService } from '@nestjs/core'; import { OEMC_CONTROLLER } from './nestjs-oemc.constants'; type ControllerType = any; function getOemcControllerName(controllerType: ControllerType): string | null { if (Reflect.getMetadata(OEMC_CONTROLLER, controllerType)) { return Reflect.getMetadata(OEMC_CONTROLLER, controllerType); } return null; } function isOemcController(controllerType: ControllerType): boolean { if (getOemcControllerName(controllerType) !== null) { return true; } return false; } @Module({ imports: [DiscoveryModule], providers: [NestjsOemcService, NestjsOemcExecuteService], controllers: [NestjsOemcController], exports: [NestjsOemcService, NestjsOemcExecuteService], }) export class NestjsOemcModule { constructor( private readonly discoveryService: DiscoveryService, private readonly nestjsOemcService: NestjsOemcService, ) {} onModuleInit(): any { const wrappers = this.discoveryService.getControllers(); const oemcControllerMaps: OemcController[] = wrappers .filter((wrapper) => wrapper.metatype && isOemcController(wrapper.metatype)) .map((wrapper) => { return { controllerAlias: getOemcControllerName(wrapper.metatype), oemcController: wrapper.instance, }; }); oemcControllerMaps.forEach((provider) => this.nestjsOemcService.registerOemcController(provider)); } }