Skip to main content

apps/gateway/src/middleware/correlation-id.ts

Metadata

Indexed Symbols

No indexed functions/methods detected in this file.

Markdown Headings (if applicable)

No markdown headings detected.

Source Preview

import { randomUUID } from "node:crypto";
import type { FastifyPluginAsync } from "fastify";
import { ensureUuid } from "../utils/uuid.js";

const CORRELATION_HEADER = "x-correlation-id";

export const correlationIdPlugin: FastifyPluginAsync = async (app) => {
app.addHook("onRequest", async (request, reply) => {
const incoming = request.headers[CORRELATION_HEADER] as string | undefined;
const correlationId = ensureUuid(incoming, randomUUID());

request.headers[CORRELATION_HEADER] = correlationId;
reply.header(CORRELATION_HEADER, correlationId);
});
};