Skip to content

Support for * as a valid path parameter #5155

Description

@BlankParticle

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/*.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions