ReviewOS

stacks/bun-router

fix(types): a handler cannot return the plain values the resolver documents

#894
Closed chris opened this 8 days ago ·
8 days ago

RouteHandler and TypedRouteHandler declare Response | Promise<Response>. The resolver has never required that:

Resolves route handlers from various formats:

  • Functions returning Response
  • Functions returning strings/objects (auto-wrapped)

wrapResponse() turns a string, number, boolean, ArrayBuffer, Uint8Array, ReadableStream, object or array into a Response, and createHandlerInvoker calls it on every plain return. So the documented idiom does not compile, and every example that returns an object has to be cast.

Widening the declared return type surfaced two places that had quietly assumed the narrow one and would hand Bun a bare object at runtime:

  • fluent-routing.ts — the middleware chain's terminal step returns await route.handler(request) directly.
  • validation-integration.ts — the JSON-API route builder returns await handler(req) directly.

Both now wrap, like every other dispatch path. A route registered through the fluent API that returns { hello: 'world' } answered with a 500 before this; it answers with JSON now.

Widening is backwards compatible: a Response is still a valid return.

Sign in to comment on this issue.