How to use interceptors with services in nestjs

24 Views Asked by At

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
    }
}
1

There are 1 best solutions below

0
Micael Levi On BEST ANSWER

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.