diff --git a/.changeset/fresh-pillows-hear.md b/.changeset/fresh-pillows-hear.md new file mode 100644 index 00000000..12bd0112 --- /dev/null +++ b/.changeset/fresh-pillows-hear.md @@ -0,0 +1,5 @@ +--- +'@hono/zod-openapi': patch +--- + +fix(zod-openapi): enable `basePath()` diff --git a/packages/zod-openapi/src/index.ts b/packages/zod-openapi/src/index.ts index 4c7f1a12..04362b44 100644 --- a/packages/zod-openapi/src/index.ts +++ b/packages/zod-openapi/src/index.ts @@ -347,6 +347,10 @@ export class OpenAPIHono< // eslint-disable-next-line @typescript-eslint/no-explicit-any return this as any } + + basePath(path: SubPath): OpenAPIHono> { + return new OpenAPIHono(super.basePath(path)) + } } type RoutingPath

= P extends `${infer Head}/{${infer Param}}${infer Tail}` diff --git a/packages/zod-openapi/test/index.test.ts b/packages/zod-openapi/test/index.test.ts index 65c0fa78..a0e20d1c 100644 --- a/packages/zod-openapi/test/index.test.ts +++ b/packages/zod-openapi/test/index.test.ts @@ -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()