# nestjs-request-context

> Helper library for accessing request from singleton services

Latest version **4.0.0** (published 2025-03-16) · ISC license · 0 weekly downloads

## Install

```sh
npm install nestjs-request-context
pnpm add nestjs-request-context
yarn add nestjs-request-context
bun add nestjs-request-context
```

## Health

**Score 35/100 (D)** — status: maintenance-mode.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.0.0 |
| Published | 2025-03-16 |
| First published | 2020-09-27 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 9.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 73 |
| Author | Augusto Bonifacio |
| Maintainers | abonifacio |

## Links

- npm: https://www.npmjs.com/package/nestjs-request-context
- Repository: https://github.com/abonifacio/nestjs-request-context
- npm.io page: https://npm.io/package/nestjs-request-context

## Recent versions

- 4.0.0 (latest) — 2025-03-16
- 3.0.0 — 2023-07-08
- 2.1.0 — 2022-09-24
- 2.0.1 — 2022-07-21
- 2.0.0 — 2022-07-21
- 1.0.0 — 2021-10-23
- 0.1.0 — 2021-10-23
- 0.0.2 — 2020-09-27
- 0.0.1 — 2020-09-27

## README

# NestJS request-context

Workaround for getting Request information from a non request-scoped service in NestJs. Initially taken from this [gist](https://gist.github.com/bengry/924a9b93c25d8a98bffdfc0a847f0dbe), and then changed to use [AsyncLocalStorage](https://nodejs.org/api/async_context.html#async_context_new_asynclocalstorage)

# Usage 

Full example in `test` folder

```typescript
@Module({
  imports: [RequestContextModule], // automatically binds the proper middleware 
  providers: [...],
  controllers: [..],
})
export class AppModule {}
```

Accessing request from service
```typescript
@Injectable()
export class SingletonService {

  getRequestId() {
    const req: Request = RequestContext.currentContext.req;
    return req.requestId;
  }

}
```


# Release Notes

### 0.1.0

Updated peer dependencies to `8.x`

### 1.0.0

Use `async_hooks` instead `asyncctx` as it's deprecated

### 2.0.1

Updated NestJs peer dependencies to `9.x`

### 2.1.0

- Removed express from peer dependencies
- Added minimal generic support

### 3.0.0

Updated NestJs peer dependencies to `10.x`

### 4.0.0

Updated NestJs peer dependencies to `*`

---
_Source: https://npm.io/package/nestjs-request-context · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
