Skip to content
brontidevPublic

About

Type-safe Promise based router

Resources

Stars

3 stars

Watchers

0 watching

Forks

Repository files navigation

@bronti/wooter

JSR JSR Score

A fetch-native, type-safe HTTP router for JavaScript/TypeScript with explicit middleware composition and zero runtime dependencies.

import { p, Wooter } from "@bronti/wooter"

const app = new Wooter()
	.use(cors)
	.use(auth)
	.route(p.path("users", p.param("id", p.num)), "GET", async ({ params, resp }) => {
		resp(Response.json({ userId: params.get("id") }))
	})

export default app.fetch

Philosophy

Wooter is built on a single principle: every request must produce a response before the lifecycle completes.

This invariant ensures predictable behavior. Combined with explicit middleware composition, it makes data flow visible and error handling straightforward.

Learn more: Wooter Philosophy

Key Features

  • ✅ Type-Safe by Default — Full TypeScript inference for routes, middleware, and parameters
  • ✅ Fetch-Native — Works with Deno, Node.js, Bun, Cloudflare Workers, and any Fetch API runtime
  • ✅ Explicit Design — No hidden behavior; response propagation is clear and intentional
  • ✅ Composable Middleware — State accumulates through middleware chains with type preservation
  • ✅ Lightweight — Zero runtime dependencies; ~5KB gzipped
  • ✅ Flexible Error Handling — Use exceptions, Result types, or custom patterns
  • ✅ Zero Magic — Understand exactly what your code does

Quick Start

Installation

# Deno
deno add @bronti/wooter

# or via jsr
npx jsr add @bronti/wooter

Your First Router

import { p, Wooter } from "@bronti/wooter"

const app = new Wooter()

// GET /hello
app.route(p.path("hello"), "GET", ({ resp }) => {
	resp(new Response("Hello, Wooter!"))
})

// GET /users/{id}
app.route(p.path("users", p.param("id", p.num)), "GET", ({ params, resp }) => {
	resp(Response.json({ id: params.get("id") }))
})

// POST /users
app.route(p.path("users"), "POST", ({ request, resp }) => {
	resp(new Response("Created", { status: 201 }))
})

export default app.fetch

Run with Deno

deno serve app.ts

Core Concepts

Response-First Lifecycle

Every request must produce a response. This is Wooter's core guarantee.

app.route(p.path("example"), "GET", ({ resp }) => {
	// ✅ This works
	resp(new Response("OK"))

	// ❌ Forgetting resp() throws HandlerDidntRespondError
})

Middleware Composition

Middleware accumulates state for downstream handlers:

const app = new Wooter()
	.use(async ({ next }) => {
		// Add user to state
		await next({ user: { id: 1, name: "Alice" } })
	})
	.use(async ({ state: { user }, next }) => {
		// Access user from previous middleware
		await next({
			permissions: getPermissions(user),
		})
	})
	.route(p.path("admin"), "GET", async ({ state, resp }) => {
		// Access all accumulated state
		resp(Response.json({ user: state.user, perms: state.permissions }))
	})

Explicit Response Propagation

Middleware chooses how to handle responses:

// forward() — Pass through without modification
await forward({ userId: 123 })

// next() — Inspect or modify response
const response = await next({ userId: 123 })
resp(modifyHeaders(response))

// tryNext() — Capture errors as Result
const result = await tryNext()
result.match(
	(response) => resp(response),
	(error) => resp(new Response("Error", { status: 500 })),
)

Core Topics

Examples

CRUD API

import { middleware, p, Wooter } from "@bronti/wooter"

const db = new Map()

const app = new Wooter()
	.route(p.path("items"), {
		GET: async ({ resp }) => {
			resp(Response.json(Array.from(db.values())))
		},
		POST: async ({ request, resp }) => {
			const item = await request.json()
			db.set(item.id, item)
			resp(Response.json(item), { status: 201 })
		},
	})
	.route(p.path("items", p.param("id", p.num)), {
		GET: async ({ params, resp }) => {
			const item = db.get(params.get("id"))
			resp(item ? Response.json(item) : new Response("Not found", { status: 404 }))
		},
		PUT: async ({ params, request, resp }) => {
			const item = await request.json()
			db.set(params.get("id"), item)
			resp(Response.json(item))
		},
		DELETE: async ({ params, resp }) => {
			db.delete(params.get("id"))
			resp(null, { status: 204 })
		},
	})

export default app.fetch

Middleware

// Authentication
const auth = middleware<{ user: User }>(
	async ({ request, next, resp }) => {
		const token = request.headers.get("Authorization")
		if (!token) return resp(new Response("Unauthorized", { status: 401 }))

		const user = verifyToken(token)
		await next({ user })
	},
)

// JSON parsing
const json = middleware<{ json: () => Promise<any> }>(
	async ({ request, forward, resp, safeExit }) => {
		let cached: any
		await forward({
			json: async () => {
				if (cached) return cached
				try {
					return cached = await request.json()
				} catch {
					resp(new Response("Invalid JSON", { status: 400 }))
					safeExit()
				}
			},
		})
	},
)

const app = new Wooter()
	.use(json)
	.use(auth)
	.route(p.path("users"), "POST", async ({ state: { json }, resp }) => {
		const body = await json()
		resp(Response.json(body), { status: 201 })
	})

Runtimes

Wooter works with any Fetch API-compatible runtime:

Runtime Version Status
Deno 1.20+ ✅ Fully supported
Node.js 18+ ✅ Fully supported
Bun 0.1.0+ ✅ Fully supported
Cloudflare Workers Any ✅ Fully supported
Deno Deploy Any ✅ Fully supported
Fastly Compute Any ✅ Fully supported
Modern Browsers - ✅ Supported (limited use cases)

Status

Wooter follows epoch semver. The core API is stable, but the library hasn't reached v100 yet. Consider it production-ready for non-critical applications.

Aspect Status
Core Routing ✅ Stable
Middleware ✅ Stable
API Surface ✅ Stable
Public Release In progress (aiming for v100)

Contributing

Contributions are welcome! Areas for improvement:

  • Documentation and examples
  • Performance optimizations
  • Bug reports and fixes
  • Feature ideas (with discussion first)
  • Community middleware

License

MIT

Learn More

About

Type-safe Promise based router

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages