Skip to content

Commit

Permalink
feat(zod-openapi): allows the response to be Response (#206)
Browse files Browse the repository at this point in the history
* feat: allows the response to be `Response`

* add changeset
  • Loading branch information
yusukebe authored Oct 22, 2023
1 parent a593d31 commit 2d2fdd0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/tough-beds-speak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hono/zod-openapi': minor
---

feat: allows the response to be `Response` not just `TypedResponse`.
8 changes: 6 additions & 2 deletions packages/zod-openapi/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
25 changes: 25 additions & 0 deletions packages/zod-openapi/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})

0 comments on commit 2d2fdd0

Please sign in to comment.