I had already know we could create global interceptors from this code below:
import { Module } from '@nestjs/common';
import { APP_INTERCEPTOR } from '@nestjs/core';
@Module({
providers: [
{
provide: APP_INTERCEPTOR,
useClass: LoggingInterceptor,
},
],
})
export class AppModule {}
Source: documentation
However, what if I want to have let say, UserInterceptor
.UserInterceptor
will get user from database and transform the request.UserInterceptor
need to inject let say UserService
.
And I want to use UserInterceptor
globally.
@Injectable()
export class UserInterceptor {
constructor(private readonly service: UserService) {}
}
From documentation, we can't do app.useGlobalInterceptors(new UserInterceptor())
because UserInterceptor
need 1 argument in the constructor (UserService).
And since we had use APP_INTERCEPTOR
for LoggingInterceptor
, I didn't found another way to assign another value to APP_INTERCEPTOR
to use the interceptor globally.
For example I think the problem will solved if we could do:
providers: [
{
provide: APP_INTERCEPTOR,
useClass: [LoggingInterceptor, UserInterceptor]
}
]
providers: [
{
provide: APP_INTERCEPTOR,
useClass: LoggingInterceptor
},
{
provide: APP_INTERCEPTOR,
useClass: UserInterceptor
}
]
Just like this
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With