Reputation: 233
I'm using in every Controller code like @UseInterceptors(ClassSerializerInterceptor)
so I decided make it global and was trying to setup it with no luck.
I was trying without and with new
and ended up with something like this totally not working.
app.useGlobalInterceptors(new ClassSerializerInterceptor(new Reflector()));
I checked NestJS source code and I assume that it cannot be used as global but it should.
Upvotes: 23
Views: 16560
Reputation: 21
Same With options:
import { ClassSerializerInterceptor, Module } from '@nestjs/common';
import { APP_INTERCEPTOR } from '@nestjs/core';
@Module({
providers: [
{
provide: APP_INTERCEPTOR,
inject: [Reflector],
useFactory: (reflector: Reflector) => {
return new ClassSerializerInterceptor(reflector, {
enableImplicitConversion: true,
excludeExtraneousValues: true,
});
},
},
],
})
export class AppModule {}
Upvotes: 2
Reputation: 833
I do prefer injecting global interceptors inside app.modules.ts
instead.
Global interceptors registered from outside of any module with useGlobalInterceptors()
cannot inject dependencies since this is done outside the context of any module.
import { ClassSerializerInterceptor, Module } from '@nestjs/common';
import { APP_INTERCEPTOR } from '@nestjs/core';
@Module({
providers: [
{
provide: APP_INTERCEPTOR,
useClass: ClassSerializerInterceptor,
},
],
})
export class AppModule {}
Reference:
How to use Service in Global-interceptor in NEST Js
https://docs.nestjs.com/interceptors#binding-interceptors
Upvotes: 24
Reputation: 3599
Have you tried using this line of code:
app.useGlobalInterceptors(new ClassSerializerInterceptor(app.get(Reflector)));
Upvotes: 63