npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@lasnutrias/nestjs-request-scope

v1.2.0

Published

Creates a request scope for nestjs projects

Downloads

87

Readme

NestJS Request Scope

A lightweight and powerful request-scoping utility for NestJS that allows you to store and retrieve values within the lifecycle of a single HTTP request using asynchronous local storage.

Installation

pnpm add @lasnutrias/nestjs-request-scope

Features

  • Request Isolation: Store data that is only accessible during the current request.
  • Easy Integration: Global or feature-based registration.
  • Type Safety: Full TypeScript support for stored values.
  • Middleware Powered: Automatically handles the scope lifecycle.

Usage

1. Root Registration (forRoot)

Register the module in your AppModule. This will automatically set up the internal middleware to manage the scope for all routes.

Standard Registration

In this mode, you need to import RequestScopeModule.forFeature() in other modules to access the service.

import { RequestScopeModule } from 'nestjs-request-scope';

@Module({
  imports: [
    RequestScopeModule.forRoot(),
  ],
})
export class AppModule {}

Global Registration

By setting isGlobal: true, the RequestScopeService becomes available throughout your entire application without needing to import it in other modules.

@Module({
  imports: [
    RequestScopeModule.forRoot({ isGlobal: true }),
  ],
})
export class AppModule {}

2. Feature Registration (forFeature)

If you didn't register the module as global, use forFeature() in any module where you need to inject the service. This ensures the service is available without re-configuring the middleware.

@Module({
  imports: [RequestScopeModule.forFeature()],
  controllers: [UserController],
  providers: [UserService],
})
export class UserModule {}

3. Injecting and Using the Service

You can inject the RequestScopeService into your controllers, services, or guards to store and retrieve request-specific data.

import { Injectable, Controller, Get } from '@nestjs/common';
import { RequestScopeService } from '@lasnutrias/nestjs-request-scope';

@Injectable()
export class UserService {
  constructor(private readonly requestScope: RequestScopeService) {}

  async processUserData() {
    // Reading from the scope
    const correlationId = this.requestScope.get<string>('correlation-id');
    
    // ... logic ...
    
    return { id: '123', trace: correlationId };
  }
}

@Controller('users')
export class UserController {
  constructor(private readonly requestScope: RequestScopeService) {}

  @Get()
  getUsers() {
    // Writing to the scope
    this.requestScope.set('correlation-id', 'abc-123-unique-id');
    
    return this.userService.processUserData();
  }
}

4. Parameter Decorator (@ScopedValue)

A convenient way to extract values from the scope directly in your controllers.

@Get('profile')
getProfile(@ScopedValue('tenant-id') tenantId: string) {
  return { tenantId };
}

API Reference

RequestScopeService

| Method | Description | | --- | --- | | get<V>(key: string): V | undefined | Retrieves a value from the current request scope. | | set<V>(key: string, value: V): void | Stores a value in the current request scope. |

License

Apache-2.0