import { randomUUID } from 'crypto'; import { Injectable, NestMiddleware } from '@nestjs/common'; import { NextFunction, Request, Response } from 'express'; export const REQUEST_ID_HEADER = 'x-request-id'; @Injectable() export class RequestIdMiddleware implements NestMiddleware { use(req: Request, res: Response, next: NextFunction) { const id = (req.headers[REQUEST_ID_HEADER] as string) || randomUUID(); req.headers[REQUEST_ID_HEADER] = id; res.setHeader(REQUEST_ID_HEADER, id); next(); } }