fix(zod-openapi): enable `basePath()` (#179)

* fix(zod-openapi): enable `basePath()`

* add changeset
pull/177/head
Yusuke Wada 2023-09-26 17:16:36 +09:00 committed by GitHub
parent 9c45dbc41d
commit 047eca5ca9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 0 deletions

View File

@ -0,0 +1,5 @@
---
'@hono/zod-openapi': patch
---
fix(zod-openapi): enable `basePath()`

View File

@ -347,6 +347,10 @@ export class OpenAPIHono<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return this as any
}
basePath<SubPath extends string>(path: SubPath): OpenAPIHono<E, S, MergePath<BasePath, SubPath>> {
return new OpenAPIHono(super.basePath(path))
}
}
type RoutingPath<P extends string> = P extends `${infer Head}/{${infer Param}}${infer Tail}`

View File

@ -728,6 +728,39 @@ describe('Multi params', () => {
})
})
describe('basePath()', () => {
const route = createRoute({
method: 'get',
path: '/message',
responses: {
200: {
description: 'Get message',
},
},
})
const app = new OpenAPIHono().basePath('/api')
app.openapi(route, (c) => c.jsonT({ message: 'Hello' }))
app.doc('/doc', {
openapi: '3.0.0',
info: {
version: '1.0.0',
title: 'My API',
},
})
it('Should return 200 response without type errors - /api/message', async () => {
const res = await app.request('/api/message')
expect(res.status).toBe(200)
expect(await res.json()).toEqual({ message: 'Hello' })
})
it('Should return 200 response - /api/doc', async () => {
const res = await app.request('/api/doc')
expect(res.status).toBe(200)
})
})
describe('With hc', () => {
describe('Multiple routes', () => {
const app = new OpenAPIHono()