Skip to content

Follow Bun route maps spread in from other files - #135

Open
alexpricedev wants to merge 1 commit into
haydenschmitty:mainfrom
alexpricedev:fix/133-bun-route-spreads
Open

alexpricedev wants to merge 1 commit into
haydenschmitty:mainfrom
alexpricedev:fix/133-bun-route-spreads

Conversation

@alexpricedev

Copy link
Copy Markdown

Fixes #133.

What was wrong

A Bun server whose routes are spread into Bun.serve from other files showed no endpoints. bunRoutes only read a routes: { … } literal in the serving file, and only when each value was a handler or a plain { GET, POST } object. So routes: secureRoutes({ ...appRoutes, ...apiRoutes }) with "/todos": createRouteHandler({ GET, POST }) entries gave nothing, and Health's sign-in check reported only that the code looked like a server.

What changed

  • route-tables.mjs
    • routes: may sit inside one wrapping call.
    • A route value that is a call handed only HTTP methods (createRouteHandler({ GET: a, POST: b })) gives one endpoint per method. A helper handed anything else (cached({ ttl: 5, handler })) still serves the route as ANY.
    • New bunRouteSpreads lists the names spread into a Bun.serve's routes. New routeMaps lists objects whose every key is a path.
  • routes.mjs
    • placeBunRouteMaps runs first in resolveRoutes. It follows each spread name to where it is defined: the same file, an imported file, or through re-exports (as exportOf does). That map's routes become endpoints of the defining file.
    • A path-keyed object that no Bun.serve spreads in stays data, so a website's own map of its links isn't mistaken for routes.
  • extract.mjs
    • Records bunSpreads and routeMaps per file.
    • Drops a route map's own keys from the requests found on those lines.

Tests

  • Two new cases in tests/route-tables.test.mjs:
    • a wrapped routes: with helper method maps;
    • a generic project whose Bun.serve spreads in maps from the same file, an imported file and a barrel. It also has an unspread path map that must stay out, and a website request that must reach the spread route.
  • npm test: 489 pass, 1 skipped (as on main). claude plugin validate . passes.

Tried on a real project

Billet, a server-rendered Bun app (src/server/main.ts spreads src/server/routes/{app,admin,api}):

  • Before the fix, the scan found 0 route definitions.
  • After it, the scan finds all 45, one per method in its route table.
  • The only request left matched outside tests is a Location header in a controller.

CHANGELOG.md has an entry under "Unreleased".

A Bun server whose routes are spread into Bun.serve from other files
(routes: secureRoutes({ ...appRoutes, ...apiRoutes })) showed no endpoints:
bunRoutes only read a routes: object literal in the serving file, and
only when its values were handlers or plain { GET, POST } objects.

Now each name spread into Bun.serve's routes is followed to the object
that defines it (the same file, an imported one, or through a re-export)
and its paths become endpoints in that file. A route whose value is a
helper handed only methods (createRouteHandler({ GET, POST })) gets one
endpoint per method, and routes: may sit inside one wrapping call. An
object of paths nothing spreads into Bun.serve stays data, and its keys
are not read as requests.

Fixes haydenschmitty#133.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bun.serve routes not detected when the route map is imported from other files or wrapped in a helper

1 participant