fix(zod-openapi): use `JSONParsed` for creating a response type (#576)

* fix(zod-openapi): use `JSONParsed` for creating a response type

* add changeset
pull/577/head
Yusuke Wada 2024-06-15 00:21:44 +09:00 committed by GitHub
parent 6b06138a50
commit 9a9de50494
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 36 additions and 4 deletions

View File

@ -0,0 +1,5 @@
---
'@hono/zod-openapi': patch
---
fix: use `JSONParsed` for creating a response type

View File

@ -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<R extends RouteConfig> = {
> extends never
? TypedResponse<{}, ExtractStatusCode<Status>, string>
: TypedResponse<
ExtractContent<R['responses'][Status]['content']>,
JSONParsed<ExtractContent<R['responses'][Status]['content']>>,
ExtractStatusCode<Status>,
'json'
>

View File

@ -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)
}
)
})
})