How to intercept specific request

Viewed 183

I'm using NestJS and Angular 2, both have similar (close) approach to work with Interceptors. I would like to find best practice to identify some specific request to do some additional work.

To declare Interceptor who will listen some Controller (in NestJS) I should use this logic:

@UseInterceptors(ObjectsInterceptor)
@Controller('objects')
export class ObjectsController {

  @Get()
  async findAll(): Promise<ObjectDto[]> {
    // Request which should be intercepted
    ...
  }

  @Get(':slug')
  async findOne(@Params('slug') slug: string): Promise<ObjectDto> {
    // Request which shouldn't be intercepted 
    ...
  }
}

In Interceptor:

@Injectable()
export class ObjectsInterceptor implements NestInterceptor {

  intercept(context: ExecutionContext, next: CallHandler<any>): Observable<any> {
    // Some logic to detect specific request
    return next.handle();
  }
}

Probably I use wrong way to solve my problem

1 Answers

just put decorator above method instead of class

  @Get()
  @UseInterceptors(ObjectsInterceptor)
  async findAll(): Promise<ObjectDto[]> {
    // Request which should be intercepted
    ...
  }
Related