fix(zod-openapi): return type of handler should be MaybePromise (#522)

* fix(zod-openapi): return type of handler should be MaybePromise

* revert: changes

* Create modern-buttons-impress.md

* fix: types
pull/523/head
Alex 2024-05-15 15:51:34 +08:00 committed by GitHub
parent c5001c01e7
commit 2d5ef82558
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 44 additions and 3 deletions

View File

@ -0,0 +1,5 @@
---
"@hono/zod-openapi": patch
---
fix(zod-openapi): return type of handler should be MaybePromise

View File

@ -27,12 +27,14 @@ import type {
TypedResponse, TypedResponse,
} from 'hono' } from 'hono'
import type { MergePath, MergeSchemaPath } from 'hono/types' import type { MergePath, MergeSchemaPath } from 'hono/types'
import { StatusCode } from 'hono/utils/http-status' import type { StatusCode } from 'hono/utils/http-status'
import type { Prettify, RemoveBlankRecord } from 'hono/utils/types' import type { Prettify, RemoveBlankRecord } from 'hono/utils/types'
import { mergePath } from 'hono/utils/url' import { mergePath } from 'hono/utils/url'
import type { AnyZodObject, ZodSchema, ZodError } from 'zod' import type { AnyZodObject, ZodSchema, ZodError } from 'zod'
import { z, ZodType } from 'zod' import { z, ZodType } from 'zod'
type MaybePromise<T> = Promise<T> | T
type RouteConfig = RouteConfigBase & { type RouteConfig = RouteConfigBase & {
middleware?: MiddlewareHandler | MiddlewareHandler[] middleware?: MiddlewareHandler | MiddlewareHandler[]
} }
@ -290,8 +292,8 @@ export class OpenAPIHono<
} }
} }
} }
? RouteConfigToTypedResponse<R> ? MaybePromise<RouteConfigToTypedResponse<R>>
: RouteConfigToTypedResponse<R> | Response | Promise<Response> : MaybePromise<RouteConfigToTypedResponse<R>> | MaybePromise<Response>
>, >,
hook: hook:
| Hook< | Hook<

View File

@ -0,0 +1,34 @@
import { OpenAPIHono, createRoute, z } from '../src'
test('supports async handler', () => {
const hono = new OpenAPIHono()
const route = createRoute({
method: 'get',
path: '/users',
responses: {
200: {
content: {
'application/json': {
schema: z.object({
id: z.string(),
}),
},
},
description: 'Retrieve the user',
},
},
})
hono.openapi(route, (c) => {
return c.json({
id: '123',
})
})
hono.openapi(route, async (c) => {
return c.json({
id: '123',
})
})
})