You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Since you're replacing the entire function definition as opposed to only adding metadata (which was the original intended purpose), any other previously defined metadata (those defined by the @Get) are lost, because class decorators are applied after methdo decorators.
You can work around it by explicitly copying the metadata in your custom decorator using this helper function:
/** * Copies all metadata from one object to another. * Useful for overwriting function definition in * decorators while keeping all previously * attached metadata * * @param from object to copy metadata from * @param to object to copy metadata to */exportfunctioncopyMetadata(from: any,to: any){constmetadataKeys=Reflect.getMetadataKeys(from);metadataKeys.map((key)=>{constvalue=Reflect.getMetadata(key,from);Reflect.defineMetadata(key,value,to);});}
https://github.com/CatsMiaow/typescript-starter/blob/master/src/app.controller.ts#L7
In the sample code above,
test2
outputs normally, but if you add (uncomment)@DecorateAll
, the controller's route outputs a 404 page.https://github.com/Papooch/decorate-all/blob/main/src/lib/decorate-all.decorator.ts#L35
If remove
Object.defineProperty
it doesn't throw a 404, but the decorator doesn't work.The text was updated successfully, but these errors were encountered: