Skip to content

feat: extend basePath() helper to accept Hono instances#4766

Open
G4brym wants to merge 1 commit intohonojs:mainfrom
G4brym:feat/get-base-path
Open

feat: extend basePath() helper to accept Hono instances#4766
G4brym wants to merge 1 commit intohonojs:mainfrom
G4brym:feat/get-base-path

Conversation

@G4brym
Copy link
Copy Markdown

@G4brym G4brym commented Feb 24, 2026

Summary

Extends the existing basePath() helper in hono/route to also accept a Hono instance, allowing libraries to read a Hono app's base path at setup time — without relying on the private _basePath property.

Motivation

Currently, the only way to read a Hono instance's base path outside of a request handler is by accessing the private _basePath property. 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 _basePath defensively with runtime type guards (see PR), but a proper public API would be much better.

Changes

  • src/helper/route/index.ts — Added an overload to basePath() that accepts a HonoBase instance and returns its base path directly
  • src/helper/route/index.test.ts — Added 4 tests for the new overload:
    • Returns "/" for a fresh instance
    • Returns the base path after basePath()
    • Returns the merged path after chained basePath() calls
    • Original instance is unaffected after basePath() creates a clone

Usage

import { basePath } from 'hono/route'

const app = new Hono().basePath('/api')
basePath(app) // '/api'

const v1 = app.basePath('/v1')
basePath(v1)  // '/api/v1'
basePath(app) // '/api' (unchanged)

The existing basePath(c: Context) handler behaviour is fully preserved.

@G4brym G4brym marked this pull request as draft February 24, 2026 12:18
@G4brym G4brym force-pushed the feat/get-base-path branch 2 times, most recently from b148db4 to ebf9231 Compare February 24, 2026 12:24
@G4brym G4brym marked this pull request as ready for review February 24, 2026 12:24
@yusukebe
Copy link
Copy Markdown
Member

yusukebe commented Mar 2, 2026

Hey @G4brym

If you want to get a base path in a handler, you can use the basePath() in the Route Helper: https://hono.dev/docs/helpers/route#basepath

Have you checked it?

@G4brym
Copy link
Copy Markdown
Author

G4brym commented Mar 2, 2026

Hey @yusukebe, thanks for the pointer!
I checked that, but it requires a Context (c) and only works inside a request handler
my use case is different, I need the base path at setup time, outside of a request

for example:

const app = new Hono().basePath('/api')                                                                                                                                                                                                                                                                              
const router = fromHono(app)  // ← need to know "/api" here, no `c` exists yet 

@G4brym
Copy link
Copy Markdown
Author

G4brym commented Mar 2, 2026

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>
@G4brym G4brym force-pushed the feat/get-base-path branch from ebf9231 to 7a32c2d Compare April 12, 2026 11:08
@G4brym G4brym changed the title feat: add getBasePath() public method feat: extend basePath() helper to accept Hono instances Apr 12, 2026
@G4brym
Copy link
Copy Markdown
Author

G4brym commented Apr 12, 2026

Hey there, just updated the pr to match the comment above

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.

2 participants