Skip to content

Commit 7a32c2d

Browse files
G4brymclaude
andcommitted
feat: update basePath() helper to also work with Hono instances
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent b1df304 commit 7a32c2d

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

src/helper/route/index.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Context } from '../../context'
2+
import { Hono } from '../../hono'
23
import { matchedRoutes, routePath, baseRoutePath, basePath } from '.'
34

45
const defaultContextOptions = {
@@ -217,3 +218,27 @@ describe('basePath', () => {
217218
expect(basePath(c)).toBe('/sub-app-path/foo')
218219
})
219220
})
221+
222+
describe('basePath with Hono instance', () => {
223+
it('should return "/" for a fresh Hono instance', () => {
224+
const app = new Hono()
225+
expect(basePath(app)).toBe('/')
226+
})
227+
228+
it('should return the base path after basePath()', () => {
229+
const app = new Hono().basePath('/api')
230+
expect(basePath(app)).toBe('/api')
231+
})
232+
233+
it('should return the merged path after chained basePath() calls', () => {
234+
const app = new Hono().basePath('/api').basePath('/v1')
235+
expect(basePath(app)).toBe('/api/v1')
236+
})
237+
238+
it('should not affect the original instance', () => {
239+
const app = new Hono()
240+
const based = app.basePath('/api')
241+
expect(basePath(app)).toBe('/')
242+
expect(basePath(based)).toBe('/api')
243+
})
244+
})

src/helper/route/index.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import type { Context } from '../../context'
1+
import { Context } from '../../context'
2+
import type { HonoBase } from '../../hono-base'
23
import { GET_MATCH_RESULT } from '../../request/constants'
34
import type { RouterRoute } from '../../types'
45
import { getPattern, splitRoutingPath } from '../../utils/url'
@@ -83,7 +84,7 @@ export const baseRoutePath = (c: Context, index?: number): string =>
8384
matchedRoutes(c).at(index ?? c.req.routeIndex)?.basePath ?? ''
8485

8586
/**
86-
* Get the basePath with embedded parameters
87+
* Get the basePath with embedded parameters, or the base path of a Hono instance.
8788
*
8889
* @param {Context} c - The context object
8990
* @param {number} index - The index of the root from which to retrieve the path, similar to Array.prototype.at(), where a negative number is the index counted from the end of the matching root. Defaults to the current root index.
@@ -102,9 +103,27 @@ export const baseRoutePath = (c: Context, index?: number): string =>
102103
*
103104
* app.route('/:sub', subApp)
104105
* ```
106+
*
107+
* @example
108+
* ```ts
109+
* import { basePath } from 'hono/route'
110+
*
111+
* const subApp = new Hono()
112+
* const path = basePath(subApp) // '/'
113+
*
114+
* const api = new Hono().basePath('/api')
115+
* const apiPath = basePath(api) // '/api'
116+
* ```
105117
*/
106118
const basePathCacheMap: WeakMap<Context, Record<number, string>> = new WeakMap()
107-
export const basePath = (c: Context, index?: number): string => {
119+
export function basePath(app: HonoBase): string
120+
export function basePath(c: Context, index?: number): string
121+
export function basePath(c: Context | HonoBase, index?: number): string {
122+
if (!(c instanceof Context)) {
123+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
124+
return (c as any)._basePath as string
125+
}
126+
108127
index ??= c.req.routeIndex
109128

110129
const cache = basePathCacheMap.get(c) || []

0 commit comments

Comments
 (0)