feat: extend basePath() helper to accept Hono instances#4766
Open
G4brym wants to merge 1 commit intohonojs:mainfrom
Open
feat: extend basePath() helper to accept Hono instances#4766G4brym wants to merge 1 commit intohonojs:mainfrom
G4brym wants to merge 1 commit intohonojs:mainfrom
Conversation
b148db4 to
ebf9231
Compare
Member
|
Hey @G4brym If you want to get a base path in a handler, you can use the Have you checked it? |
Author
|
Hey @yusukebe, thanks for the pointer! for example: const app = new Hono().basePath('/api')
const router = fromHono(app) // ← need to know "/api" here, no `c` exists yet |
Author
|
or maybe the basePath helper function could be updated to also work with Hono apps, like: import {
basePath,
} from 'hono/route'
const subApp = new Hono()
const path = basePath(subApp) // '/api'
app.route('/:sub', subApp)i can update the pr if you agree with this change |
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
ebf9231 to
7a32c2d
Compare
Author
|
Hey there, just updated the pr to match the comment above |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Extends the existing
basePath()helper inhono/routeto also accept aHonoinstance, allowing libraries to read a Hono app's base path at setup time — without relying on the private_basePathproperty.Motivation
Currently, the only way to read a Hono instance's base path outside of a request handler is by accessing the private
_basePathproperty. This is fragile — it is not part of the public API and could break without notice.Libraries that integrate with Hono (such as chanfana, an OpenAPI schema generator) need to detect the base path to correctly generate API documentation and route schemas. Right now, chanfana reads
_basePathdefensively with runtime type guards (see PR), but a proper public API would be much better.Changes
src/helper/route/index.ts— Added an overload tobasePath()that accepts aHonoBaseinstance and returns its base path directlysrc/helper/route/index.test.ts— Added 4 tests for the new overload:"/"for a fresh instancebasePath()basePath()callsbasePath()creates a cloneUsage
The existing
basePath(c: Context)handler behaviour is fully preserved.