@web-server-userland/fastify-request-id

Fastify Request ID Plugin


Keywords
fastify, web-server-userland, headers, request-id, fastify-plugin, javascript, nodejs
License
MIT
Install
npm install @web-server-userland/fastify-request-id@1.0.1

Documentation

@web-server-userland/fastify-request-id

CI NPM version js-standard-style NPM size Coverage Status

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.