Skip to content

Commit 513efc5

Browse files
Add docs and types for exposing asyncLocalStorage instance (#220)
Signed-off-by: Florian Bienefelt <[email protected]> Signed-off-by: Florian Bienefelt <[email protected]> Co-authored-by: Florian Bienefelt <[email protected]>
1 parent 2a4070d commit 513efc5

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,20 @@ const bar = requestContext.get('bar')
149149

150150
If you have `"strictNullChecks": true` (or have `"strict": true`, which sets `"strictNullChecks": true`) in your TypeScript configuration, you will notice that the type of the returned value can still be `undefined` even though the `RequestContextData` interface has a specific type. For a discussion about how to work around this and the pros/cons of doing so, please read [this issue (#93)](https://github.com/fastify/fastify-request-context/issues/93).
151151

152+
## Usage outside of a request
153+
154+
If functions depend on requestContext but are not called in a request, i.e. in tests or workers, they can be wrapped in the asyncLocalStorage instance of requestContext:
155+
156+
```
157+
import { asyncLocalStorage } from '@fastify/request-context';
158+
159+
it('should set request context', () => {
160+
asyncLocalStorage.run({}, async () => {
161+
requestContext.set('userId', 'some-fake-user-id');
162+
someCodeThatUsesRequestContext(); // requestContext.get('userId') will work
163+
})
164+
})
165+
```
152166

153167
## License
154168

index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ module.exports = fp(fastifyRequestContext, {
6666
})
6767
module.exports.default = fastifyRequestContext
6868
module.exports.fastifyRequestContext = fastifyRequestContext
69-
69+
module.exports.asyncLocalStorage = asyncLocalStorage
7070
module.exports.requestContext = requestContext
7171

7272
// Deprecated

types/index.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AsyncResource } from 'node:async_hooks'
1+
import { AsyncLocalStorage, AsyncResource } from 'node:async_hooks'
22
import { FastifyPluginCallback, FastifyRequest } from 'fastify'
33

44
type FastifyRequestContext =
@@ -54,7 +54,7 @@ declare namespace fastifyRequestContext {
5454
}
5555

5656
export const requestContext: RequestContext
57-
57+
export const asyncLocalStorage: AsyncLocalStorage<RequestContext>
5858
/**
5959
* @deprecated Use FastifyRequestContextOptions instead
6060
*/

types/index.test-d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import {
22
requestContext,
3+
asyncLocalStorage,
34
fastifyRequestContext,
45
FastifyRequestContextOptions,
56
RequestContext,
67
RequestContextDataFactory,
78
} from '..'
89
import { expectAssignable, expectType, expectError } from 'tsd'
910
import { FastifyBaseLogger, FastifyInstance, RouteHandlerMethod } from 'fastify'
11+
import { AsyncLocalStorage } from 'node:async_hooks'
1012

1113
const fastify = require('fastify')
1214

@@ -61,6 +63,7 @@ expectError<RequestContextDataFactory>(() => ({ log: 'dummy' }))
6163

6264
expectType<RequestContext>(app.requestContext)
6365
expectType<RequestContext>(requestContext)
66+
expectType<AsyncLocalStorage<RequestContext>>(asyncLocalStorage)
6467

6568
const getHandler: RouteHandlerMethod = function (request, _reply) {
6669
expectType<RequestContext>(request.requestContext)

0 commit comments

Comments
 (0)