feat(zod-openapi): allows the response to be `Response` (#206)

* feat: allows the response to be `Response`

* add changeset
pull/207/head
Yusuke Wada 2023-10-22 12:05:43 +09:00 committed by GitHub
parent a593d311f5
commit 2d2fdd0379
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 2 deletions

View File

@ -0,0 +1,5 @@
---
'@hono/zod-openapi': minor
---
feat: allows the response to be `Response` not just `TypedResponse`.

View File

@ -143,13 +143,17 @@ type Hook<T, E extends Env, P extends string, O> = (
error: ZodError
},
c: Context<E, P>
) => TypedResponse<O> | Promise<TypedResponse<T>> | void
) => TypedResponse<O> | Promise<TypedResponse<T>> | Response | Promise<Response> | void
type ConvertPathType<T extends string> = T extends `${infer Start}/{${infer Param}}${infer Rest}`
? `${Start}/:${Param}${ConvertPathType<Rest>}`
: T
type HandlerResponse<O> = TypedResponse<O> | Promise<TypedResponse<O>>
type HandlerResponse<O> =
| TypedResponse<O>
| Promise<TypedResponse<O>>
| Response
| Promise<Response>
export type OpenAPIHonoOptions<E extends Env> = {
defaultHook?: Hook<any, E, any, any>

View File

@ -946,3 +946,28 @@ describe('With hc', () => {
})
})
})
describe('It allows the response type to be Response', () => {
const app = new OpenAPIHono()
app.openapi(
createRoute({
method: 'get',
path: '/no-content',
responses: {
204: {
description: 'No Content',
},
},
}),
(c) => {
return c.body(null, 204)
}
)
it('should return a 204 response without a type error', async () => {
const res = await app.request('/no-content')
expect(res.status).toBe(204)
expect(res.body).toBe(null)
})
})