index.ts
This file
Path
src/middleware/context-storage/index.ts
59 lines 路 1.3 KiB 路 TypeScript
Last changed by
docs(context-storage): fix JSDoc (#5086)Yusuke Wadabefore adoptiond6b1d32
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960/** * @module * Context Storage Middleware for Hono. */
import { AsyncLocalStorage } from 'node:async_hooks'import type { Context } from '../../context'import type { Env, MiddlewareHandler } from '../../types'
const asyncLocalStorage = new AsyncLocalStorage<Context>()
/** * Context Storage Middleware for Hono. * * @see {@link https://hono.dev/docs/middleware/builtin/context-storage} * * @returns {MiddlewareHandler} The middleware handler function. * * @example * ```ts * type Env = { * Variables: { * message: string * } * } * * const app = new Hono<Env>() * * app.use(contextStorage()) * * app.use(async (c, next) => { * c.set('message', 'Hono is hot!!') * await next() * }) * * app.get('/', async (c) => c.text(getMessage())) * * const getMessage = () => { * return getContext<Env>().var.message * } * ``` */export const contextStorage = (): MiddlewareHandler => { return async function contextStorage(c, next) { await asyncLocalStorage.run(c, next) }}
export const tryGetContext = <E extends Env = Env>(): Context<E> | undefined => { return asyncLocalStorage.getStore() as Context<E> | undefined}
export const getContext = <E extends Env = Env>(): Context<E> => { const context = tryGetContext<E>() if (!context) { throw new Error('Context is not available') } return context}