UNPKG

@nextwrappers/async-local-storage

Version:

Middleware wrapper for Next.js Route Handlers that executes requests within an AsyncLocalStorage context.

25 lines (21 loc) 1.04 kB
import { AsyncLocalStorage } from 'node:async_hooks'; type DefaultExt = { params?: unknown; }; /** * Creates an async local storage wrapper for a route handler * @param options The options including an optional async local `storage` * instance and an `initialize` function which receives the request * and returns the `store` * @returns */ declare function asyncLocalStorage<Store>(options: AsyncLocalStorageWrapperOptions<Store>): { storage: AsyncLocalStorage<Store>; getStore: () => Store | undefined; wrapper: <HExt extends DefaultExt = DefaultExt, HReq extends Request = Request, HRes extends Response | Promise<Response> = Response>(handler: (req: HReq & Request, ext?: HExt | undefined) => HRes) => (req: HReq & Request, ext?: HExt | undefined) => HRes; }; type AsyncLocalStorageWrapperOptions<Store> = { storage?: AsyncLocalStorage<Store>; initialize?: <Req extends Request, Ext extends DefaultExt>(req: Req, ext?: Ext) => Store; }; export { AsyncLocalStorageWrapperOptions, asyncLocalStorage };