diff --git a/.changeset/tough-beds-speak.md b/.changeset/tough-beds-speak.md new file mode 100644 index 00000000..c63aaf4a --- /dev/null +++ b/.changeset/tough-beds-speak.md @@ -0,0 +1,5 @@ +--- +'@hono/zod-openapi': minor +--- + +feat: allows the response to be `Response` not just `TypedResponse`. diff --git a/packages/zod-openapi/src/index.ts b/packages/zod-openapi/src/index.ts index 4ba8d745..ce38933d 100644 --- a/packages/zod-openapi/src/index.ts +++ b/packages/zod-openapi/src/index.ts @@ -143,13 +143,17 @@ type Hook = ( error: ZodError }, c: Context -) => TypedResponse | Promise> | void +) => TypedResponse | Promise> | Response | Promise | void type ConvertPathType = T extends `${infer Start}/{${infer Param}}${infer Rest}` ? `${Start}/:${Param}${ConvertPathType}` : T -type HandlerResponse = TypedResponse | Promise> +type HandlerResponse = + | TypedResponse + | Promise> + | Response + | Promise export type OpenAPIHonoOptions = { defaultHook?: Hook diff --git a/packages/zod-openapi/test/index.test.ts b/packages/zod-openapi/test/index.test.ts index be036d76..b8ed6262 100644 --- a/packages/zod-openapi/test/index.test.ts +++ b/packages/zod-openapi/test/index.test.ts @@ -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) + }) +})