Context manager for Express routing lib
npm install express-async-contextZero-dependency context-provision for express-application based on the AsyncLocalStorage.

   
- Installation
- Usage
- Motivation
- API Reference
``shell`
npm install express-async-context
`ts
import express from 'express';
import createContext from 'express-async-context';
const Context = createContext(req => ({
traceId: req.headers['x-request-id'] ?? Math.random().toFixed(20).slice(2),
}));
const app = express();
app.use(Context.provider);
app.get('/trace-id', Context.consumer(
(req, res) => ({ traceId }) => res.json({ traceId }),
));
app.listen(8080, () => {
console.log('Server is listening on port: 8080');
console.log('Follow: http://localhost:8080/trace-id');
});
`
`shell`
curl -H "X-Request-Id: 58895124899023443277" http://localhost:8080/trace-id
The express-async-context library is designed to aproach context provision to theexpress
chain of request handling in the -application without mutation of therequest or/and response.
Under the hood library uses AsyncLocalStorage
and is based on the thunk-idiom
that means calculation postponed until it will be provided with the context.
The main benifit of context we can get when we use IoC-container as a context.
To make such injection safe the static type-safe containers required, as instance:
true-di.
See Live DI Demo on Sandbox
- function createContext
- type ContextFactory
- interface ContextManager
- type HandlerThunk
- type ErrorHandlerThunk
- type Thunk
- type RunFn
`typescript`
Accepts contextFactory function and creates a ContextManager.
`ts`
The type describes function that accepts express.Request, express.Response and returns context data of any type T.
`ts`
interface ContextManager
provider: (req: express.Request, res: express.Response, next: express.NextFunction) => void;
consumer: {
(handler: express.RequestHandler | HandlerThunk
(handler: express.ErrorRequestHandler | ErrorHandlerThunk
}
The interface contains two members:
- provider - is an usual express middleware that creates context datacontextFactory
for each request using and "_binds_" this data to the request
- consumer - is a decorator for HandlerThunk and ErrorHandlerThunk that converts themexpress.RequestHandler
to usual and express.ErrorRequestHandler.
`ts`
(req: express.Request, res: express.Response, next: express.NextFunction) =>
(context: T, run: RunFn
The curried request handler that requires two-times application.
HandlerThunk could be considered as an express.RequestHandler Thunk
that returns a postponed handling of the request -- the
`ts`
(err: any, req: express.Request, res: express.Response, next: express.NextFunction) =>
(context: T, run: RunFn
The curried handler of error trhown during the request processing.
ErrorHandlerThunk could be considered as an express.ErrorRequestHandler thatThunk
returns a postponed handling of the error -- the
`ts`
(context: T, run: RunFn
The postponed calculation, including handler of the request or an error.
The correspondent function receives context data and the run-function,Thunk
that runs any other .
`ts`
Runs and injects the context data and itself to the postponed calculation
that accepts as a single argument.
RunFn` returns the result of execution of its argument-function.