Closed vientorepublic closed 9 months ago
Hello,
How did you apply this module to the AppModule? Can you paste the code block here partly?
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('*');
}
}
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 :)
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}`,
);
}
Maybe yes, you can try it on your side and share the results :)
NestJS Version: 10.0.0
After applying the module to AppModule:
Try adding X-Request-Id header in middleware: