fix(zod-openapi): update RouteHandler type to support MaybePromise (#529)

* fix(zod-openapi): update RouteHandler type to support MaybePromise

* add .changeset/cuddly-pets-drum.md
pull/530/head
akineko 2024-05-19 00:58:15 +09:00 committed by GitHub
parent cd0a2ce6c0
commit 0a43d2c562
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 38 additions and 12 deletions

View File

@ -0,0 +1,5 @@
---
'@hono/zod-openapi': patch
---
fix(zod-openapi): update RouteHandler type to support MaybePromise

View File

@ -197,8 +197,8 @@ export type RouteHandler<
}
}
}
? RouteConfigToTypedResponse<R>
: RouteConfigToTypedResponse<R> | Response | Promise<Response>
? MaybePromise<RouteConfigToTypedResponse<R>>
: MaybePromise<RouteConfigToTypedResponse<R>> | MaybePromise<Response>
>
export type RouteHook<

View File

@ -1,8 +1,7 @@
import type { RouteHandler } from '../src'
import { OpenAPIHono, createRoute, z } from '../src'
test('supports async handler', () => {
const hono = new OpenAPIHono()
describe('supports async handler', () => {
const route = createRoute({
method: 'get',
path: '/users',
@ -20,15 +19,37 @@ test('supports async handler', () => {
},
})
hono.openapi(route, (c) => {
return c.json({
id: '123',
test('argument of openapi method', () => {
const hono = new OpenAPIHono()
hono.openapi(route, (c) => {
return c.json({
id: '123',
})
})
hono.openapi(route, async (c) => {
return c.json({
id: '123',
})
})
})
hono.openapi(route, async (c) => {
return c.json({
id: '123',
})
test('RouteHandler type', () => {
const handler: RouteHandler<typeof route> = (c) => {
return c.json({
id: '123',
})
}
const asyncHandler: RouteHandler<typeof route> = async (c) => {
return c.json({
id: '123',
})
}
const hono = new OpenAPIHono()
hono.openapi(route, handler)
hono.openapi(route, asyncHandler)
})
})