Skip to content
Open
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
7 changes: 7 additions & 0 deletions .changeset/fix-propagate-undefined-errors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"openapi-react-query": patch
---

fix(openapi-react-query): propagate undefined errors from openapi-fetch

Use `!response.ok` instead of `error` truthiness check to properly handle cases where openapi-fetch returns `error: undefined` for non-OK responses with empty bodies.
10 changes: 5 additions & 5 deletions packages/openapi-react-query/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export default function createClient<Paths extends {}, Media extends MediaType =
const mth = method.toUpperCase() as Uppercase<typeof method>;
const fn = client[mth] as ClientMethod<Paths, typeof method, Media>;
const { data, error, response } = await fn(path, { signal, ...(init as any) }); // TODO: find a way to avoid as any
if (error) {
if (!response.ok) {
throw error;
}
if (response.status === 204 || response.headers.get("Content-Length") === "0") {
Expand Down Expand Up @@ -247,8 +247,8 @@ export default function createClient<Paths extends {}, Media extends MediaType =
},
};

const { data, error } = await fn(path, mergedInit as any);
if (error) {
const { data, error, response } = await fn(path, mergedInit as any);
if (!response.ok) {
throw error;
}
return data;
Expand All @@ -265,8 +265,8 @@ export default function createClient<Paths extends {}, Media extends MediaType =
mutationFn: async (init) => {
const mth = method.toUpperCase() as Uppercase<typeof method>;
const fn = client[mth] as ClientMethod<Paths, typeof method, Media>;
const { data, error } = await fn(path, init as InitWithUnknowns<typeof init>);
if (error) {
const { data, error, response } = await fn(path, init as InitWithUnknowns<typeof init>);
if (!response.ok) {
throw error;
}

Expand Down
27 changes: 27 additions & 0 deletions packages/openapi-react-query/test/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,33 @@ describe("client", () => {
expect(data).toBeUndefined();
});

it("should propagate error when response is not ok with empty body", async () => {
const fetchClient = createFetchClient<paths>({ baseUrl });
const client = createClient(fetchClient);

useMockRequestHandler({
baseUrl,
method: "get",
path: "/string-array",
status: 500,
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).toBeUndefined();
expect(data).toBeUndefined();
});

it("should resolve data properly and have error as null when queryFn returns null", async () => {
const fetchClient = createFetchClient<paths>({ baseUrl });
const client = createClient(fetchClient);
Expand Down
Loading