InversifyHonoHttpAdapter
The Hono HTTP adapter implementation. This adapter allows you to use InversifyJS framework with Hono applications, providing ultrafast routing and edge runtime support.
This adapter extends InversifyHttpAdapter. See the base class documentation for information about common methods like applyGlobalMiddleware, useGlobalFilters, applyGlobalGuards, useGlobalInterceptors, and useGlobalPipe.
Installation
- npm
- yarn
- pnpm
npm install @inversifyjs/http-hono hono inversify reflect-metadata
yarn add @inversifyjs/http-hono hono inversify reflect-metadata
pnpm add @inversifyjs/http-hono hono inversify reflect-metadata
Constructor
constructor(
container: Container,
httpAdapterOptions?: HttpAdapterOptions,
customApp?: Hono,
)
Creates a Hono adapter instance.
Parameters
container: The Inversify container that holds controllers, guards, pipes, middleware, interceptors, and filters.httpAdapterOptions(optional): Configuration options for the adapter. See Options below.customApp(optional): A custom Hono application instance. If not provided, a default Hono instance is created.
Default Options
The adapter uses these defaults if not overridden:
{
logger: true,
}
Methods
build
async build(): Promise<Hono>
Builds and returns the Hono application with all configured routes, middleware, and handlers.
Returns: The Hono instance ready to be used with serve or various runtime adapters.
Options
HttpAdapterOptions
interface HttpAdapterOptions {
logger?: boolean | Logger;
}
Properties
logger: Set totrueto log route mappings on build, orfalseto disable logging, or provide a custom logger implementing theLoggerinterface from@inversifyjs/logger. Default:true
Usage Example
const container: Container = new Container();
// ... bind your controllers, services, etc.
// Create the adapter with options
const adapter: InversifyHonoHttpAdapter = new InversifyHonoHttpAdapter(
container,
{
logger: true,
},
);
// Build the Hono application
const app: Hono = await adapter.build();
// Start the server (Node.js runtime)
serve({
fetch: app.fetch,
port: 3000,
});
console.log('Server listening on port 3000');
Hono-Specific Features
Edge Runtime Support
Hono is designed to work seamlessly with edge runtimes like Cloudflare Workers, Deno Deploy, and Bun. The InversifyJS adapter maintains this compatibility.
Lightweight and Fast
Hono has zero dependencies and uses modern Web Standards APIs, making it extremely lightweight and fast. The router is optimized using a RegExp-based approach.
Multiple Runtime Support
You can run Hono with various runtimes:
// Node.js
import { serve } from '@hono/node-server';
// Cloudflare Workers
export default app;
// Deno
Deno.serve(app.fetch);
// Bun
export default {
port: 3000,
fetch: app.fetch,
};
Body Parsing
Unlike Express or Fastify, body parsing in Hono is handled asynchronously. The adapter automatically awaits body parameters decorated with @Body().
Type Parameters
The adapter is strongly typed with Hono types:
InversifyHonoHttpAdapter extends InversifyHttpAdapter<
HonoRequest, // Hono Request
Context, // Hono Context
Next, // Hono Next function
Response | undefined, // Handler return type
HttpAdapterOptions
>
See Also
- InversifyHttpAdapter (Base Class) - Common methods and configuration
- Controller - Creating route handlers
- Middleware - Adding middleware to routes
- Guards - Protecting routes with guards
- Hono Documentation - Official Hono documentation