Skip to content

Commit 694522a

Browse files
authored
React query handle 204 or zero content length (#2235)
* fix(openapi-react-query): Handle 204 or zero Content-Length header by returning data as null * fix(openapi-react-query): updated and added 204 & zero Content-Length queryFn tests * chore(openapi-react-query): Fixed linting in test
1 parent 67889ba commit 694522a

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

packages/openapi-react-query/src/index.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,13 @@ export default function createClient<Paths extends {}, Media extends MediaType =
194194
}: QueryFunctionContext<QueryKey<Paths, Method, Path>>) => {
195195
const mth = method.toUpperCase() as Uppercase<typeof method>;
196196
const fn = client[mth] as ClientMethod<Paths, typeof method, Media>;
197-
const { data, error } = await fn(path, { signal, ...(init as any) }); // TODO: find a way to avoid as any
197+
const { data, error, response } = await fn(path, { signal, ...(init as any) }); // TODO: find a way to avoid as any
198198
if (error) {
199199
throw error;
200200
}
201+
if (response.status === 204 || response.headers.get("Content-Length") === "0") {
202+
return data ?? null;
203+
}
201204

202205
return data;
203206
};

packages/openapi-react-query/test/index.test.tsx

+51-1
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ describe("client", () => {
315315
expect(error).toBeNull();
316316
});
317317

318-
it("should resolve error properly and have undefined data when queryFn returns undefined", async () => {
318+
it("handles undefined response with non-zero Content-Length (status 200) by setting error and undefined data", async () => {
319319
const fetchClient = createFetchClient<paths>({ baseUrl });
320320
const client = createClient(fetchClient);
321321

@@ -324,6 +324,9 @@ describe("client", () => {
324324
method: "get",
325325
path: "/string-array",
326326
status: 200,
327+
headers: {
328+
"Content-Length": "10",
329+
},
327330
body: undefined,
328331
});
329332

@@ -337,6 +340,53 @@ describe("client", () => {
337340
expect(data).toBeUndefined();
338341
});
339342

343+
it("handles undefined response with zero Content-Length by setting data and error to null", async () => {
344+
const fetchClient = createFetchClient<paths>({ baseUrl });
345+
const client = createClient(fetchClient);
346+
347+
useMockRequestHandler({
348+
baseUrl,
349+
method: "get",
350+
path: "/string-array",
351+
status: 200,
352+
headers: {
353+
"Content-Length": "0",
354+
},
355+
body: undefined,
356+
});
357+
358+
const { result } = renderHook(() => client.useQuery("get", "/string-array"), { wrapper });
359+
360+
await waitFor(() => expect(result.current.isFetching).toBe(false));
361+
362+
const { data, error } = result.current;
363+
364+
expect(error).toBeNull();
365+
expect(data).toBeNull();
366+
});
367+
368+
it("handles undefined response with 204 No Content status by setting data and error to null", async () => {
369+
const fetchClient = createFetchClient<paths>({ baseUrl });
370+
const client = createClient(fetchClient);
371+
372+
useMockRequestHandler({
373+
baseUrl,
374+
method: "get",
375+
path: "/string-array",
376+
status: 204,
377+
body: undefined,
378+
});
379+
380+
const { result } = renderHook(() => client.useQuery("get", "/string-array"), { wrapper });
381+
382+
await waitFor(() => expect(result.current.isFetching).toBe(false));
383+
384+
const { data, error } = result.current;
385+
386+
expect(error).toBeNull();
387+
expect(data).toBeNull();
388+
});
389+
340390
it("should infer correct data and error type", async () => {
341391
const fetchClient = createFetchClient<paths>({ baseUrl, fetch: fetchInfinite });
342392
const client = createClient(fetchClient);

0 commit comments

Comments
 (0)