Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 204 to undefined #1968

Merged
merged 2 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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