async-hooks-state
TypeScript icon, indicating that this package has built-in type declarations

0.0.6 • Public • Published

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()

Package Sidebar

Install

npm i async-hooks-state

Weekly Downloads

0

Version

0.0.6

License

MIT

Unpacked Size

8.87 kB

Total Files

13

Last publish

Collaborators

  • xprmntl