diff --git a/.changeset/modern-buttons-impress.md b/.changeset/modern-buttons-impress.md new file mode 100644 index 00000000..920c5f05 --- /dev/null +++ b/.changeset/modern-buttons-impress.md @@ -0,0 +1,5 @@ +--- +"@hono/zod-openapi": patch +--- + +fix(zod-openapi): return type of handler should be MaybePromise diff --git a/packages/zod-openapi/src/index.ts b/packages/zod-openapi/src/index.ts index 57b3eb25..4372975c 100644 --- a/packages/zod-openapi/src/index.ts +++ b/packages/zod-openapi/src/index.ts @@ -27,12 +27,14 @@ import type { TypedResponse, } from 'hono' 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 { mergePath } from 'hono/utils/url' import type { AnyZodObject, ZodSchema, ZodError } from 'zod' import { z, ZodType } from 'zod' +type MaybePromise = Promise | T + type RouteConfig = RouteConfigBase & { middleware?: MiddlewareHandler | MiddlewareHandler[] } @@ -290,8 +292,8 @@ export class OpenAPIHono< } } } - ? RouteConfigToTypedResponse - : RouteConfigToTypedResponse | Response | Promise + ? MaybePromise> + : MaybePromise> | MaybePromise >, hook: | Hook< diff --git a/packages/zod-openapi/test/handler.test-d.ts b/packages/zod-openapi/test/handler.test-d.ts new file mode 100644 index 00000000..2eaf5429 --- /dev/null +++ b/packages/zod-openapi/test/handler.test-d.ts @@ -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', + }) + }) +})