Skip to content

Commit

Permalink
feat: export getHonoContext (#884)
Browse files Browse the repository at this point in the history
following #852, this should be more explicit. Still unstable, but less
experimental.
  • Loading branch information
dai-shi authored Sep 16, 2024
1 parent ebae041 commit 232a3a5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
14 changes: 12 additions & 2 deletions packages/waku/src/lib/hono/runner.ts
Original file line number Diff line number Diff line change
@@ -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 = () =>
Expand All @@ -13,6 +14,7 @@ const createEmptyReadableStream = () =>
},
});

// Middleware runner (Is there a better name?)
export const runner = (options: MiddlewareOptions): MiddlewareHandler => {
const entriesPromise =
options.cmd === 'start'
Expand Down Expand Up @@ -68,3 +70,11 @@ export const runner = (options: MiddlewareOptions): MiddlewareHandler => {
await next();
};
};

export const getHonoContext = <C extends Context = Context>() => {
const c = unstable_getCustomContext()[HONO_CONTEXT];
if (!c) {
throw new Error('Hono context is not available');
}
return c as C;
};
2 changes: 1 addition & 1 deletion packages/waku/src/unstable_hono.ts
Original file line number Diff line number Diff line change
@@ -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 const importHono = () => import('hono');
export const importHonoNodeServer: any = () => import('@hono/node-server');
Expand Down

0 comments on commit 232a3a5

Please sign in to comment.