fix(zod-openapi): infer Env correctly if the middleware is `[]` (#1106)

* fix(zod-openapi): infer Env correctly if the middleware is `[]`

* add changeset
pull/1107/head
Yusuke Wada 2025-04-07 18:41:04 +09:00 committed by GitHub
parent f349fba499
commit 448a8fc687
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 35 additions and 4 deletions

View File

@ -0,0 +1,5 @@
---
'@hono/zod-openapi': patch
---
fix: infer Env correctly if the middleware is `[]`

View File

@ -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<Equal<typeof foo, string>>
return c.json({})
}
)
})
})

View File

@ -305,7 +305,7 @@ export type MiddlewareToHandlerType<M extends MiddlewareHandler<any, any, any>[]
: never
: M extends [infer Last]
? Last // Return the last remaining handler in the array
: never
: MiddlewareHandler<Env>
type RouteMiddlewareParams<R extends RouteConfig> = OfHandlerType<
MiddlewareToHandlerType<AsArray<R['middleware']>>

View File

@ -2,9 +2,14 @@
"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"