Skip to content

Commit

Permalink
feat: 204 to undefined (#1968)
Browse files Browse the repository at this point in the history
* fix: 204 to undefined

* changeset
  • Loading branch information
DjordyKoert authored Oct 25, 2024
1 parent f50d5c2 commit 267977e
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/rude-pumas-wonder.md
Original file line number Diff line number Diff line change
@@ -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
3 changes: 1 addition & 2 deletions packages/openapi-fetch/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 6 additions & 4 deletions packages/openapi-fetch/test/http-methods/delete.test.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -10,7 +10,8 @@ describe("DELETE", () => {
});

// assert correct data was returned
expect(data).toEqual({});
assertType<undefined>(data);
expect(data).toEqual(undefined);
expect(response.status).toBe(204);

// assert error is empty
Expand All @@ -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<paths>(
{},
async () => new Response(null, { status: 200, headers: { "Content-Length": "0" } }),
Expand All @@ -39,7 +40,8 @@ describe("DELETE", () => {
});

// assert correct data was returned
expect(data).toEqual({});
assertType<undefined>(data);
expect(data).toEqual(undefined);

// assert error is empty
expect(error).toBeUndefined();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 () => {
Expand Down

0 comments on commit 267977e

Please sign in to comment.