async-hooks-state
v0.0.6
Published
A (very) simple AsyncLocalStorage wrapper/middleware
Downloads
7
Maintainers
Readme
async-hooks-state
A (very) simple AsyncLocalStorage wrapper & expressjs
middleware.
Install
npm i async-hooks-state
Usage
expressjs
example:
import {hooked, getStorage, getRequest} from 'async-hooks-state';
import {NextFunction, Request, Response} from 'express';
const app = express();
const optionalContext = { foo: 'bar' };
app.use(hooked(optionalContext));
// later, in any async context
const route = (req: Request, res: Response, next: NextFunction) => {
const r = getRequest();
assert(r === req);
assert(getStorage().getStore().get('foo') === 'bar');
}
API
hooked(defaultContext: Record<string | number | symbol, unknown> = {})
getStorage()
getRequest()
getResponse()