https://github.com/web-server-userland/fastify-request-id
Fastify Request ID Plugin
https://github.com/web-server-userland/fastify-request-id
fastify javascript nodejs
Last synced: about 1 year ago
JSON representation
Fastify Request ID Plugin
- Host: GitHub
- URL: https://github.com/web-server-userland/fastify-request-id
- Owner: web-server-userland
- License: mit
- Created: 2022-07-07T02:57:44.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2022-10-21T03:48:23.000Z (over 3 years ago)
- Last Synced: 2025-03-27T23:33:48.300Z (about 1 year ago)
- Topics: fastify, javascript, nodejs
- Language: JavaScript
- Homepage:
- Size: 64.5 KB
- Stars: 4
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @web-server-userland/fastify-request-id

[](https://www.npmjs.com/package/@web-server-userland/fastify-request-id)
[](https://standardjs.com/)
[](https://www.npmjs.com/package/@web-server-userland/fastify-request-id)
[](https://coveralls.io/github/web-server-userland/fastify-request-id?branch=main)
A plugin for Fastify that adds support for `request-id` and `session-id`.
Supports Fastify versions 4.x.
> Support TypeScript
## Install
```shell
# 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
```JavaScript
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:
```javascript
/**
* @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](./LICENSE).