Repository navigation
feat(routes): support bracket patterns in typed routing APIs - #747
Conversation
|
Why are we doing this? |
Because now the route annotations are very different from the file paths to the routes. Currently given such a route: However, for the catch-all URLs, the syntax is kinda obscure — the generated type for The goal here is to match what users see in the file directory for better readabilty. |
cpojer
left a comment
There was a problem hiding this comment.
Makes sense! The PR description didn't explain this.
Background
I messed up when I first added
routeUrlin #640, the internal route type follows Hono's syntax, but that's not what users see when they create file-based routes.Developers naturally expect to reuse the parameter patterns they see in filenames:
routes/api/users/[id].tsfetch('/api/users/[id]', …)routes/files/[...path].tsrouteUrl('/files/[...path]', …)routes/chat/[room].ws.tsconnect('/chat/[room]', …)users/[id].server.tsaction('/users/[id]?update', …)The named catch-all was especially awkward:
The inconsistency also affected types: a page named
users/[id]couldn’t use that same pattern directly withuseParams<'/users/[id]'>()orPageParams<'/users/[id]'>.To stop this problem before it becomes too widespread, we should add support for file-based route patterns while still allowing Hono syntax for now, then deprecate the Hono syntax in a v2 release or something.
Summary
Support bracket route patterns such as
/users/[id]and/docs/[...slug]across generated route types, URL construction, typed HTTP and streaming clients, WebSockets, and page actions. Keep existing generated keys available for compatibility and preserve their inferred contracts.Update public examples and agent guidance to use bracket patterns. Add regression coverage for parameter encoding, catch-alls, named actions, generated/package declarations, all four adapters, browsers, and Workers.
Validation:
vp run checkvp run buildvp test run— 16,185 passed, 32 skipped (781 files passed, one skipped)vp run --filter ./docs build