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
5 changes: 5 additions & 0 deletions .changeset/propagate-empty-error-response.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-react-query": patch
---

fix(openapi-react-query): propagate undefined errors from empty-body non-OK responses
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 (error !== undefined || !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 (error !== undefined || !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 (error !== undefined || !response.ok) {
throw error;
}

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

it("should propagate undefined errors from empty non-OK responses", 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,
});

await expect(queryClient.fetchQuery(client.queryOptions("get", "/string-array"))).rejects.toBeUndefined();
});

it("should infer correct data and error type", async () => {
const fetchClient = createFetchClient<paths>({ baseUrl, fetch: fetchInfinite });
const client = createClient(fetchClient);
Expand Down Expand Up @@ -902,6 +920,28 @@ describe("client", () => {
await expect(result.current.mutateAsync({ body: { message: "Hello", replied_at: 0 } })).rejects.toThrow();
});

it("should propagate undefined errors from empty non-OK responses", async () => {
const fetchClient = createFetchClient<paths>({ baseUrl });
const client = createClient(fetchClient);

useMockRequestHandler({
baseUrl,
method: "put",
path: "/comment",
status: 500,
headers: {
"Content-Length": "0",
},
body: undefined,
});

const { result } = renderHook(() => client.useMutation("put", "/comment"), {
wrapper,
});

await expect(result.current.mutateAsync({ body: { message: "Hello", replied_at: 0 } })).rejects.toBeUndefined();
});

it("should use provided custom queryClient", async () => {
const fetchClient = createFetchClient<paths>({ baseUrl });
const client = createClient(fetchClient);
Expand Down Expand Up @@ -1268,5 +1308,46 @@ describe("client", () => {

expect(result.current.data).toEqual([1, 2, 3, 4, 5, 6]);
});

it("should propagate undefined errors from empty non-OK responses", async () => {
const fetchClient = createFetchClient<paths>({ baseUrl });
const client = createClient(fetchClient);

useMockRequestHandler({
baseUrl,
method: "get",
path: "/paginated-data",
status: 500,
headers: {
"Content-Length": "0",
},
body: undefined,
});

const { result } = renderHook(
() =>
client.useInfiniteQuery(
"get",
"/paginated-data",
{
params: {
query: {
limit: 3,
},
},
},
{
getNextPageParam: (lastPage) => lastPage.nextPage,
initialPageParam: 0,
},
),
{ wrapper },
);

await waitFor(() => expect(result.current.isError).toBe(true));

expect(result.current.error).toBeUndefined();
expect(result.current.data).toBeUndefined();
});
});
});
Loading