npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@nwire/koa

v0.15.1

Published

Nwire — Koa-backed HTTP adopter. Consumes wires from @nwire/wires/http and serves them as Koa routes under endpoint lifecycle.

Readme

@nwire/koa

The canonical HTTP adopter — a Koa server that consumes Nwire wires.

pnpm add @nwire/koa @nwire/app @nwire/endpoint @nwire/wires

Quick start

import { createApp } from "@nwire/app";
import { endpoint } from "@nwire/endpoint";
import { post } from "@nwire/wires/http";
import { httpKoa } from "@nwire/koa";
import { z } from "zod";

const app = createApp({ appName: "api" });

app.wire(post("/hello", { body: z.object({ name: z.string().min(1) }) }), async (input) => ({
  message: `Hello, ${input.name}!`,
}));

await endpoint("api", { port: 3000 })
  .use(httpKoa({ prefix: "/api" }))
  .mount(app)
  .run();

Config

httpKoa({
  prefix: "/api", // mounted on every wired route
  bodyParser: { jsonLimit: "1mb" },
  middleware: [requireUser], // adopter-wide Koa middleware run before every wire
  logger: myLogger, // defaults to ConsoleLogger
});

Security headers, CORS, and correlation are the endpoint's concerns, applied to every surface at once:

endpoint("api", { port: 3000, http: { cors: { origin: ["https://app.example"] } } })
  .use(httpKoa({ prefix: "/api" }))
  .mount(app)
  .run();

Per-route middleware

Wires can carry route-scoped middleware. httpKoa runs them after the adopter-wide chain and before the handler.

import { post } from "@nwire/wires/http";

app.wire(
  post("/admin/users", {
    body: UserBody,
    middleware: [requireRole("admin")],
  }),
  createUser,
);

Handler shape

Two shapes are supported and route to the same dispatch:

// Foundation HTTP — plain (input, ctx)
app.wire(post("/order", { body: OrderBody }), async (input, ctx) => {
  const repo = ctx.resolve<OrderRepo>("orders");
  return repo.create(input);
});

// Forge HandlerDefinition — routed through runtime.execute
const placeOrder = defineHandler(placeOrderAction, async (input, ctx) => {
  /* ... */
});
app.wire(post("/order", { body: OrderBody }), placeOrder);

The adopter detects forge handlers (.run + runtime.execute present) and routes them through the runtime so action.before/after hooks, retries, telemetry, and per-action middleware all fire. Plain handlers take the direct path with a minimal ctx (resolve, logger, koa, envelope).

Response shaping

| Handler returns | HTTP response | | ---------------------------------------------- | ------------------------------- | | { $kind: "response-instance", status, body } | status, JSON body | | { $status, body } (legacy envelope) | $status, JSON body | | anything else | 200, JSON serialized verbatim | | validation throws | 400, structured error JSON | | handler throws defineError-shaped | error.status, structured JSON |

Testing

adapter.koa() exposes the underlying Koa app for in-process testing via supertest or simulateRequest from @nwire/test-kit:

const koa = httpKoa({ port: 0 });
const running = await endpoint("test", { ... }).use(koa).mount(app).run();

const res = await simulateRequest(koa.koa(), {
  method: "POST",
  path: "/hello",
  body: { name: "Alice" },
});

adapter.port() returns the bound port (useful when port: 0).

Related