Repository navigation
Follow Bun route maps spread in from other files - #135
Open
alexpricedev wants to merge 1 commit into
Open
alexpricedev wants to merge 1 commit into
alexpricedev wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #133.
What was wrong
A Bun server whose
routesare spread intoBun.servefrom other files showed no endpoints.bunRoutesonly read aroutes: { … }literal in the serving file, and only when each value was a handler or a plain{ GET, POST }object. Soroutes: 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.mjsroutes:may sit inside one wrapping call.createRouteHandler({ GET: a, POST: b })) gives one endpoint per method. A helper handed anything else (cached({ ttl: 5, handler })) still serves the route asANY.bunRouteSpreadslists the names spread into aBun.serve's routes. NewrouteMapslists objects whose every key is a path.routes.mjsplaceBunRouteMapsruns first inresolveRoutes. It follows each spread name to where it is defined: the same file, an imported file, or through re-exports (asexportOfdoes). That map's routes become endpoints of the defining file.Bun.servespreads in stays data, so a website's own map of its links isn't mistaken for routes.extract.mjsbunSpreadsandrouteMapsper file.Tests
tests/route-tables.test.mjs:routes:with helper method maps;Bun.servespreads 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 onmain).claude plugin validate .passes.Tried on a real project
Billet, a server-rendered Bun app (
src/server/main.tsspreadssrc/server/routes/{app,admin,api}):Locationheader in a controller.CHANGELOG.mdhas an entry under "Unreleased".