A simple module to track the request flows in the application.
It adds X-Request-Id
to the request/response header and provides the injectable service to see that unique ID everywhere.
Without this kind of method, for logging the request flows you should have passed the request object everywhere you want to log about.
Now, it just can be done by importing this module in the root application module, and injecting its service anywhere you want to see the unique request ID.
All of these could be done by nestjs-cls
.
Install it using npm
by the following command.
npm install nestjs-request-id
Import it to the root application module.
For an option, you can choose one of the followings. These options are defined as RequestIdFormatType
, enum
type.
RANDOM
: Random unique string by nanoid
packageUUID_V1
: UUID v1 string by uuid
packageUUID_V4
: UUID v4 string by uuid
packageYou can omit passing the type. It uses UUID v4 method for the default unique ID format.
@Module({
imports: [
RequestIdModule.register({
type: RequestIdFormatType.RANDOM,
length: 10, // Length affects on RANDOM format type only
}),
],
...
})
export class ApplicationModule {}
Inject RequestIdService
using REQUEST_ID_TOKEN
token.
@Injectable()
export class ApplicationService {
constructor(
@Inject(REQUEST_ID_TOKEN)
private readonly requestIdService: RequestIdService,
) {}
...
}
And you can see the type and its unique ID using the following usage.
// To get the current format type
this.requestIdService.requestIdType;
// To get the length of the request ID in RANDOM format type
this.requestIdService.requestIdLength;
// To get its request ID
this.requestIdService.requestId;
It follows the MIT license.