@tank/hono-mastery
1.0.0Description
Build and deploy multi-runtime TypeScript APIs with Hono. Covers routing, Context API, built-in and custom middleware, Zod/Valibot validation, RPC mode with hc client, OpenAPI integration, testing, error handling, and deployment to Cloudflare Workers, Bun, Deno, Node.js, AWS Lambda, and Vercel.
Triggered by
honohono jshono frameworkhono vs expresshono cloudflare workershono rpc
Download
Verified
tank install @tank/hono-masteryHono Mastery
Core Philosophy
- Web Standards first — Hono uses only Web Standard APIs (Request, Response, fetch). Code written for one runtime runs on every runtime without adaptation.
- TypeScript types are the contract — Chain handlers to preserve type inference. Avoid separating handlers into "controllers" because path params and middleware types break. Use
c.req.param()inline where types flow. - Middleware is the composition layer — Every cross-cutting concern (auth, CORS, logging, validation) is middleware. Register with
app.use()by path, compose withcreateMiddleware()for type safety. - RPC replaces hand-written API clients — Export
typeof appand usehc<AppType>()on the client. The Zod validator schema becomes the single source of truth for both request validation and client types. - Adapters bridge runtimes — Write one Hono app, deploy anywhere. Each runtime adapter translates its entry point to Hono's
fetch(request, env, ctx)signature.
Quick-Start: Common Problems
"How do I structure a larger Hono app?"
- Create sub-apps per domain:
const users = new Hono()with chained handlers - Mount with
app.route('/users', users)in the main entry - For RPC, chain routes:
const routes = app.route('/users', users).route('/posts', posts) - Export the type:
export type AppType = typeof routes-> Seereferences/routing-and-context.md
"My Cloudflare bindings have no types"
- Define:
type Bindings = { DB: D1Database; KV: KVNamespace } - Pass:
new Hono<{ Bindings: Bindings }>() - Access:
c.env.DB.prepare(...)with full autocomplete - For middleware using env values, wrap:
const auth = basicAuth({ username: c.env.USERNAME, password: c.env.PASSWORD }); return auth(c, next)-> Seereferences/cloudflare-integration.md
"How do I add Zod validation with OpenAPI?"
- Install:
@hono/zod-validatoror@hono/zod-openapi - Use
zValidator('json', schema)as middleware before the handler - Access validated data:
c.req.valid('json') - For OpenAPI: use
createRoute()with request/response schemas, thenapp.openapi(route, handler)-> Seereferences/validation-and-openapi.md
"RPC types are not working across packages"
- Ensure identical Hono versions in server and client packages
- Chain all handlers:
const app = new Hono().get(...)notapp.get(...) - Export:
export type AppType = typeof app - For monorepos: use TypeScript project references or compile with
tscfirst -> Seereferences/rpc-and-client.md
Decision Trees
Runtime Selection
| Signal | Runtime |
|---|---|
| Edge deployment on Cloudflare | Cloudflare Workers |
| Fast startup, modern server | Bun |
| Secure sandbox, Deno Deploy | Deno |
| Existing Node.js infrastructure | Node.js via @hono/node-server |
| Serverless on AWS | AWS Lambda via adapter |
| Vercel serverless/edge | Vercel adapter |
Validation Approach
| Signal | Approach |
|---|---|
| Need OpenAPI docs from schemas | @hono/zod-openapi |
| Simple request validation | zValidator from @hono/zod-validator |
| Want smallest bundle | Valibot via @hono/standard-validator |
| Framework-agnostic validators | Standard Schema (sValidator) |
| Minimal, no dependencies | Manual validator() from hono/validator |
Auth Strategy
| Signal | Approach |
|---|---|
| Simple API key / password gate | basicAuth() or bearerAuth() built-in |
| JWT token verification | jwt() built-in middleware |
| Custom session / OAuth | Custom middleware with createMiddleware() |
| Cloudflare Access / Zero Trust | Check CF-Access-JWT-Assertion header |
Reference Index
| File | Contents |
|---|---|
references/routing-and-context.md | Routing patterns (params, regex, groups, basePath, chaining, priority), Context API (c.req, c.json, c.html, c.redirect, c.set/get, c.var, c.env), request/response handling |
references/middleware-patterns.md | Built-in middleware catalog (cors, jwt, basicAuth, bearerAuth, logger, compress, secureHeaders, cache, etag, csrf, bodyLimit), custom middleware with createMiddleware, execution order, type-safe variables |
references/validation-and-openapi.md | Manual validator, Zod/Valibot/ArkType integration, Standard Schema, @hono/zod-openapi, Swagger UI setup, validation targets (json, form, query, param, header, cookie) |
references/rpc-and-client.md | hc client setup, type inference, InferRequestType/ResponseType, path parameters in RPC, status-code-based typing, $url/$path helpers, monorepo patterns, IDE performance tips |
references/cloudflare-integration.md | Workers setup, typed Bindings (D1, R2, KV, Durable Objects), executionCtx.waitUntil, env variables, static assets, GitHub Actions deployment, scheduled events |
references/multi-runtime-deployment.md | Bun, Deno, Node.js (@hono/node-server), AWS Lambda, Vercel, Fastly adapters, entry point patterns, environment differences, migration from Express |
references/testing-and-error-handling.md | app.request testing, testClient helper, env mocking, onError handler, notFound handler, HTTPException, error propagation in middleware, Vitest setup |