apps/gateway/src/middleware/correlation-id.ts
Metadata
- Purpose: Gateway middleware for cross-cutting concerns.
- Domain:
applications - Language:
ts - Bytes: 578
- Lines: 16
- Content hash (short):
6577f91c - Source (start): apps/gateway/src/middleware/correlation-id.ts:1
- Source (end): apps/gateway/src/middleware/correlation-id.ts:16
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);
});
};