Search code examples
servicenestjsinterceptor

How to use interceptors with services in nestjs


How can i use interceptors with services?

I tried use it like with controllers but it didnt work.

import { 
    Injectable,
    UseInterceptors, 
} from '@nestjs/common';

import MyInterceptor from '...'

@Injectable()
export class Service {
    @UseInterceptors(MyInterceptor)
    public anyFunc() {
       // logic
    }
}

Solution

  • you can't. That's not a feature.

    Interceptors were designed to enhance controllers (+ graphql stuff), not providers. That's why the binding interceptors section in the docs only mention controllers.