From 2d2fdd0379a31320b06f6d1a9e4634bfe1b7c657 Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Sun, 22 Oct 2023 12:05:43 +0900 Subject: [PATCH] feat(zod-openapi): allows the response to be `Response` (#206) * feat: allows the response to be `Response` * add changeset --- .changeset/tough-beds-speak.md | 5 +++++ packages/zod-openapi/src/index.ts | 8 ++++++-- packages/zod-openapi/test/index.test.ts | 25 +++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 .changeset/tough-beds-speak.md diff --git a/.changeset/tough-beds-speak.md b/.changeset/tough-beds-speak.md new file mode 100644 index 000000000..c63aaf4a8 --- /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 4ba8d7451..ce38933dc 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 be036d768..b8ed6262b 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) + }) +})