Không có sự khác biệt nào giữa interceptor thông thường và interceptor của microservices. Ví dụ sau dùng một interceptor method-scoped được khởi tạo thủ công. Giống như với ứng dụng dựa trên HTTP, bạn cũng có thể dùng interceptor controller-scoped (nghĩa là, thêm decorator @UseInterceptors() lên đầu class controller).
@UseInterceptors(new TransformInterceptor())
@MessagePattern({ cmd: 'sum' })
accumulate(data: number[]): number {
return (data || []).reduce((a, b) => a + b);
}
@UseInterceptors(new TransformInterceptor())
@MessagePattern({ cmd: 'sum' })
accumulate(data) {
return (data || []).reduce((a, b) => a + b);
}