Skip to content

Export asyncLocalStorage to allow running it in another place#213

Closed
Floriferous wants to merge 1 commit intofastify:mainfrom
Floriferous:patch-1
Closed

Export asyncLocalStorage to allow running it in another place#213
Floriferous wants to merge 1 commit intofastify:mainfrom
Floriferous:patch-1

Conversation

@Floriferous
Copy link
Contributor

@Floriferous Floriferous commented Feb 7, 2025

Exporting it should allow developers to write tests where you can set the context artificially.

import { asyncLocalStorage } from '@fastify/request-context';

it('should set request context', () => {
  asyncLocalStorage.run({}, async () => {
    requestContext.set('userId', 'some-fake-user-id');
    someCodeThatUsesRequestContext(); // requestContext.get('userId') will work
  })
})

Closes #211

Signed-off-by: Florian Bienefelt <florian.bienefelt@gmail.com>
module.exports.default = fastifyRequestContext
module.exports.fastifyRequestContext = fastifyRequestContext

module.exports.asyncLocalStorage = asyncLocalStorage
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add documentation with example usage (can be taken from your PR description)?

module.exports.default = fastifyRequestContext
module.exports.fastifyRequestContext = fastifyRequestContext

module.exports.asyncLocalStorage = asyncLocalStorage
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TS types need to be updated

@kibertoad
Copy link
Member

@Floriferous do you need any help finishing this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow setting requestContext outside of a fastify route

2 participants