request-id.middleware.ts
Gives every request a correlation id so logs, errors, and rate-limit events can be tied back to a single client call.
What it does
- Looks at the incoming
X-Request-Id(orX-Request-ID) header. - If the header is missing, empty, or longer than 128 chars → generates a UUID (
crypto.randomUUID()). - Stores the chosen id on
c.set('requestId', ...)so the rest of the stack can read it. - Echoes the same value back on the response as
X-Request-Idafter the handler finishes.
c.get('requestId') // string — always available downstreamWhere it is mounted
In src/index.ts, before any route, so every endpoint (including /health, /ready, /internal/*) gets an id:
app.use('*', requestIdMiddleware)Who consumes the id
- The global error handler (
app.onError) attaches the id to the JSON error response so users can quote it to support. rate-limit.middleware.tswrites it on everyRateLimitEventand on the429body.- Structured logs in
lib/logger.tsinclude it for grep-able traces.
CORS exposure
The id is added to exposeHeaders in the CORS config in index.ts, so browser code (extension, vite-frontend, admin-frontend) can read it from Response.headers.get('X-Request-Id').
Last updated on