changemaker.lite/api/src/middleware/correlation-id.ts
bunker-admin 39d74e7b85 Add guided tour, media enhancements, error handling, and DevOps improvements
Major additions: onboarding tour system, correlation-id middleware, media
error handler, restore script, env validation script, Dockerignore files.
Updates across 70+ admin components for improved UX and error handling.

Bunker Admin
2026-03-26 10:31:51 -06:00

17 lines
611 B
TypeScript

import { randomUUID } from 'crypto';
import { Request, Response, NextFunction } from 'express';
const CORRELATION_HEADER = 'x-request-id';
/**
* Middleware that assigns a unique correlation ID to each request.
* Uses the incoming x-request-id header if present, otherwise generates a new UUID.
* Sets the correlation ID on both the request object and response header.
*/
export function correlationId(req: Request, res: Response, next: NextFunction) {
const id = (req.headers[CORRELATION_HEADER] as string) || randomUUID();
req.correlationId = id;
res.setHeader(CORRELATION_HEADER, id);
next();
}