Why are global interceptors not functioning as expected?

22 Views Asked by At

Main.ts

import { NestFactory } from '@nestjs/core';
import { Transport } from '@nestjs/microservices';
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';
import { Logger } from 'nestjs-pino';
import { AppModule } from './app.module';
import { ConfigService } from './utils/config/config.service';

async function bootstrap() {
  const app = await NestFactory.create<NestFastifyApplication>(AppModule, new FastifyAdapter());
  app.enableCors({ credentials: true, origin: true });
  app.useLogger(app.get(Logger));
  app.useGlobalPipes(new ValidationPipe());
  app.connectMicroservice({
    transport: Transport.NATS,
    options: {
      servers: [process.env.NATS_SERVER_URL!],
      queue: 'PLAYER',
    },
  });

  const configService = app.get(ConfigService);

  await app.startAllMicroservices();
  await app.listen(configService.port, '0.0.0.0');
  console.log(`Gateway is running on: ${await app.getUrl()}`);
}
bootstrap();

Interceptor.module.ts

import { APP_INTERCEPTOR } from '@nestjs/core';
import { UuidInterceptor } from './uuid.interceptor';
import { RetryInterceptor } from './retry.interceptor';
import { TimeoutInterceptor } from './timeout.interceptor';
import { LoggerErrorInterceptor } from 'nestjs-pino';

@Module({
  providers: [
    {
      provide: APP_INTERCEPTOR,
      useClass: UuidInterceptor,
    },
    {
      provide: APP_INTERCEPTOR,
      useClass: RetryInterceptor,
    },
    {
      provide: APP_INTERCEPTOR,
      useClass: TimeoutInterceptor,
    },
    {
      provide: APP_INTERCEPTOR,
      useClass: LoggerErrorInterceptor,
    },
  ],
})
export class InterceptorsModule {}

I've created several interceptors and included them in the interceptors.module.ts, which I've imported into app.module.ts. However, when I make any requests, they don't seem to be called.

I tried to add interceptors specificly in main.ts in useGlobalInterceptors but i also didn't work

0

There are 0 best solutions below