Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

nestjs-cls

Package Overview
Dependencies
Maintainers
1
Versions
52
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nestjs-cls

A continuation-local storage module compatible with NestJS's dependency injection.

  • 4.4.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
245K
decreased by-11.62%
Maintainers
1
Weekly downloads
 
Created

What is nestjs-cls?

The 'nestjs-cls' package provides a way to manage context-local storage in NestJS applications. It allows you to store and retrieve data that is scoped to the current request, making it useful for tasks like logging, tracing, and managing user sessions.

What are nestjs-cls's main functionalities?

Context Management

This feature allows you to set and get values within the context of a request. The 'ClsService' is used to manage context-local storage, making it easy to store and retrieve data that is specific to the current request.

const { ClsService } = require('nestjs-cls');

@Injectable()
export class MyService {
  constructor(private readonly cls: ClsService) {}

  doSomething() {
    this.cls.set('key', 'value');
    const value = this.cls.get('key');
    console.log(value); // Outputs: 'value'
  }
}

Middleware Integration

This feature demonstrates how to integrate 'ClsMiddleware' into your NestJS application. By applying this middleware, you ensure that context-local storage is available for all routes, making it easy to manage request-specific data throughout your application.

const { ClsMiddleware } = require('nestjs-cls');

@Module({
  providers: [ClsMiddleware],
})
export class AppModule {
  configure(consumer: MiddlewareConsumer) {
    consumer
      .apply(ClsMiddleware)
      .forRoutes('*');
  }
}

Async Context Management

This feature shows how to manage context-local storage in asynchronous operations. The 'run' method of 'ClsService' ensures that the context is preserved across asynchronous boundaries, allowing you to set and get values within async functions.

const { ClsService } = require('nestjs-cls');

@Injectable()
export class MyService {
  constructor(private readonly cls: ClsService) {}

  async doSomethingAsync() {
    await this.cls.run(async () => {
      this.cls.set('key', 'value');
      const value = this.cls.get('key');
      console.log(value); // Outputs: 'value'
    });
  }
}

Other packages similar to nestjs-cls

Keywords

FAQs

Package last updated on 06 Aug 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc