diff --git a/src/shared/middleware/correlationId.ts b/src/shared/middleware/correlationId.ts index 9f30e0a2f8..a97f15bb6c 100644 --- a/src/shared/middleware/correlationId.ts +++ b/src/shared/middleware/correlationId.ts @@ -41,58 +41,3 @@ export function runWithCorrelation(correlationId, fn) { const id = correlationId || generateCorrelationId(); return correlationStore.run(id, fn); } - -/** - * Express/Next.js middleware that injects correlation IDs. - * - * Usage: - * // In Next.js middleware or Express app - * import { correlationMiddleware } from './correlationId.js'; - * app.use(correlationMiddleware); - * - * @param {Request} request - * @param {Function} next - * @returns {Promise} - */ -export function correlationMiddleware(request, next) { - const requestId = - request.headers.get("x-request-id") || - request.headers.get("x-correlation-id") || - generateCorrelationId(); - - return runWithCorrelation(requestId, async () => { - const response = await next(); - - // Attach correlation ID to response - if (response && response.headers) { - response.headers.set("x-request-id", requestId); - } - - return response; - }); -} - -/** - * Create a logger wrapper that automatically includes correlation IDs. - * - * @param {Object} baseLogger - Base logger with info/warn/error methods - * @returns {Object} Wrapped logger - */ -export function createCorrelatedLogger(baseLogger) { - const withCorrelation = (level, ...args) => { - const correlationId = getCorrelationId(); - if (correlationId) { - const meta = typeof args[args.length - 1] === "object" ? args.pop() : {}; - meta.correlationId = correlationId; - args.push(meta); - } - baseLogger[level](...args); - }; - - return { - info: (...args) => withCorrelation("info", ...args), - warn: (...args) => withCorrelation("warn", ...args), - error: (...args) => withCorrelation("error", ...args), - debug: (...args) => withCorrelation("debug", ...args), - }; -} diff --git a/tests/unit/observability-fase04.test.ts b/tests/unit/observability-fase04.test.ts index b5feed55b2..9a6ea846fe 100644 --- a/tests/unit/observability-fase04.test.ts +++ b/tests/unit/observability-fase04.test.ts @@ -171,7 +171,15 @@ test("requestTimeout: getProviderTimeout returns provider-specific value", () => // ─── Correlation ID Tests ──────────────────────────── -import { getCorrelationId, runWithCorrelation } from "../../src/shared/middleware/correlationId.ts"; +import * as correlationId from "../../src/shared/middleware/correlationId.ts"; +const { getCorrelationId, runWithCorrelation } = correlationId; + +test("correlationId: public surface excludes unused wrapper helpers", () => { + assert.equal(Object.hasOwn(correlationId, "correlationMiddleware"), false); + assert.equal(Object.hasOwn(correlationId, "createCorrelatedLogger"), false); + assert.equal(typeof correlationId.getCorrelationId, "function"); + assert.equal(typeof correlationId.runWithCorrelation, "function"); +}); test("correlationId: getCorrelationId returns undefined outside context", () => { assert.equal(getCorrelationId(), undefined);