awesometic / nestjs-request-id

A NestJS module for tracking the request flows by adding unique request ID
MIT License
5 stars 1 forks source link

no headers added to response #13

Closed vientorepublic closed 9 months ago

vientorepublic commented 9 months ago

NestJS Version: 10.0.0

After applying the module to AppModule:

스크린샷 2023-11-30 오전 8 29 04

Try adding X-Request-Id header in middleware:

스크린샷 2023-11-30 오전 8 30 24 스크린샷 2023-11-30 오전 8 33 36
awesometic commented 9 months ago

Hello,

How did you apply this module to the AppModule? Can you paste the code block here partly?

vientorepublic commented 9 months ago

I applied it to the imports of the module decorator as follows:

import { LoggerMiddleware } from './middleware/logger.middleware';
import { RequestIdFormatType, RequestIdModule } from 'nestjs-request-id';

@Module({
  imports: [
    RequestIdModule.register({
      type: RequestIdFormatType.UUID_V4,
    }),
    // other modules...
  ],
  controllers: [],
  providers: [],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(LoggerMiddleware).forRoutes('*');
  }
}
awesometic commented 9 months ago

Thanks,

If I understand right, you can trace the request in the NestJS life cycle but can't see that identifier in the received response. I will patch that to add the same header to the response soon.

Thanks for the feedback :)

vientorepublic commented 9 months ago

Is it possible to set a header using setHeader among the response parameter methods in requestid.middleware.ts?

like this:

switch (this.requestIdService.requestIdType) {
  case RequestIdFormatType.RANDOM:
    res.setHeader(REQUEST_ID_HEADER, await nanoid(givenLength));
    break;
  case RequestIdFormatType.UUID_V1:
    res.setHeader(REQUEST_ID_HEADER, uuid.v1());
    break;
  case RequestIdFormatType.UUID_V4:
    res.setHeader(REQUEST_ID_HEADER, uuid.v4());
    break;
  default:
    // Throw an error if the given format type is not supported
    throw new Error(
      `Unsupported request ID format type: ${this.requestIdService.requestIdType}`,
    );
}
awesometic commented 9 months ago

Maybe yes, you can try it on your side and share the results :)