[ ] Regression
[ ] Bug report
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
I didn't find any way to check if a class is controller/service without referring to Nest internal values.
I'd like Nest to provide exported functions to check if a class is decorated with Controller /Injectable.
N/A
I am writing a tool to automatically load controllers& services from project directory. However, as Nest throws when loading non-Controller classes as controller, it requires to check if a class is Controller before gathering them into an array and pass to Module decorator.
Nest version: 5.4.0
For Tooling issues:
- Node version: v10.13.0
- Platform: macOS
Others:
You could just use TypeScript AST.
But as the loading is performed at runtime with TypeScript/ compiled JavaScript, where TypeScript AST may not be existing, I think it would be convenient to provide such function.
Try Reflect from reflect-metadata, if you want to access all modules, just use ModulesContainer, it can be import from @nestjs/core/injector/modules-container.
@dzzzzzy This is not possible, as Injectable() does not add any metadata to the class.
@coderfox what you could do is using ModulesContainer to get all the modules.
You can go thought them and reflect there metadata to get components
as
Reflect.getMetadata(metadata.PROVIDERS, metatype) || [];
and controllers
Reflect.getMetadata(metadata.CONTROLLERS, metatype) || [];
@adrien2p Thanks for your reply, but I require such function before those controllers and services are loaded, to determine whether they are controller or service and load them to Nest, as Nest throws when attempting to load non-controller / service as controller / service. I don't think such method resolves the problem.
Unless you're creating a 3rd party library, you could just proxy/wrap the decorators and add metadata to them yourself to determine whether or not its a controller or service.
Anyhow you're wrong, the @Controller() decorator does define metadata on the metatype.
I still don't get what you want.
Basically what you're trying to accomplish is the exact opposite of what module architecture frameworks are here for.
If you just glob through your entire project to put controllers and providers into the module decorator, then what's the point of even using Nest?
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
Unless you're creating a 3rd party library, you could just proxy/wrap the decorators and add metadata to them yourself to determine whether or not its a controller or service.
Anyhow you're wrong, the
@Controller()decorator does define metadata on the metatype.https://github.com/nestjs/nest/blob/385071ba6dd3b98f91edfca0ed5aadb611bb678f/packages/common/decorators/core/controller.decorator.ts#L4-L13
I still don't get what you want.
Basically what you're trying to accomplish is the exact opposite of what module architecture frameworks are here for.
If you just glob through your entire project to put controllers and providers into the module decorator, then what's the point of even using Nest?