diff --git a/packages/express-http-context/src/browser.js b/packages/express-http-context/src/browser.js index 7d5e245..6e79ca6 100644 --- a/packages/express-http-context/src/browser.js +++ b/packages/express-http-context/src/browser.js @@ -4,6 +4,10 @@ export const middleware = (req, res, next) => { throw new Error("`middleware` cannot be called from the browser code."); }; +export const isolate = (fn) => { + throw new Error("`isolate` cannot be called from the browser code."); +}; + export const get = () => null; export const set = (key, value) => {}; diff --git a/packages/express-http-context/src/index.js b/packages/express-http-context/src/index.js index 17aea08..759955b 100644 --- a/packages/express-http-context/src/index.js +++ b/packages/express-http-context/src/index.js @@ -6,9 +6,12 @@ export const store = new AsyncLocalStorage(); /** Express.js middleware that is responsible for initializing the context for each request. */ export const middleware = (req, res, next) => { - store.run({}, () => { - next(); - }); + store.run({}, next); +}; + +/** Runs a given function in an isolated context */ +export const isolate = (fn) => { + store.run({}, fn); }; /** diff --git a/packages/express-http-context/types/index.d.ts b/packages/express-http-context/types/index.d.ts index 9705199..9e4ad1b 100644 --- a/packages/express-http-context/types/index.d.ts +++ b/packages/express-http-context/types/index.d.ts @@ -4,6 +4,9 @@ import { AsyncLocalStorage } from "async_hooks"; /** Express.js middleware that is responsible for initializing the context for each request. */ export declare function middleware(req: Request, res: Response, next: NextFunction): void; +/** Runs a given function in an isolated context */ +export declare function isolate(fn: Function): void; + /** * Gets a value from the context by key. Will return undefined if the context has not yet been initialized for this request or if a value is not found for the specified key. */