Interceptors

There is no difference between regular interceptors and microservices interceptors. The following example uses a manually instantiated method-scoped interceptor. Just as with HTTP based applications, you can also use controller-scoped interceptors (i.e., prefix the controller class with a @UseInterceptors() decorator).

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