Skip to content

Commit 448a8fc

Browse files
authored
fix(zod-openapi): infer Env correctly if the middleware is [] (#1106)
* fix(zod-openapi): infer Env correctly if the middleware is `[]` * add changeset
1 parent f349fba commit 448a8fc

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

Diff for: .changeset/dark-kiwis-check.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@hono/zod-openapi': patch
3+
---
4+
5+
fix: infer Env correctly if the middleware is `[]`

Diff for: packages/zod-openapi/src/index.test-d.ts

+21
Original file line numberDiff line numberDiff line change
@@ -337,4 +337,25 @@ describe('Middleware', () => {
337337
}
338338
)
339339
})
340+
341+
it('Should infer Env correctly when the middleware is empty', async () => {
342+
const app = new OpenAPIHono<{ Variables: { foo: string } }>()
343+
app.openapi(
344+
createRoute({
345+
method: 'get',
346+
path: '/books',
347+
middleware: [] as const, // empty
348+
responses: {
349+
200: {
350+
description: 'response',
351+
},
352+
},
353+
}),
354+
(c) => {
355+
const foo = c.get('foo')
356+
type verify = Expect<Equal<typeof foo, string>>
357+
return c.json({})
358+
}
359+
)
360+
})
340361
})

Diff for: packages/zod-openapi/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ export type MiddlewareToHandlerType<M extends MiddlewareHandler<any, any, any>[]
305305
: never
306306
: M extends [infer Last]
307307
? Last // Return the last remaining handler in the array
308-
: never
308+
: MiddlewareHandler<Env>
309309

310310
type RouteMiddlewareParams<R extends RouteConfig> = OfHandlerType<
311311
MiddlewareToHandlerType<AsArray<R['middleware']>>

Diff for: packages/zod-openapi/tsconfig.spec.json

+8-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22
"extends": "../../tsconfig.base.json",
33
"compilerOptions": {
44
"outDir": "../../dist/out-tsc/packages/zod-openapi",
5-
"types": ["vitest/globals"]
5+
"types": [
6+
"vitest/globals"
7+
]
68
},
7-
"include": ["**/*.test.ts"],
9+
"include": [
10+
"**/*.test.ts",
11+
"**/*.test-d.ts"
12+
],
813
"references": [
914
{
1015
"path": "./tsconfig.build.json"
1116
}
1217
]
13-
}
18+
}

0 commit comments

Comments
 (0)