File size: 451 Bytes
347f81b
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import type { Context, Next } from "hono";
import { randomUUID } from "crypto";

/**
 * Middleware that generates a unique request ID for each request.
 * Sets X-Request-Id response header and stores it in c.set() for logging.
 */
export async function requestId(c: Context, next: Next): Promise<void> {
  const id = c.req.header("x-request-id") ?? randomUUID().slice(0, 8);
  c.set("requestId", id);
  c.header("X-Request-Id", id);
  await next();
}