Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using path parameters in nestjs guard

Can i get path parameters in nestjs guard function using some other way than just looking for a raw request object from http context?

what i want to do for example is

@Patch(':id/someActionName')
  @UseGuards(SomeGuard)
    async activateRole(@Param('id') id,@Body() input: SomeObject): Promise<any> {
        //some logic
        return response;
    }

and my SomeGuard would get value of 'id' parameter and 'input' parameter, input parameter is easy, but i don't see easy way to get 'id'

like image 862
Dmitry Yanet Avatar asked Oct 11 '18 08:10

Dmitry Yanet


1 Answers

In your guard you can access the route parameters by getting the request from the context like so:

canActivate(context: ExecutionContext): boolean {
  const request = context.switchToHttp().getRequest();
  const params = request.params;
  const id = params.id; // automatically parsed
}

This wasn't in the documentation and I had the exact same problem as you and had to dig through the request object.

like image 84
kyle Avatar answered Sep 21 '22 11:09

kyle