請求 ID 中介軟體
請求 ID 中介軟體會為每個請求產生一個唯一的 ID,您可以在處理程序中使用它。
導入
ts
import { Hono } from 'hono'
import { requestId } from 'hono/request-id'
用法
您可以透過套用請求 ID 中介軟體的處理程序和中介軟體中的 requestId
變數來存取請求 ID。
ts
const app = new Hono()
app.use('*', requestId())
app.get('/', (c) => {
return c.text(`Your request id is ${c.get('requestId')}`)
})
如果您想明確指定類型,請導入 RequestIdVariables
並將其傳遞到 new Hono()
的泛型中。
ts
import type { RequestIdVariables } from 'hono/request-id'
const app = new Hono<{
Variables: RequestIdVariables
}>()
選項
選填 limitLength: number
請求 ID 的最大長度。預設值為 255
。
選填 headerName: string
用於請求 ID 的標頭名稱。預設值為 X-Request-Id
。
選填 generator: (c: Context) => string
請求 ID 生成函數。預設情況下,它使用 crypto.randomUUID()
。