hono icon indicating copy to clipboard operation
hono copied to clipboard

Add `LinearRouter.matchRoute` static method for direct matching

Open Digital39999 opened this issue 4 months ago • 2 comments

What is the feature you are proposing?

What is the feature you are proposing?

Introduce a LinearRouter.matchRoute static method that allows developers to use the route-matching logic directly, without needing to:

  1. Initialize a full router instance, and
  2. Add routes only to then check if a path matches.

This would simply abstract the logic already implemented in the router’s existing match function, but make it accessible as a standalone utility.

Why is this needed?

The route-matching logic is useful outside the context of a full router.

Providing matchRoute as a direct method would:

  • Simplify testing (check if a path would match a given route definition).
  • Help with debugging (inspect how a route resolves without building a router).

Digital39999 avatar Sep 16 '25 09:09 Digital39999

But how will you put path+handler in router? For that you need an instance of router then router.add(path, handler) then you can use router.match. But for static router.match i don't know how it will work? @yusukebe

pradeepbgs avatar Sep 22 '25 00:09 pradeepbgs

But how will you put path+handler in router? For that you need an instance of router then router.add(path, handler) then you can use router.match. But for static router.match i don't know how it will work? @yusukebe

Router.match isn't static, check the implementation here: https://github.com/honojs/hono/pull/4410. Instead of using the whole router class to check if route path matches the actual request pathname, just use a functions. Nothing changes.

Digital39999 avatar Sep 22 '25 07:09 Digital39999