fix(zod-openapi): use `JSONParsed` for creating a response type (#576)
* fix(zod-openapi): use `JSONParsed` for creating a response type * add changesetpull/577/head
parent
6b06138a50
commit
9a9de50494
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'@hono/zod-openapi': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix: use `JSONParsed` for creating a response type
|
|
@ -27,6 +27,7 @@ import type {
|
||||||
TypedResponse,
|
TypedResponse,
|
||||||
} from 'hono'
|
} from 'hono'
|
||||||
import type { MergePath, MergeSchemaPath } from 'hono/types'
|
import type { MergePath, MergeSchemaPath } from 'hono/types'
|
||||||
|
import type { JSONParsed, RemoveBlankRecord } from 'hono/utils/types'
|
||||||
import type {
|
import type {
|
||||||
ClientErrorStatusCode,
|
ClientErrorStatusCode,
|
||||||
InfoStatusCode,
|
InfoStatusCode,
|
||||||
|
@ -35,7 +36,6 @@ import type {
|
||||||
StatusCode,
|
StatusCode,
|
||||||
SuccessStatusCode,
|
SuccessStatusCode,
|
||||||
} from 'hono/utils/http-status'
|
} from 'hono/utils/http-status'
|
||||||
import type { RemoveBlankRecord } from 'hono/utils/types'
|
|
||||||
import { mergePath } from 'hono/utils/url'
|
import { mergePath } from 'hono/utils/url'
|
||||||
import type { AnyZodObject, ZodError, ZodSchema } from 'zod'
|
import type { AnyZodObject, ZodError, ZodSchema } from 'zod'
|
||||||
import { ZodType, z } from 'zod'
|
import { ZodType, z } from 'zod'
|
||||||
|
@ -167,7 +167,7 @@ export type RouteConfigToTypedResponse<R extends RouteConfig> = {
|
||||||
> extends never
|
> extends never
|
||||||
? TypedResponse<{}, ExtractStatusCode<Status>, string>
|
? TypedResponse<{}, ExtractStatusCode<Status>, string>
|
||||||
: TypedResponse<
|
: TypedResponse<
|
||||||
ExtractContent<R['responses'][Status]['content']>,
|
JSONParsed<ExtractContent<R['responses'][Status]['content']>>,
|
||||||
ExtractStatusCode<Status>,
|
ExtractStatusCode<Status>,
|
||||||
'json'
|
'json'
|
||||||
>
|
>
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import type { Hono, Env, ToSchema } from 'hono'
|
import type { Env, Hono, ToSchema } from 'hono'
|
||||||
import { describe, it, expectTypeOf, assertType } from 'vitest'
|
import { assertType, describe, expectTypeOf, it } from 'vitest'
|
||||||
import { OpenAPIHono, createRoute, z } from '../src/index'
|
import { OpenAPIHono, createRoute, z } from '../src/index'
|
||||||
|
|
||||||
describe('Types', () => {
|
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)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
Loading…
Reference in New Issue