From 1bab84e525d2ce0872d0cac60c2d6b86a8366dde Mon Sep 17 00:00:00 2001 From: Joakim Allen Date: Fri, 28 Mar 2025 19:18:34 +0100 Subject: [PATCH 1/3] fix(openapi-react-query): Handle 204 or zero Content-Length header by returning data as null --- packages/openapi-react-query/src/index.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/openapi-react-query/src/index.ts b/packages/openapi-react-query/src/index.ts index 855b249c9..d0e502427 100644 --- a/packages/openapi-react-query/src/index.ts +++ b/packages/openapi-react-query/src/index.ts @@ -191,10 +191,13 @@ export default function createClient>) => { const mth = method.toUpperCase() as Uppercase; const fn = client[mth] as ClientMethod; - const { data, error } = await fn(path, { signal, ...(init as any) }); // TODO: find a way to avoid as any + const { data, error, response } = await fn(path, { signal, ...(init as any) }); // TODO: find a way to avoid as any if (error) { throw error; } + if (response.status === 204 || response.headers.get("Content-Length") === "0") { + return data ?? null; + } return data; }; From f666269ad613fc48b4d12a34b456e841d1f86629 Mon Sep 17 00:00:00 2001 From: Joakim Allen Date: Fri, 28 Mar 2025 19:20:05 +0100 Subject: [PATCH 2/3] fix(openapi-react-query): updated and added 204 & zero Content-Length queryFn tests --- .../openapi-react-query/test/index.test.tsx | 52 ++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/packages/openapi-react-query/test/index.test.tsx b/packages/openapi-react-query/test/index.test.tsx index 5ccda7a42..ae0aa20c9 100644 --- a/packages/openapi-react-query/test/index.test.tsx +++ b/packages/openapi-react-query/test/index.test.tsx @@ -315,7 +315,7 @@ describe("client", () => { expect(error).toBeNull(); }); - it("should resolve error properly and have undefined data when queryFn returns undefined", async () => { + it("handles undefined response with non-zero Content-Length (status 200) by setting error and undefined data", async () => { const fetchClient = createFetchClient({ baseUrl }); const client = createClient(fetchClient); @@ -324,6 +324,9 @@ describe("client", () => { method: "get", path: "/string-array", status: 200, + headers: { + "Content-Length": "10" + }, body: undefined, }); @@ -337,6 +340,53 @@ describe("client", () => { expect(data).toBeUndefined(); }); + it("handles undefined response with zero Content-Length by setting data and error to null", async () => { + const fetchClient = createFetchClient({ baseUrl }); + const client = createClient(fetchClient); + + useMockRequestHandler({ + baseUrl, + method: "get", + path: "/string-array", + status: 200, + headers: { + "Content-Length": "0" + }, + body: undefined, + }); + + const { result } = renderHook(() => client.useQuery("get", "/string-array"), { wrapper }); + + await waitFor(() => expect(result.current.isFetching).toBe(false)); + + const { data, error } = result.current; + + expect(error).toBeNull(); + expect(data).toBeNull(); + }); + + it("handles undefined response with 204 No Content status by setting data and error to null", async () => { + const fetchClient = createFetchClient({ baseUrl }); + const client = createClient(fetchClient); + + useMockRequestHandler({ + baseUrl, + method: "get", + path: "/string-array", + status: 204, + body: undefined, + }); + + const { result } = renderHook(() => client.useQuery("get", "/string-array"), { wrapper }); + + await waitFor(() => expect(result.current.isFetching).toBe(false)); + + const { data, error } = result.current; + + expect(error).toBeNull(); + expect(data).toBeNull(); + }); + it("should infer correct data and error type", async () => { const fetchClient = createFetchClient({ baseUrl, fetch: fetchInfinite }); const client = createClient(fetchClient); From 5e165342a26f12265c18d43a5e7fbf569e6987de Mon Sep 17 00:00:00 2001 From: Wheelebin Date: Fri, 28 Mar 2025 19:43:16 +0100 Subject: [PATCH 3/3] chore(openapi-react-query): Fixed linting in test --- packages/openapi-react-query/test/index.test.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/openapi-react-query/test/index.test.tsx b/packages/openapi-react-query/test/index.test.tsx index ae0aa20c9..c2d3a0321 100644 --- a/packages/openapi-react-query/test/index.test.tsx +++ b/packages/openapi-react-query/test/index.test.tsx @@ -325,7 +325,7 @@ describe("client", () => { path: "/string-array", status: 200, headers: { - "Content-Length": "10" + "Content-Length": "10", }, body: undefined, }); @@ -350,7 +350,7 @@ describe("client", () => { path: "/string-array", status: 200, headers: { - "Content-Length": "0" + "Content-Length": "0", }, body: undefined, });