-
-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug: NestJS Controllers not Routing [1.6.4] #124
Comments
Unfortunately, I didn't work with NestJS. To help me better understand and reproduce the issue, could you please provide a minimum reproduce repository along with launch instructions? This will enable me to quickly replicate the bug on my end. |
…on about logger-decorator issue #124 pustovitDmytro/logger-decorator#124
…on about logger-decorator issue #124 pustovitDmytro/logger-decorator#124
…on about logger-decorator issue #124 pustovitDmytro/logger-decorator#124
…on about logger-decorator issue #124 pustovitDmytro/logger-decorator#124
Oh no, I did not mean to spam this, sorry |
Hi again, think found a solution So, the API could be smth like that, and should explicitly include all metadata props, defined by other decorators: @log({
keepReflectMetadata: [ 'path' ] // path is the key I found useful for nestJS
}) Note that you can build your own singleton and reuse it: import { Decorator } from 'logger-decorator';
const log = new Decorator({ keepReflectMetadata: [ ] }); if you are looking for any other possible metadata props in NestJS you can watch next files: after adding 'path' metadata, looks like it helped, both /unmapped and /mapped/fails are logged and returns response.
however I noticed I hope I will release a new version with the |
Unfortunately it seems Reflect Metadata does not have an option to view all class/function/method metadata, only to access specific keys - Though keepMetadata: string[] would be a great solution until such option is available Do you think this feature could/should be requested? As for the undefined, notice it also applies to the unmapped, meaning there's probably another metadata key tracked by RouterExplorer, maybe one that defines which HTTP Method is used, so the logger may need to keep both intact Now just to clarify - Decorating a class with @log will apply the @log with the same config to all methods of that class, right? Meaning, keepMetadata should keep intact all the methods' metadata, not only the class itself's, right? ––––––––––––––––––––––––––––––––––– As for the previous comments you deleted // common.ts
import { Get } from '@nestjs/common'
import log from 'logger-decorator'
export Get = (path?, config?) => applyDecorators(log(config), Get(path))
// controller.ts
import { Get } from './common/common.ts' That way, any @get used on this Controller will automatically be logged |
After a little investigation I found it! Those are the keys used :) |
It seems the @<HTTPMethod> decorator itself is able to keep the metadata intact, so their code might be the key to a solution This, for example, works, keeping both the Role, and the Post metadata: import { SetMetadata } from "@nestjs/common";
const Role = (role: UserRole) => SetMetadata('role', role);
@Post()
@Role(UserRole.ADMIN)
create(@Body() body: CreateDto, @GetRole() role: UserRole) {
return this.clientService.create(role, body)
} Edit: I might be wrong here, it both uses their internal SetMetadata function that might be storing the keys, and it does not return a proxy function, which might be the problem, though, take my words with a grain of salt, I'm kind of lost |
|
import { Controller, PATH_METADATA, METHOD_METADATA, Get } from '@nestjs/common'
import log from 'logger-controller'
@Controller('resource')
@log({ keepMetadata: [PATH_METADATA, METHOD_METADATA] })
export class ResourceController { ... } So that when the log decorates the class, the keepMetadata protects all the metadata keys defined in all methods/properties within that class. As it basically decorates the methods within with the @log method decorator with the same config, is that right?
|
It seems I do have access to the METHOD_METADATA, printing 0 for Get, 1 for Post and 2 for Put I went through all their other constants, they do not use other metadata When testing it out, I found that simply setting those two metadata keys does work, so it SHOULD work - might the mistake be somewhere in your code instead? @SetMetadata('path', 'testing') // /client/testing
@SetMetadata('method', 2) // Put
@log() // Removes metadata assigned by @Post
@Post() // Post in path '/'
create(@Body() body: CreateDto) {
return this.clientService.create(body)
} Console: [Nest] 66453 - 02/01/2024, 2:00:26 PM LOG [RouterExplorer] Mapped {/client/testing, PUT} route +0ms |
yeah, metadata===0 for get, so I omitted it in if(metadata). yeah,
in this snippet it is an exact approach, I recommend |
🎉 This issue has been resolved in version 1.7.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Issue Description
Probably a result of the first issue, the class decorator decorating the functions, already having been decorated by their own @httpMethod decorators
To Reproduce
Steps to reproduce the behavior:
Example
Environment:
The text was updated successfully, but these errors were encountered: