Pipes

There is no fundamental difference between regular pipes and microservices pipes. The only difference is that instead of throwing HttpException, you should use RpcException.

info Hint The RpcException class is exposed from @nestjs/microservices package.

Binding pipes

The following example uses a manually instantiated method-scoped pipe. Just as with HTTP based applications, you can also use controller-scoped pipes (i.e., prefix the controller class with a @UsePipes() decorator).

  1. @@filename()
  2. @UsePipes(new ValidationPipe())
  3. @MessagePattern({ cmd: 'sum' })
  4. accumulate(data: number[]): number {
  5. return (data || []).reduce((a, b) => a + b);
  6. }
  7. @@switch
  8. @UsePipes(new ValidationPipe())
  9. @MessagePattern({ cmd: 'sum' })
  10. accumulate(data) {
  11. return (data || []).reduce((a, b) => a + b);
  12. }