From 267977e369f7a84c5d828023e6175729e4dd4c22 Mon Sep 17 00:00:00 2001 From: Djordy Koert Date: Fri, 25 Oct 2024 21:01:27 +0200 Subject: [PATCH] feat: 204 to undefined (#1968) * fix: 204 to undefined * changeset --- .changeset/rude-pumas-wonder.md | 5 +++++ packages/openapi-fetch/src/index.js | 3 +-- .../openapi-fetch/test/http-methods/delete.test.ts | 10 ++++++---- .../test/never-response/never-response.test.ts | 8 ++++---- 4 files changed, 16 insertions(+), 10 deletions(-) create mode 100644 .changeset/rude-pumas-wonder.md diff --git a/.changeset/rude-pumas-wonder.md b/.changeset/rude-pumas-wonder.md new file mode 100644 index 00000000..33ab39dc --- /dev/null +++ b/.changeset/rude-pumas-wonder.md @@ -0,0 +1,5 @@ +--- +"openapi-fetch": minor +--- + +204 responses or response with a Content-Length of 0 will now return undefined instead of an empty object diff --git a/packages/openapi-fetch/src/index.js b/packages/openapi-fetch/src/index.js index 17726464..1b8502c6 100644 --- a/packages/openapi-fetch/src/index.js +++ b/packages/openapi-fetch/src/index.js @@ -151,9 +151,8 @@ export default function createClient(clientOptions) { } // handle empty content - // note: we return `{}` because we want user truthy checks for `.data` or `.error` to succeed if (response.status === 204 || response.headers.get("Content-Length") === "0") { - return response.ok ? { data: {}, response } : { error: {}, response }; + return response.ok ? { data: undefined, response } : { error: undefined, response }; } // parse response (falling back to .text() when necessary) diff --git a/packages/openapi-fetch/test/http-methods/delete.test.ts b/packages/openapi-fetch/test/http-methods/delete.test.ts index 8d5beaaa..9c9cc43e 100644 --- a/packages/openapi-fetch/test/http-methods/delete.test.ts +++ b/packages/openapi-fetch/test/http-methods/delete.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, test } from "vitest"; +import { describe, expect, test, assertType } from "vitest"; import { createObservedClient } from "../helpers.js"; import type { paths } from "./schemas/delete.js"; @@ -10,7 +10,8 @@ describe("DELETE", () => { }); // assert correct data was returned - expect(data).toEqual({}); + assertType(data); + expect(data).toEqual(undefined); expect(response.status).toBe(204); // assert error is empty @@ -27,7 +28,7 @@ describe("DELETE", () => { expect(method).toBe("DELETE"); }); - test("returns empty object on Content-Length: 0", async () => { + test("returns undefined on Content-Length: 0", async () => { const client = createObservedClient( {}, async () => new Response(null, { status: 200, headers: { "Content-Length": "0" } }), @@ -39,7 +40,8 @@ describe("DELETE", () => { }); // assert correct data was returned - expect(data).toEqual({}); + assertType(data); + expect(data).toEqual(undefined); // assert error is empty expect(error).toBeUndefined(); diff --git a/packages/openapi-fetch/test/never-response/never-response.test.ts b/packages/openapi-fetch/test/never-response/never-response.test.ts index bca47fae..43cdb9a4 100644 --- a/packages/openapi-fetch/test/never-response/never-response.test.ts +++ b/packages/openapi-fetch/test/never-response/never-response.test.ts @@ -58,8 +58,8 @@ describe("GET", () => { // assert correct URL was called expect(actualPathname).toBe("/posts/123"); - // assert 204 to be transformed to empty object - expect(data).toEqual({}); + // assert 204 to be transformed to be undefined + expect(data).toEqual(undefined); expect(response.status).toBe(204); // assert error is empty @@ -128,8 +128,8 @@ describe("GET", () => { // assert correct method was called expect(method).toBe("GET"); - // assert 204 to be transformed to empty object - expect(data).toEqual({}); + // assert 204 to be transformed to undefined + expect(data).toEqual(undefined); }); test("gracefully handles invalid JSON for errors", async () => {