A plugin for Fastify that adds support for request-id
and session-id
.
Supports Fastify versions 4.x.
Support TypeScript
# npm
npm i @web-server-userland/fastify-request-id
# pnpm
pnpm add @web-server-userland/fastify-request-id
# yarn
yarn add @web-server-userland/fastify-request-id
const fastify = require('fastify')()
fastify.register(require('@web-server-userland/fastify-request-id'), {
// put your options here
})
fastify.get('/', (req, reply) => {
console.log(req.reqID, req.sesID, req.ids)
reply.send({ hello: 'world' }) // => response header has `x-request-id` and `x-session-id`
})
fastify.listen(3000)
You can use it as is without passing any option or you can configure it as explained below.
generateHash
: Generate hash. For example:
/**
* @params {"requestID" | "sessionID"} type - will generate type hash
*/
generateHash: (type) => {
if (type === "requestID") {
return `req${uuidv4()}`
}
if (type === "sessionID") {
return `ses${uuidv4()}`
}
}
-
requestIDName
: request id name - default:x-request-id
-
sessionIDName
: session id name - default:x-session-id
Licensed under MIT.