@remix-run/async-context-middleware
v0.1.2
Published
Middleware for storing request context in AsyncLocalStorage
Maintainers
Readme
async-context-middleware
Middleware for storing request context in AsyncLocalStorage for use with @remix-run/fetch-router.
This middleware stores the request context in AsyncLocalStorage (using node:async_hooks), making it available to all functions in the same async execution context.
Installation
npm install @remix-run/async-context-middlewareUsage
Simply use the asyncContext() middleware at the router level to make the request context available to all functions in the same async execution context. Get access to the context using the getContext() function.
import { createRouter } from '@remix-run/fetch-router'
import { asyncContext, getContext } from '@remix-run/async-context-middleware'
let router = createRouter({
middleware: [asyncContext()],
})
router.get('/users/:id', async () => {
// Access context from anywhere in the async call stack
let context = getContext()
let userId = context.params.id
return new Response(`User ${userId}`)
})Note: This middleware requires support for node:async_hooks.
Related Packages
fetch-router- Router for the web Fetch API
License
See LICENSE
