Skip to main content

@hile/http

Build Koa HTTP APIs with file-system controllers, middleware, response plugins, and Zod validation.

Choose This Package When

Use When

Use @hile/http for Koa-based APIs, middleware, manual routes, file-system controllers, Zod validation, and response plugins.

Do Not Use When

  • Do not use it for Next.js pages; use @hile/http-next when Next.js and API routes share a port.
  • Do not assume Zod validation mutates/coerces Koa context data. It validates only.

Install

Imports

Copy-Paste Example

File route examples:

More Examples

The second parse is intentional when you need parsed/coerced data. Hile’s controller validation does not write safeParse().data back to ctx.request.body.

Runtime And Lifecycle Notes

  • new Http({ port }) creates a Koa app and a find-my-way router.
  • http.use(middleware) registers Koa middleware before listen().
  • http.listen() returns a close function.
  • http.load(directory, options) scans *.controller.{ts,js,tsx,jsx,mjs} by suffix.
  • defineController() supports method-only, method-plus-middlewares, and metadata-plus-Zod forms.
  • Response plugins transform handler return values and set ctx.body when the final result is not undefined.

Anti-Patterns

  • Setting ctx.body and returning a value from the same controller.
  • Loading controllers after starting only because an old example does it; prefer load before listen in new code.
  • Using old validation examples that assume Zod coercion rewrote ctx.query.

Verification Checklist

  • Controller files default-export defineController(...) or an array of controllers.
  • Controllers return response values.
  • Boot service awaits http.load() before http.listen().
  • Zod schemas are used for validation, and parsed data is explicitly parsed when needed.

HTTP API + Model + TypeORM

Complete Example

Controller:
Model:
Boot file:
Package config:

Package-Local AI Guide

This package also ships AI.md in npm so agents can read accurate examples after installation.