I have defined custom pipe in Nest.js
@Injectable()
export class QueryValidationPipe implements PipeTransform<GetDto> {
public transform(value: GetDto, metadata: ArgumentMetadata): GetDto {
console.log('TEST');
(...)
return value;
}
I'm using it in controller for certain GET method in controller A. I'm using Nest.js @Query
decorator:
@Get()
@UsePipes(new QueryValidationPipe())
@UseFilters(new ExceptionFilters())
public async getMethod(@Query() query: GetDto) {
(...)
}
And it works fine, for every request, pipe is logging test message to console. Now in controller B I'm using same decorator, but for method with underlying req and res object as parameters:
@Get()
@UsePipes(new QueryValidationPipe())
@UseFilters(new ExceptionFilters())
public async getMethodTo(@Req() req: GetRequest, @Res() res: Response): Promise<Response> {
(...)
}
Here it doesn't work. Pipe transform method is not called despite requests being received by above method. What can be cause of this? I didn't found any word in documentation about pipes not working when @Req and @Res parameters are used in method.
And if that's the case (it seems it is), how can I fix this? I can't just use @Query query method parameter because I need to send back different headers depending on query param.
I have defined custom pipe in Nest.js
@Injectable()
export class QueryValidationPipe implements PipeTransform<GetDto> {
public transform(value: GetDto, metadata: ArgumentMetadata): GetDto {
console.log('TEST');
(...)
return value;
}
I'm using it in controller for certain GET method in controller A. I'm using Nest.js @Query
decorator:
@Get()
@UsePipes(new QueryValidationPipe())
@UseFilters(new ExceptionFilters())
public async getMethod(@Query() query: GetDto) {
(...)
}
And it works fine, for every request, pipe is logging test message to console. Now in controller B I'm using same decorator, but for method with underlying req and res object as parameters:
@Get()
@UsePipes(new QueryValidationPipe())
@UseFilters(new ExceptionFilters())
public async getMethodTo(@Req() req: GetRequest, @Res() res: Response): Promise<Response> {
(...)
}
Here it doesn't work. Pipe transform method is not called despite requests being received by above method. What can be cause of this? I didn't found any word in documentation about pipes not working when @Req and @Res parameters are used in method.
And if that's the case (it seems it is), how can I fix this? I can't just use @Query query method parameter because I need to send back different headers depending on query param.
Share Improve this question edited Jul 21, 2020 at 20:18 Furman asked Jul 21, 2020 at 20:09 FurmanFurman 2,4635 gold badges37 silver badges52 bronze badges1 Answer
Reset to default 5Pipes only work for @Body()
, @Param()
, @Query()
and custom decorators in the REST context. There's a brief mention here, but that should definitely get added to the docs.
Technically, you could make a custom decorator for req
and res
and get pipes to run for them.
export const ReqDec = createParamDecorator(
(data: unknown, ctx: ExecutionContext) => {
const request = ctx.switchToHttp().getRequest();
return request;
},
);
And then use it as @ReqDec()