Skip to content

Apps and routes ​

Building an HTTP application with http.app and http.route.

package @smitejs/http

@smitejs/http is a declarative DSL for describing an HTTP API. You describe routes and handlers; the DSL takes care of turning that description into a serving application.

The app ​

http.app(name?) creates an app and returns a reference carrying use() and serve(). Everything hangs off that one reference: inject routers (and aspects) with app.use(...), and call app.serve() (or serve(app)) when you are ready to handle requests.

ts
const app = http.app("store");
const routes = http.router();
app.use(routes);
ts
"app"

The route ​

http.router() creates a standalone route reference, carrying input, accept, and per-method shortcuts. Routes are where shared shape lives: a route can declare per-bucket input validation that all of its endpoints inherit, then fan out into multiple method + path endpoints. Routes are inert until injected: app.use(routes) writes their endpoints into the app's IR, so a router can be declared anywhere and attached to an app later.

At request time the router picks the one endpoint whose method and path match, and hands the request to its handler:

ts
const app = http.app("store");
const routes = http.router();
routes.get(
  "/users/:id",
  { params: z.object({ id: z.string() }) },
  (ctx) => json({ id: ctx.params.id }),
);
routes.post("/users", { body: z.object({ name: z.string() }) }, (ctx) =>
  status(201).json({ name: ctx.body.name }),
);
app.use(routes);

const health = methods.get("/health", {}, () => json({ ok: true }));
app.use(health);
ts
2
2

One value, many consumers ​

The app you build is a plain value you can hand to different consumers: serve turns it into a request handler, the client generator reads it to emit a typed client, and future tooling can walk it the same way — no special plumbing required.

Released under the MIT License.

/ search•[ ] prev/next•j/k scroll•h home•t theme•guide/fundamentals/apps-and-routes