@engblock/microfrontend-hono-gateway
v0.0.2
Published
[](https://badge.fury.io/js/%40engblock%2Fhono-gateway-core) [](https://opensource.org/licenses/MIT)
Readme
Hono Gateway Core (@engblock/hono-gateway-core)
Core library for building flexible, type-safe micro-frontend or micro-service gateways using Hono. This library provides the foundational components to route incoming requests based on URL prefixes to different handlers, supporting both direct function calls (monorepo style) and HTTP forwarding (e.g., Cloudflare Workers Service Bindings).
Overview
In modern web architectures, it's common to split applications into smaller, independently deployable units (micro-frontends or micro-services). A gateway acts as the single entry point, routing requests to the appropriate downstream service or rendering module.
hono-gateway-core simplifies the creation of such gateways within the Hono framework by providing:
- A Gateway Factory (
createGateway): Sets up a Hono application with core routing logic. - An Abstraction Layer (
MicroAppHandler): Decouples the gateway from the specific mechanism used to handle the request (direct call vs. HTTP fetch). - Concrete Handler Implementations:
DirectCallHandler: For invoking localssrfunctions directly (ideal for monorepos).HttpFetchHandler: For forwarding requests to other HTTP endpoints or Cloudflare Worker Service Bindings.
- Type Safety: Leverages TypeScript for robust development.
Core Concepts
- Gateway: The main Hono application instance created by
createGateway. It intercepts requests matching patterns like/:appName/*and uses the configuration to find the correct handler. MicroAppHandler: An interface defining the contract for handling a request delegated by the gateway. The key method ishandle(c: Context): Promise<Response>, which must return a completeResponseobject.DirectCallHandler: An implementation ofMicroAppHandlerthat takes a module conforming to theDirectRenderAppinterface (having anssr(c: Context): Promise<string> | stringmethod). It calls thessrmethod and wraps the resulting HTML string in aResponse.HttpFetchHandler: An implementation ofMicroAppHandlerthat forwards the incoming request to another HTTP service. It can be configured to use Cloudflare Worker Service Bindings (type: 'cf_binding') or a standard base URL (type: 'url'). It returns theResponsereceived from the downstream service.HandlerConfigFactory: A function(c: Context<GatewayEnv>) => HandlerConfigprovided tocreateGateway. It's responsible for creating the mapping between URL prefixes (e.g.,"profile") and their correspondingMicroAppHandlerinstances. Crucially, it receives the HonoContext(c), allowing access to environment variables (c.env) needed for configuring handlers (likeHttpFetchHandlerwith service bindings).
Features
- Flexible Delegation: Supports both direct function invocation and HTTP forwarding out-of-the-box.
- Type-Safe: Built with TypeScript, providing strong typing for configuration and context.
- Extensible: Easily create custom
MicroAppHandlerimplementations for other delegation strategies. - Hono Integration: Built on top of the fast and lightweight Hono framework.
- Environment-Aware Configuration: Access environment variables/bindings within the configuration factory.
- Testability: The handler abstraction makes individual routing strategies easier to test in isolation.
Installation
# Using npm
npm install @engblock/hono-gateway-core hono
# Using yarn
yarn add @engblock/hono-gateway-core hono
# Using pnpm
pnpm add @engblock/hono-gateway-core hono