Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export asyncLocalStorage to allow running it in another place #213

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Floriferous
Copy link

@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

@@ -63,7 +63,7 @@ module.exports = fp(fastifyRequestContext, {
})
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)?

@@ -63,7 +63,7 @@ module.exports = fp(fastifyRequestContext, {
})
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