-
Notifications
You must be signed in to change notification settings - Fork 4
/
request-id.js
35 lines (31 loc) · 985 Bytes
/
request-id.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
'use strict'
const uuid = require('uuid')
/**
* Generates a unique request ID for all requests, setting it as `ctx.id`, `ctx.request.id` and
* `ctx.state.requestId`. It will also set the `X-Request-Id` header to aid clients with debugging.
*
* If there is an `X-Request-Id` header from client, this will be set as `ctx.state.clientRequestId`
* and as the header `X-Client-Request-Id`.
*
* @example
* ctx.headers
* // { `x-request-id`: '72243aca-e4bb-4a3a-a2e7-ed380c256826' }
*
* ctx.state
* // { requestId: '72243aca-e4bb-4a3a-a2e7-ed380c256826' }
*
* @returns {Promise}
*/
module.exports = function requestId (ctx, next) {
let requestId = uuid.v4()
let clientId = ctx.get('X-Request-Id')
if (clientId) {
ctx.state.clientRequestId = clientId
ctx.set('X-Client-Request-Id', ctx.state.clientRequestId)
}
ctx.id = requestId
ctx.request.id = requestId
ctx.state.requestId = requestId
ctx.set('X-Request-Id', requestId)
return next()
}