From 448a8fc687cca2bcab2353ea4237f1293706d5e2 Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Mon, 7 Apr 2025 18:41:04 +0900 Subject: [PATCH] fix(zod-openapi): infer Env correctly if the middleware is `[]` (#1106) * fix(zod-openapi): infer Env correctly if the middleware is `[]` * add changeset --- .changeset/dark-kiwis-check.md | 5 +++++ packages/zod-openapi/src/index.test-d.ts | 21 +++++++++++++++++++++ packages/zod-openapi/src/index.ts | 2 +- packages/zod-openapi/tsconfig.spec.json | 11 ++++++++--- 4 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 .changeset/dark-kiwis-check.md diff --git a/.changeset/dark-kiwis-check.md b/.changeset/dark-kiwis-check.md new file mode 100644 index 00000000..58b4a664 --- /dev/null +++ b/.changeset/dark-kiwis-check.md @@ -0,0 +1,5 @@ +--- +'@hono/zod-openapi': patch +--- + +fix: infer Env correctly if the middleware is `[]` diff --git a/packages/zod-openapi/src/index.test-d.ts b/packages/zod-openapi/src/index.test-d.ts index 99865105..e0127e9c 100644 --- a/packages/zod-openapi/src/index.test-d.ts +++ b/packages/zod-openapi/src/index.test-d.ts @@ -337,4 +337,25 @@ describe('Middleware', () => { } ) }) + + it('Should infer Env correctly when the middleware is empty', async () => { + const app = new OpenAPIHono<{ Variables: { foo: string } }>() + app.openapi( + createRoute({ + method: 'get', + path: '/books', + middleware: [] as const, // empty + responses: { + 200: { + description: 'response', + }, + }, + }), + (c) => { + const foo = c.get('foo') + type verify = Expect> + return c.json({}) + } + ) + }) }) diff --git a/packages/zod-openapi/src/index.ts b/packages/zod-openapi/src/index.ts index f42a2f68..6e477c34 100644 --- a/packages/zod-openapi/src/index.ts +++ b/packages/zod-openapi/src/index.ts @@ -305,7 +305,7 @@ export type MiddlewareToHandlerType[] : never : M extends [infer Last] ? Last // Return the last remaining handler in the array - : never + : MiddlewareHandler type RouteMiddlewareParams = OfHandlerType< MiddlewareToHandlerType> diff --git a/packages/zod-openapi/tsconfig.spec.json b/packages/zod-openapi/tsconfig.spec.json index 191c0270..a1c2f484 100644 --- a/packages/zod-openapi/tsconfig.spec.json +++ b/packages/zod-openapi/tsconfig.spec.json @@ -2,12 +2,17 @@ "extends": "../../tsconfig.base.json", "compilerOptions": { "outDir": "../../dist/out-tsc/packages/zod-openapi", - "types": ["vitest/globals"] + "types": [ + "vitest/globals" + ] }, - "include": ["**/*.test.ts"], + "include": [ + "**/*.test.ts", + "**/*.test-d.ts" + ], "references": [ { "path": "./tsconfig.build.json" } ] -} +} \ No newline at end of file