@web-server-userland/fastify-request-id
v1.0.1
Published
Fastify Request ID Plugin
Downloads
120
Maintainers
Readme
@web-server-userland/fastify-request-id
A plugin for Fastify that adds support for request-id
and session-id
.
Supports Fastify versions 4.x.
Support TypeScript
Install
# 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
Usage
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.
Options
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
License
Licensed under MIT.