Nest: add isController/ isService check util

Created on 17 Nov 2018  路  8Comments  路  Source: nestjs/nest

I'm submitting a...


[ ] 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.

Current behavior

I didn't find any way to check if a class is controller/service without referring to Nest internal values.

Expected behavior

I'd like Nest to provide exported functions to check if a class is decorated with Controller /Injectable.

Minimal reproduction of the problem with instructions

N/A

What is the motivation / use case for changing the behavior?

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.

Environment


Nest version: 5.4.0

For Tooling issues:
- Node version: v10.13.0
- Platform:  macOS

Others:

question 馃檶

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?

All 8 comments

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.

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?

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kamilmysliwiec picture kamilmysliwiec  路  178Comments

szkumorowski picture szkumorowski  路  62Comments

MonsieurMan picture MonsieurMan  路  28Comments

fmeynard picture fmeynard  路  45Comments

BrunnerLivio picture BrunnerLivio  路  40Comments