Giter Club home page Giter Club logo

nestjs-request-id's Introduction

NestJS Request ID

A simple module to track the request flows in the application.

It adds X-Request-Id to the request/response header and provides the injectable service to see that unique ID everywhere.

Without this kind of method, for logging the request flows you should have passed the request object everywhere you want to log about.

Now, it just can be done by importing this module in the root application module, and injecting its service anywhere you want to see the unique request ID.

All of these could be done by nestjs-cls.

Requires

  • Node.js 16 or above
  • NestJS 9 or above

How to install

Install it using npm by the following command.

npm install nestjs-request-id

Usage

Import it to the root application module.

For an option, you can choose one of the followings. These options are defined as RequestIdFormatType, enum type.

  • RANDOM: Random unique string by nanoid package
  • UUID_V1: UUID v1 string by uuid package
  • UUID_V4: UUID v4 string by uuid package

You can omit passing the type. It uses UUID v4 method for the default unique ID format.

@Module({
  imports: [
    RequestIdModule.register({
      type: RequestIdFormatType.RANDOM,
      length: 10, // Length affects on RANDOM format type only
    }),
  ],
  ...
})
export class ApplicationModule {}

Inject RequestIdService using REQUEST_ID_TOKEN token.

@Injectable()
export class ApplicationService {
  constructor(
    @Inject(REQUEST_ID_TOKEN)
    private readonly requestIdService: RequestIdService,
  ) {}
  ...
}

And you can see the type and its unique ID using the following usage.

// To get the current format type
this.requestIdService.requestIdType;

// To get the length of the request ID in RANDOM format type
this.requestIdService.requestIdLength;

// To get its request ID
this.requestIdService.requestId;

License

It follows the MIT license.

nestjs-request-id's People

Contributors

awesometic avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

nestjs-request-id's Issues

Request scoped

Hi,

I stumbled upon your module while trying to solve a problem related to provider scope.

Wouldn't your request scoped RequestIdService make every provider that depend on it request scoped ?

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • chore(deps): update dependency @types/supertest to v2.0.16
  • chore(deps): update dependency ts-node to v10.9.2
  • fix(deps): update dependency reflect-metadata to ^0.2.0
  • fix(deps): update dependency rxjs to v7.8.1
  • fix(deps): update nest monorepo to v9.4.3 (@nestjs/common, @nestjs/core, @nestjs/platform-express, @nestjs/testing)
  • chore(deps): update dependency @types/node to v20
  • chore(deps): update dependency @types/supertest to v6
  • chore(deps): update dependency husky to v9
  • chore(deps): update dependency supertest to v7
  • chore(deps): update dependency typescript to v5
  • chore(deps): update lints (major) (eslint, eslint-config-prettier, eslint-plugin-prettier, eslint-plugin-unused-imports, prettier)
  • chore(deps): update typescript-eslint monorepo to v7 (major) (@typescript-eslint/eslint-plugin, @typescript-eslint/parser)
  • fix(deps): update dependency nestjs-cls to v4
  • fix(deps): update dependency uuid to v10
  • fix(deps): update nest monorepo to v10 (major) (@nestjs/common, @nestjs/core, @nestjs/platform-express, @nestjs/testing)
  • 🔐 Create all rate-limited PRs at once 🔐

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

npm
package.json
  • @nestjs/common ^9.0.0
  • @nestjs/core ^9.0.0
  • @nestjs/platform-express ^9.0.0
  • nestjs-cls ^3.6.0
  • reflect-metadata ^0.1.13
  • rxjs ^7.5.5
  • uuid ^9.0.1
  • @nestjs/testing ^9.0.0
  • @types/express ^4.17.13
  • @types/jest ^28.1.4
  • @types/node ^18.0.3
  • @types/supertest ^2.0.12
  • @types/uuid ^8.3.4
  • @typescript-eslint/eslint-plugin ^5.30.5
  • @typescript-eslint/parser ^5.30.5
  • eslint ^8.27.0
  • eslint-config-google ^0.14.0
  • eslint-config-prettier ^8.5.0
  • eslint-plugin-import ^2.26.0
  • eslint-plugin-prettier ^4.2.1
  • eslint-plugin-unused-imports ^2.0.0
  • husky ^8.0.1
  • jest ^28.1.2
  • prettier ^2.7.1
  • supertest ^6.2.4
  • ts-jest ^28.0.5
  • ts-loader ^9.3.1
  • ts-node ^10.8.2
  • typescript ^4.7.4

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.