Within my express.js application, I have set up an endpoint where I extract the user from the request as shown below:
@Put('/:_id')
async update (@Request() req: express.Request) {
// @ts-ignore
const user = req.user;
....
}
I am interested in creating a custom Parameter Decorator to easily retrieve the user using @GetUser user
, which would allow my endpoint to appear like the example below:
@Put('/:_id')
async update (@GetUser user: any) {
// We can now directly use the user object
....
}
Upon investigating, I found out that I can develop a custom Parameter Decorator by implementing the following code snippet:
export function GetUser(
target: Object,
propertyKey: string,
parameterIndex: number
) {
console.log(`Decorating param ${parameterIndex} from ${propertyKey}`);
}
The question arises on how to access the request
within this custom Parameter Decorator to fetch the user
?