request-id.ts16 lines · main
| 1 | import type { MiddlewareHandler } from 'hono'; |
| 2 | |
| 3 | import { newId } from '@briven/shared'; |
| 4 | |
| 5 | /** |
| 6 | * Attach a ULID request id to every request. Forwarded downstream via the |
| 7 | * `x-request-id` response header so logs, traces, and customer bug reports |
| 8 | * share one key. |
| 9 | */ |
| 10 | export const requestId = (): MiddlewareHandler => async (c, next) => { |
| 11 | const incoming = c.req.header('x-request-id'); |
| 12 | const id = incoming && incoming.length <= 64 ? incoming : newId('ev'); |
| 13 | c.set('requestId', id); |
| 14 | c.header('x-request-id', id); |
| 15 | await next(); |
| 16 | }; |