Skip to content

Conversation

@usualoma
Copy link
Member

fixes #4196

Currently, only routes associated with handlers can be retrieved. This PR enables passing an optional index parameter (which has the same meaning as Array.prototype.at(index)), allowing routePath(c, -1) to retrieve the route that matched at the end.

The author should do the following, if applicable

  • Add tests
  • Run tests
  • bun run format:fix && bun run lint:fix to format the code
  • Add TSDoc/JSDoc to document the code

@usualoma
Copy link
Member Author

Hi @yusukebe

What do you think about this?

@codecov
Copy link

codecov bot commented Sep 23, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.25%. Comparing base (ba98aa2) to head (34bb026).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4423   +/-   ##
=======================================
  Coverage   91.25%   91.25%           
=======================================
  Files         171      171           
  Lines       10913    10915    +2     
  Branches     3144     3146    +2     
=======================================
+ Hits         9959     9961    +2     
  Misses        953      953           
  Partials        1        1           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

*/
export const routePath = (c: Context): string => matchedRoutes(c)[c.req.routeIndex].path
export const routePath = (c: Context, index?: number): string =>
matchedRoutes(c).at(index ?? c.req.routeIndex)?.path ?? ''
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It isn't easy to define the behavior if it cannot find the route. I think both '' and undefined are okay. In this case, '' is better because this way does not break that routePath returns value is string (not string | undefined). So this is good.

Copy link
Member

@yusukebe yusukebe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@yusukebe
Copy link
Member

Hi @usualoma

This is a good idea! Looks good. The change is only adding the one option, so we can include this in the next patch release. Thanks!

@yusukebe yusukebe merged commit 81bda2e into honojs:main Sep 24, 2025
20 checks passed
@usualoma usualoma deleted the feat-get-route-path-at branch September 24, 2025 03:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support get real route path in middleware

2 participants