fix(zod-openapi): Fix basePath method to transfer defaultHook of the parent app. (#408)

* Fix basePath method to transfer defaultHook of the parent app.

* Revert back variable typing

* Add changeset for @hono/zod-openapi
pull/409/head
David Havl 2024-03-04 22:53:44 +01:00 committed by GitHub
parent 5c0d4adf62
commit d4ca1ce98f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 15 additions and 1 deletions

View File

@ -0,0 +1,5 @@
---
'@hono/zod-openapi': patch
---
fix: Fix basePath method disregarding defaultHook

View File

@ -409,7 +409,7 @@ export class OpenAPIHono<
}
basePath<SubPath extends string>(path: SubPath): OpenAPIHono<E, S, MergePath<BasePath, SubPath>> {
return new OpenAPIHono(super.basePath(path) as any)
return new OpenAPIHono({...super.basePath(path) as any, defaultHook: this.defaultHook})
}
}

View File

@ -823,6 +823,15 @@ describe('basePath()', () => {
const res = await app.request('/api/doc')
expect(res.status).toBe(200)
})
it('Should retain defaultHook of the parent app', async () => {
const defaultHook = () => {}
const app = new OpenAPIHono({
defaultHook
}).basePath('/api')
expect(app.defaultHook).toBeDefined()
expect(app.defaultHook).toBe(defaultHook)
})
})
describe('With hc', () => {