Hono Middleware Package
Shared Hono utilities including logger, rate limiter, auth, error handler, and health routes
@duyetbot/hono-middleware
Shared Hono middleware and routes for Cloudflare Workers apps.
Overview
This package provides a standardized way to create Hono-based Cloudflare Workers apps with consistent middleware, health checks, and error handling.
Installation
Quick Start
API Reference
createBaseApp<TEnv>(options: AppOptions): Hono<{ Bindings: TEnv }>
Creates a Hono app with standard middleware and routes.
Options
| Option | Type | Default | Description |
|---|---|---|---|
name | string | required | App name (shown in health check) |
version | string | '1.0.0' | App version |
logger | boolean | true | Enable request logging |
rateLimit | { limit: number; window: number } | undefined | Rate limiting config |
health | boolean | true | Include health check routes |
Included Middleware
Logger Middleware
Logs all requests with:
- Unique request ID
- Method and path
- Response time
- Status code
Rate Limiter
In-memory rate limiting per IP address.
Returns 429 Too Many Requests when limit exceeded.
Error Handler
Consistent error response format:
Included Routes
Health Routes
When health: true (default):
| Route | Description | Response |
|---|---|---|
GET /health | Full health check | { status: 'ok', name, version, timestamp } |
GET /health/live | Liveness probe | { status: 'ok' } |
GET /health/ready | Readiness probe | { status: 'ok' } |
Composable Usage
For more control, use individual components:
Individual Exports
createLogger(options?: LoggerOptions)
Creates logger middleware.
createRateLimiter(options: RateLimitOptions)
Creates rate limiter middleware.
createAuth(options: AuthOptions)
Creates authentication middleware.
errorHandler
Global error handler.
healthRoutes(name: string, version?: string)
Creates health check routes.
Usage in Platform Apps
Telegram Bot
GitHub Bot
Architecture Decision
Why Separate Apps with Shared Middleware?
We chose separate platform apps (telegram-bot, github-bot) over a unified API gateway because:
| Factor | Unified Gateway | Separate Apps |
|---|---|---|
| Fault Isolation | One bug affects all | Failures don't cascade |
| Scaling | Scale everything together | Scale independently |
| Bundle Size | Large (all platforms) | Small (one platform) |
| Cold Start | Slower | Faster |
| Secrets | All mixed together | Isolated per app |
| Deployment | All or nothing | Independent deploys |
The shared middleware package provides the benefits of code reuse without the drawbacks of a monolithic gateway.
Cloudflare Workers Considerations
- Billing: Per-request pricing favors smaller, focused workers
- Cold starts: Smaller bundles = faster response times
- Limits: Each worker has its own CPU/memory limits
Development
Testing
Local Development
Migration Guide
From Custom Middleware
Before:
After:
From apps/api
The apps/api package is deprecated. Migrate to:
- Use
@duyetbot/hono-middlewarefor shared middleware - Keep platform-specific logic in respective apps
- Remove
apps/apifrom the monorepo
Package Structure
Dependencies
hono- Web framework@duyetbot/types(peer) - Shared types