From d4ca1ce98f33ae67100986613144e9d12fb933b3 Mon Sep 17 00:00:00 2001 From: David Havl Date: Mon, 4 Mar 2024 22:53:44 +0100 Subject: [PATCH] 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 --- .changeset/quiet-shrimps-rescue.md | 5 +++++ packages/zod-openapi/src/index.ts | 2 +- packages/zod-openapi/test/index.test.ts | 9 +++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 .changeset/quiet-shrimps-rescue.md diff --git a/.changeset/quiet-shrimps-rescue.md b/.changeset/quiet-shrimps-rescue.md new file mode 100644 index 00000000..c76c5db2 --- /dev/null +++ b/.changeset/quiet-shrimps-rescue.md @@ -0,0 +1,5 @@ +--- +'@hono/zod-openapi': patch +--- + +fix: Fix basePath method disregarding defaultHook diff --git a/packages/zod-openapi/src/index.ts b/packages/zod-openapi/src/index.ts index 8b6cd248..71166b9b 100644 --- a/packages/zod-openapi/src/index.ts +++ b/packages/zod-openapi/src/index.ts @@ -409,7 +409,7 @@ export class OpenAPIHono< } basePath(path: SubPath): OpenAPIHono> { - return new OpenAPIHono(super.basePath(path) as any) + return new OpenAPIHono({...super.basePath(path) as any, defaultHook: this.defaultHook}) } } diff --git a/packages/zod-openapi/test/index.test.ts b/packages/zod-openapi/test/index.test.ts index ac4b8388..8ce78fa0 100644 --- a/packages/zod-openapi/test/index.test.ts +++ b/packages/zod-openapi/test/index.test.ts @@ -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', () => {