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> ? MaybePromise<RouteConfigToTypedResponse<R>>
: RouteConfigToTypedResponse<R> | Response | Promise<Response> : MaybePromise<RouteConfigToTypedResponse<R>> | MaybePromise<Response>
> >
export type RouteHook< export type RouteHook<

View File

@ -1,8 +1,7 @@
import type { RouteHandler } from '../src'
import { OpenAPIHono, createRoute, z } from '../src' import { OpenAPIHono, createRoute, z } from '../src'
test('supports async handler', () => { describe('supports async handler', () => {
const hono = new OpenAPIHono()
const route = createRoute({ const route = createRoute({
method: 'get', method: 'get',
path: '/users', path: '/users',
@ -20,6 +19,9 @@ test('supports async handler', () => {
}, },
}) })
test('argument of openapi method', () => {
const hono = new OpenAPIHono()
hono.openapi(route, (c) => { hono.openapi(route, (c) => {
return c.json({ return c.json({
id: '123', id: '123',
@ -31,4 +33,23 @@ test('supports async handler', () => {
id: '123', 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)
})
}) })