What is the feature you are proposing?
This would be a part of #5106 as a new feature in Hono v5
Basically add support for properly typed wildcard * param in routes and we can access it via c.req.param(), its a better way to do /thing/:path{.*}
This test explains it the best
it('Should return wildcard parameters', async () => {
const app = new Hono()
app.get('/thing/*', (c) => c.json(c.req.param()))
const res = await app.request('/thing/foo/bar')
expect(await res.json()).toEqual({ '*': 'foo/bar' })
})
The API would look like
app.get('/thing/*', (c) => {
c.req.param('*') // string
c.req.param() // { '*': string }
})
and you will use it like this
app.get('/thing/*', (c) => {
const { '*': _splat } = c.req.param(); // _splat is what TanStack calls it, you can call it anything
const _splat2 = c.req.param('*')
})
This is the behaviours I have in mind
| Route pattern |
Request path |
Wildcard capture |
Notes |
/a/* |
/a/b |
b |
The separating / is not part of the capture. |
/a/* |
/a/b/c |
b/c |
The capture may contain multiple path segments. |
/a/* |
/a/ |
"" |
Empty wildcard capture. |
/a/* |
/a |
"" |
The trailing wildcard may match no additional path. |
/a/* |
/ab |
No match |
The wildcard is a distinct path segment. |
/* |
/a/b |
a/b |
Captures the entire path without the leading /. |
* |
/a/b |
a/b |
Captures the entire path without the leading /. |
/a* |
/a/b |
No wildcard parameter |
Invalid wildcard pattern |
/a* |
/abc |
No wildcard parameter |
Invalid wildcard pattern |
/a/:path{.*} |
/a/b/c |
path = "b/c" |
Existing named-regexp equivalent of /a/*. |
What is the feature you are proposing?
This would be a part of #5106 as a new feature in Hono v5
Basically add support for properly typed wildcard
*param in routes and we can access it viac.req.param(), its a better way to do/thing/:path{.*}This test explains it the best
The API would look like
and you will use it like this
This is the behaviours I have in mind
/a/*/a/bb/is not part of the capture./a/*/a/b/cb/c/a/*/a/""/a/*/a""/a/*/ab/*/a/ba/b/.*/a/ba/b/./a*/a/b/a*/abc/a/:path{.*}/a/b/cpath = "b/c"/a/*.