Skip to content

Commit 95170ad

Browse files
committed
docs: add @hoajs/request-id docs
1 parent d745d07 commit 95170ad

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

docs/middleware/request-id.md

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,55 @@
1-
Coming soon~
1+
## @hoajs/request-id
2+
3+
Generate and propagate a traceable Request ID for each request and response to help with log correlation, debugging, and cross-service tracing.
4+
5+
## Quick Start
6+
7+
```js
8+
import { Hoa } from 'hoa'
9+
import { requestId } from '@hoajs/request-id'
10+
11+
const app = new Hoa()
12+
app.use(requestId())
13+
14+
app.use(async (ctx) => {
15+
ctx.res.body = `Hello, ${ctx.state.requestId}!`
16+
})
17+
18+
export default app
19+
```
20+
21+
## Options
22+
23+
```ts
24+
interface RequestIdOptions {
25+
// Maximum length (default: 255)
26+
limitLength?: number
27+
// Response header name (default: 'X-Request-Id'); set '' to disable header read/write
28+
headerName?: string
29+
// Custom ID generator (default: crypto.randomUUID); ctx will be passed to the function
30+
generator?: (ctx: import('hoa').HoaContext) => string
31+
}
32+
```
33+
34+
- limitLength: Limits the maximum length of the request ID; if exceeded, a new ID will be generated.
35+
- headerName: The header name used to read/write the ID; pass an empty string '' to disable reading from the request header and writing the response header.
36+
- generator: Custom ID generation logic; receives ctx; defaults to `crypto.randomUUID()`.
37+
38+
## Examples
39+
40+
- Custom response header name:
41+
```js
42+
app.use(requestId({ headerName: 'X-Correlation-Id' }))
43+
```
44+
45+
- Disable reading request header and writing response header:
46+
```js
47+
app.use(requestId({ headerName: '' }))
48+
```
49+
50+
- Custom ID generator:
51+
```js
52+
app.use(requestId({
53+
generator: (ctx) => `${ctx.app.name}-${Date.now()}`
54+
}))
55+
```

0 commit comments

Comments
 (0)