From d45232b35e90198176965975f76ea786a59cc042 Mon Sep 17 00:00:00 2001 From: daishi Date: Mon, 16 Sep 2024 10:31:58 +0900 Subject: [PATCH] feat: export getHonoContext --- packages/waku/src/lib/hono/runner.ts | 14 ++++++++++++-- packages/waku/src/unstable_hono.ts | 2 +- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/packages/waku/src/lib/hono/runner.ts b/packages/waku/src/lib/hono/runner.ts index f0bbc4d72..c17836b6a 100644 --- a/packages/waku/src/lib/hono/runner.ts +++ b/packages/waku/src/lib/hono/runner.ts @@ -1,9 +1,10 @@ -import type { MiddlewareHandler } from 'hono'; +import type { Context, MiddlewareHandler } from 'hono'; +import { unstable_getCustomContext } from '../../server.js'; import { resolveConfig } from '../config.js'; import type { HandlerContext, MiddlewareOptions } from '../middleware/types.js'; -// Experimental Unstable API +// Internal context key const HONO_CONTEXT = '__hono_context'; const createEmptyReadableStream = () => @@ -13,6 +14,7 @@ const createEmptyReadableStream = () => }, }); +// Middleware runner (Is there a better name?) export const runner = (options: MiddlewareOptions): MiddlewareHandler => { const entriesPromise = options.cmd === 'start' @@ -68,3 +70,11 @@ export const runner = (options: MiddlewareOptions): MiddlewareHandler => { await next(); }; }; + +export const getHonoContext = () => { + const c = unstable_getCustomContext()[HONO_CONTEXT]; + if (!c) { + throw new Error('Hono context is not available'); + } + return c as C; +}; diff --git a/packages/waku/src/unstable_hono.ts b/packages/waku/src/unstable_hono.ts index f2c9e7626..66e6f2e58 100644 --- a/packages/waku/src/unstable_hono.ts +++ b/packages/waku/src/unstable_hono.ts @@ -1,6 +1,6 @@ // These exports are for internal use only and subject to change without notice. -export { runner } from './lib/hono/runner.js'; +export { runner, getHonoContext } from './lib/hono/runner.js'; export { Hono } from 'hono'; export { getRequestListener } from '@hono/node-server';