import * as hono from 'hono'; import { Env, Hono, Context } from 'hono'; import { UpgradeWebSocket } from 'hono/ws'; import { ServerBuild, UNSAFE_MiddlewareEnabled, RouterContextProvider, AppLoadContext } from 'react-router'; type ReactRouterHonoServerAppLoadContext = UNSAFE_MiddlewareEnabled extends true ? RouterContextProvider : AppLoadContext; interface HonoServerOptionsBase { /** * The base Hono app to use as a replacement for the default one created automatically * * It will be used to mount the React Router server on the `basename` path * defined in the [React Router config](https://api.reactrouter.com/v7/types/_react_router_dev.config.Config.html) * * {@link Hono} */ app?: Hono; /** * Enable the default logger * * Defaults to `true` */ defaultLogger?: boolean; /** * The port to start the server on * * Defaults to `process.env.PORT || 3000` */ port?: number; /** * Augment the React Router AppLoadContext * * Don't forget to declare the AppLoadContext in your app, next to where you create the Hono server * * ```ts * declare module "react-router" { * interface AppLoadContext { * // Add your custom context here * whatever: string; * } * } * ``` * * **To make the typing works correctly, in your `react-router.config.ts` or where you want, add future v8_middleware flag type to true.** * * ```ts * declare module "react-router" { * interface Future { * v8_middleware: true; // 👈 Enable middleware types * } * } * ``` */ getLoadContext?: ( c: Context, options: { build: ServerBuild; mode: string; } ) => Promise | ReactRouterHonoServerAppLoadContext; /** * Hook to add middleware that runs before any built-in middleware, including assets serving. * * You can use it to add protection middleware, for example. */ beforeAll?: (app: Hono) => Promise | void; } interface WithWebsocket { /** * Enable WebSockets support in `configure` * * For `bun` and `cloudflare` we will use the `@hono/node-ws`'s `injectWebSocket` on dev (only), * * Defaults to `false` */ useWebSocket: true; /** * Customize the Hono server, for example, adding middleware * * It is applied after the default middleware and before the React Router middleware */ configure: (app: Hono, options: { upgradeWebSocket: UpgradeWebSocket }) => Promise | void; } interface WithoutWebsocket { /** * Enable WebSockets support in `configure` * * For `bun` and `cloudflare` we will use the `@hono/node-ws`'s `injectWebSocket` on dev (only), * * Defaults to `false` */ useWebSocket?: false | undefined; /** * Customize the Hono server, for example, adding middleware * * It is applied after the default middleware and before the React Router middleware */ configure?: (app: Hono) => Promise | void; } /** * Helper to create a getLoadContext function fully typed */ declare function createGetLoadContext(getLoadContext: HonoServerOptionsBase["getLoadContext"]): ((c: hono.Context, options: { build: ServerBuild; mode: string; }) => Promise | ReactRouterHonoServerAppLoadContext) | undefined; export { type HonoServerOptionsBase as H, type WithoutWebsocket as W, type WithWebsocket as a, createGetLoadContext as c };