fix(route): preserve the base path of the mounted route() app (#4942)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Taku Amano · committed by GitHub · parent 7e62bcd · Browse files · 3 files·+82 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+82 added−3 removed
- tests:+73 added−0 removed
- other:+9 added−3 removed
Provenance
- Commit
dcabbec- Tree
52b618b- Author
Taku Amano <taku@taaas.jp>- Committer
GitHub <noreply@github.com>- Parent
7e62bcd
3 files changed+82 added−3 removed
modified:src/helper/route/index.test.ts+21 added
1 import { Context } from '../../context'2+import { Hono } from '../../hono'3 import { matchedRoutes, routePath, baseRoutePath, basePath } from '.'4 5 const defaultContextOptions = {6 executionCtx: {183 unmodified lines190 191 expect(basePath(c)).toBe('/sub-app-path/foo/app')192 })193 194+ it('should return the fully expanded basePath for nested route() apps', async () => {195+ const app = new Hono()196+ const sub1 = new Hono()197+ const sub2 = new Hono()198+ const sub3 = new Hono()199+200+ sub3.get('/assets/*', (c) => c.json({ baseRoutePath: baseRoutePath(c), basePath: basePath(c) }))201+ sub2.route('/:app/sub3/assets', sub3)202+ sub1.route('/:app/sub2/assets', sub2)203+ app.route('/:app/sub1/assets', sub1)204+205+ const res = await app.request(206+ '/s1/sub1/assets/ss2/sub2/assets/sss3/sub3/assets/assets/file-hoge/assets-kage.js'207+ )208+ expect(await res.json()).toEqual({209+ baseRoutePath: '/:app/sub1/assets/:app/sub2/assets/:app/sub3/assets',210+ basePath: '/s1/sub1/assets/ss2/sub2/assets/sss3/sub3/assets',211+ })212+ })213+214 it('should return basePath with custom regex pattern', () => {215 const handlerA = () => {}216 const rawRequest = new Request('http://localhost?page=2&tag=A&tag=B')217 const c = new Context(rawRequest, {23 unmodified linesmodified:src/hono-base.ts+9 added-3 removed
224 unmodified lines225 (await compose([], app.errorHandler)(c, () => r.handler(c, next))).res226 ;(handler as any)[COMPOSED_HANDLER] = r.handler227 }228 229- subApp.#addRoute(r.method, r.path, handler)229+ subApp.#addRoute(r.method, r.path, handler, r.basePath)230 })231 return this232 }233 147 unmodified lines381 this.#addRoute(METHOD_NAME_ALL, mergePath(path, '*'), handler)382 return this383 }384 385- #addRoute(method: string, path: string, handler: H): void {385+ #addRoute(method: string, path: string, handler: H, baseRoutePath?: string): void {386 method = method.toUpperCase()387 path = mergePath(this._basePath, path)388- const r: RouterRoute = { basePath: this._basePath, path, method, handler }388+ const r: RouterRoute = {389+ basePath:390+ baseRoutePath !== undefined ? mergePath(this._basePath, baseRoutePath) : this._basePath,391+ path,392+ method,393+ handler,394+ }395 this.router.add(method, path, [handler, r])396 this.routes.push(r)397 }398 147 unmodified linesmodified:src/hono.test.ts+52 added
482 unmodified lines483 expect(res.status).toBe(200)484 expect(await res.text()).toBe('get /book')485 })486 487+ describe('Nested route - basePath of the mounted routes', () => {488+ it('Should set basePath to the mount path', () => {489+ const app = new Hono()490+ const sub = new Hono()491+ sub.get('/posts/:id', (c) => c.text('post'))492+ app.route('/:sub', sub)493+494+ expect(app.routes).toEqual([495+ {496+ basePath: '/:sub',497+ method: 'GET',498+ path: '/:sub/posts/:id',499+ handler: expect.any(Function),500+ },501+ ])502+ })503+504+ it('Should accumulate basePath through nested route() calls', () => {505+ const app = new Hono()506+ const sub1 = new Hono()507+ const sub2 = new Hono()508+ sub2.get('/posts/:id', (c) => c.text('post'))509+ sub1.route('/:sub2', sub2)510+ app.route('/:sub1', sub1)511+512+ expect(app.routes).toEqual([513+ {514+ basePath: '/:sub1/:sub2',515+ method: 'GET',516+ path: '/:sub1/:sub2/posts/:id',517+ handler: expect.any(Function),518+ },519+ ])520+ })521+522+ it('Should merge the basePath of a subApp created with basePath()', () => {523+ const app = new Hono()524+ const sub = new Hono().basePath('/book')525+ sub.get('/:id', (c) => c.text('book'))526+ app.route('/api', sub)527+528+ expect(app.routes).toEqual([529+ {530+ basePath: '/api/book',531+ method: 'GET',532+ path: '/api/book/:id',533+ handler: expect.any(Function),534+ },535+ ])536+ })537+ })538+539 it('Multiple route', async () => {540 const app = new Hono()541 542 const book = new Hono()3183 unmodified lines