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
2 changes: 1 addition & 1 deletion packages/openapi-react-query/biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"$schema": "https://biomejs.dev/schemas/2.3.14/schema.json",
"extends": "//",
"files": {
"includes": ["**", "!dist/**", "!test/fixtures/**"]
"includes": ["**", "!dist", "!test/fixtures"]
},
"linter": {
"rules": {
Expand Down
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
91 changes: 86 additions & 5 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 @@ -847,10 +865,10 @@ describe("client", () => {
() =>
client.useMutation("put", "/comment", {
onMutate: () => onMutateReturnValue,
onError: (err, _, onMutateResult, context) => {
onError: (_err, _, onMutateResult, _context) => {
assertType<expectedOnMutateResultType>(onMutateResult);
},
onSettled: (_data, _error, _variables, onMutateResult, context) => {
onSettled: (_data, _error, _variables, onMutateResult, _context) => {
assertType<expectedOnMutateResultType>(onMutateResult);
},
}),
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 @@ -1082,7 +1122,7 @@ describe("client", () => {
expect(firstRequestUrl?.searchParams.get("cursor")).toBe("0");

// Set up mock for second page before triggering next page fetch
const secondRequestHandler = useMockRequestHandler({
const _secondRequestHandler = useMockRequestHandler({
baseUrl,
method: "get",
path: "/paginated-data",
Expand Down Expand Up @@ -1209,7 +1249,7 @@ describe("client", () => {
const client = createClient(fetchClient);

// First page request handler
const firstRequestHandler = useMockRequestHandler({
const _firstRequestHandler = useMockRequestHandler({
baseUrl,
method: "get",
path: "/paginated-data",
Expand Down Expand Up @@ -1245,7 +1285,7 @@ describe("client", () => {
expect(result.current.data).toEqual([1, 2, 3]);

// Set up mock for second page before triggering next page fetch
const secondRequestHandler = useMockRequestHandler({
const _secondRequestHandler = useMockRequestHandler({
baseUrl,
method: "get",
path: "/paginated-data",
Expand All @@ -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