Gathering detailed insights and metrics for @medibloc/nestjs-request-context
Gathering detailed insights and metrics for @medibloc/nestjs-request-context
Gathering detailed insights and metrics for @medibloc/nestjs-request-context
Gathering detailed insights and metrics for @medibloc/nestjs-request-context
NestJS Request Context using AsyncLocalStorage
npm install @medibloc/nestjs-request-context
Typescript
Module System
Min. Node Version
TypeScript (86.25%)
JavaScript (13.75%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
46 Stars
6 Commits
4 Forks
3 Watchers
2 Branches
2 Contributors
Updated on Feb 07, 2025
Latest Version
1.0.1
Package Id
@medibloc/nestjs-request-context@1.0.1
Unpacked Size
14.94 kB
Size
5.29 kB
File Count
20
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
A NestJS module that stores any type of data in the request-scope and makes it accessible from any layer, such as singleton service layers or even repository layers.
Since AsyncLocalStorage is used internally, the required Node.js version is >=14.15.2 that includes significant fixes.
1yarn add '@medibloc/nestjs-request-context'
There are several ways to enable the request context.
Extend RequestContext
to include any data you want.
1import { RequestContext } from '@medibloc/nestjs-request-context'; 2 3export class MyRequestContext extends RequestContext { 4 actor: string; 5}
Import RequestContextModule
to your AppModule
with specifying the context class that you defined above.
It automatically registers a middleware that initialize a context for each request (for all route path *
).
1import { RequestContextModule } from '@medibloc/nestjs-request-context'; 2import { MyRequestContext } from './my-context.model'; 3 4@Module({ 5 imports: [ 6 RequestContextModule.forRoot({ 7 contextClass: MyRequestContext, 8 isGlobal: true, 9 }) 10 ] 11}) 12export class AppModule {}
If isGlobal
is true
, you will not need to import RequestContextModule
in other modules once it's been loaded in the root module.
Now, you can access the request context from any Controller/Resolver/Service/Repository layers.
1import { RequestContext } from '@medibloc/nestjs-request-context'; 2import { MyRequestContext } from './my-context.model'; 3 4@Controller('') 5export class MyController { 6 @Get() 7 test(): string { 8 const ctx: MyRequestContext = RequestContext.get(); 9 ctx.actor = 'Jack'; 10 11 return this.myService.test(); 12 } 13} 14 15@Injectable() 16export class MyService { 17 test(): string { 18 const ctx: MyRequestContext = RequestContext.get(); 19 console.log(`current actor: ${ctx.actor}`); 20 21 return ctx.actor; 22 } 23}
If you prefer to use the functional middleware rather than import the module RequestContextModule
,
you can use the requestContextMiddleware
function by passing the context class that you want.
In this case, you don't need to import the RequestContextModule
to the AppModule
.
1import { requestContextMiddleware } from '@medibloc/nestjs-request-context'; 2import { MyRequestContext } from './my-context.model'; 3 4const app = await NestFactory(AppModule); 5... 6app.use(requestContextMiddleware(MyRequestContext)); 7 8await app.listen(3000);
Later steps are the same as in the section above.
If you use GraphQL Subscriptions, the request context middleware will not be executed automatically, because NestJS middlewares are not for Websocket connections.
If you want to initialize the request context for each Websocket connection,
please set the onConnect
option like below.
In this case, you don't need to import the RequestContextModule
to the AppModule
or use the requestContextMiddleware
function.
1@Module({ 2 GraphQLModule.forRoot({ 3 installSubscriptionHandlers: true, 4 subscriptions: { 5 onConnect: () => RequestContext.start(PaletteRequestContext), 6 }, 7 }), 8}) 9export class AppModule{}
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
Found 2/6 approved changesets -- score normalized to 3
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
46 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More