diff --git a/.changeset/curly-pears-act.md b/.changeset/curly-pears-act.md new file mode 100644 index 00000000..f141e31a --- /dev/null +++ b/.changeset/curly-pears-act.md @@ -0,0 +1,5 @@ +--- +'@hono/zod-openapi': patch +--- + +fix: use `JSONParsed` for creating a response type diff --git a/packages/zod-openapi/src/index.ts b/packages/zod-openapi/src/index.ts index 95dd29f1..319540b8 100644 --- a/packages/zod-openapi/src/index.ts +++ b/packages/zod-openapi/src/index.ts @@ -27,6 +27,7 @@ import type { TypedResponse, } from 'hono' import type { MergePath, MergeSchemaPath } from 'hono/types' +import type { JSONParsed, RemoveBlankRecord } from 'hono/utils/types' import type { ClientErrorStatusCode, InfoStatusCode, @@ -35,7 +36,6 @@ import type { StatusCode, SuccessStatusCode, } from 'hono/utils/http-status' -import type { RemoveBlankRecord } from 'hono/utils/types' import { mergePath } from 'hono/utils/url' import type { AnyZodObject, ZodError, ZodSchema } from 'zod' import { ZodType, z } from 'zod' @@ -167,7 +167,7 @@ export type RouteConfigToTypedResponse = { > extends never ? TypedResponse<{}, ExtractStatusCode, string> : TypedResponse< - ExtractContent, + JSONParsed>, ExtractStatusCode, 'json' > diff --git a/packages/zod-openapi/test/index.test-d.ts b/packages/zod-openapi/test/index.test-d.ts index 062c04be..7386e12f 100644 --- a/packages/zod-openapi/test/index.test-d.ts +++ b/packages/zod-openapi/test/index.test-d.ts @@ -1,5 +1,5 @@ -import type { Hono, Env, ToSchema } from 'hono' -import { describe, it, expectTypeOf, assertType } from 'vitest' +import type { Env, Hono, ToSchema } from 'hono' +import { assertType, describe, expectTypeOf, it } from 'vitest' import { OpenAPIHono, createRoute, z } from '../src/index' describe('Types', () => { @@ -171,3 +171,30 @@ describe('Input types', () => { }) }) }) + +describe('Response schema includes a Date type', () => { + it('Should not throw a type error', () => { + new OpenAPIHono().openapi( + createRoute({ + method: 'get', + path: '/example', + responses: { + 200: { + content: { + 'application/json': { + schema: z.object({ + updatedAt: z.date(), + }), + }, + }, + description: '', + }, + }, + }), + async (ctx) => { + // Don't throw an error: + return ctx.json({ updatedAt: new Date() }, 200) + } + ) + }) +})