Hernán
Hernán

Reputation: 4587

NestJS: Set @Param and body values to DTO in POST request

I have a POST request entry where I specify an "account" parameter as a path parameter, and a boolean in the body to set validation state, like:

POST /users/authorized/USER_ACCOUNT1

The body would carry:

valid=1

I have the following controller entry:

@ApiTags('users')
    @ApiOperation( { summary: 'Set user account status.  '} )
    @Post('authorized/:account')
    async setAuthStatus(params: SetUserAuthDto) {
        return this.userService.setUserAuthDto(params);
    }

How I can feed both the "account" and the request body "status" parameter to the same DTO? I assume I cannot use both decorators @Param and @Body there. Should I use pipes?

I'm new in NestJS so excuse my ignorance.

Thanks.

Upvotes: 6

Views: 14717

Answers (1)

Jay McDoniel
Jay McDoniel

Reputation: 70111

There's nothing wrong with using both @Params() and @Body(). It's a common pattern actually

@Post('authorized/:account')
async setAuthStatus(@Param('account') accountValue: string, @Body() body: SetUserAuthDto) {
  // do your thing here
}

However, if you want to use a single decorator you could use the createParamDecorator method and have it return req.params and req.body in a single, merged object, so you could do @ParamAndBody()

Upvotes: 8

Related Questions