fix(zod-openapi): BasePath not included in client types (#429)

fixes #428
pull/430/head
David 2024-03-22 01:23:53 -07:00 committed by GitHub
parent 6bdbf88854
commit b1f8a5325c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 18 additions and 3 deletions

View File

@ -0,0 +1,5 @@
---
'@hono/zod-openapi': patch
---
fix: base path not included in client types

View File

@ -250,7 +250,7 @@ export class OpenAPIHono<
: HandlerAllResponse<OutputType<R>> : HandlerAllResponse<OutputType<R>>
>, >,
hook: Hook<I, E, P, OutputType<R>> | undefined = this.defaultHook hook: Hook<I, E, P, OutputType<R>> | undefined = this.defaultHook
): OpenAPIHono<E, S & ToSchema<R['method'], P, I['in'], OutputType<R>>, BasePath> => { ): OpenAPIHono<E, S & ToSchema<R['method'], MergePath<BasePath, P>, I['in'], OutputType<R>>, BasePath> => {
this.openAPIRegistry.registerPath(route) this.openAPIRegistry.registerPath(route)
const validators: MiddlewareHandler[] = [] const validators: MiddlewareHandler[] = []

View File

@ -44,7 +44,7 @@ describe('Types', () => {
const appRoutes = app.openapi(route, (c) => { const appRoutes = app.openapi(route, (c) => {
const data = c.req.valid('json') const data = c.req.valid('json')
assertType<number>(data.id) assertType<number>(data.id)
return c.jsonT({ return c.json({
id: data.id, id: data.id,
message: 'Success', message: 'Success',
}) })
@ -70,7 +70,7 @@ describe('Types', () => {
'/' '/'
> >
expectTypeOf(appRoutes).toMatchTypeOf<H>() expectTypeOf(appRoutes).toMatchTypeOf<H>()
}) });
}) })
describe('Input types', () => { describe('Input types', () => {

View File

@ -832,6 +832,16 @@ describe('basePath()', () => {
expect(app.defaultHook).toBeDefined() expect(app.defaultHook).toBeDefined()
expect(app.defaultHook).toBe(defaultHook) expect(app.defaultHook).toBe(defaultHook)
}) })
it('Should include base path in typings', () => {
const routes = new OpenAPIHono()
.basePath('/api')
.openapi(route, (c) => c.json({ message: 'Hello' }))
const client = hc<typeof routes>('http://localhost/')
expect(client.api.message.$url().pathname).toBe('/api/message')
})
}) })
describe('With hc', () => { describe('With hc', () => {