From 060476277823974d381bac25486135d620a0339d Mon Sep 17 00:00:00 2001 From: Evgenii Perminov Date: Mon, 28 Oct 2024 18:09:49 +0100 Subject: [PATCH 01/10] check specifically for undefined in data for throwing errors --- packages/openapi-react-query/src/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/openapi-react-query/src/index.ts b/packages/openapi-react-query/src/index.ts index 97d83a68..03a39b7b 100644 --- a/packages/openapi-react-query/src/index.ts +++ b/packages/openapi-react-query/src/index.ts @@ -115,7 +115,7 @@ export default function createClient; const fn = client[mth] as ClientMethod; const { data, error } = await fn(path, { signal, ...(init as any) }); // TODO: find a way to avoid as any - if (error || !data) { + if (error || data === undefined) { throw error; } return data; @@ -141,7 +141,7 @@ export default function createClient; const fn = client[mth] as ClientMethod; const { data, error } = await fn(path, init as InitWithUnknowns); - if (error || !data) { + if (error || data === undefined) { throw error; } return data; From 5dfdd6c1ea41a459aa53bd26e93d9d385da829e3 Mon Sep 17 00:00:00 2001 From: Evgenii Perminov Date: Tue, 29 Oct 2024 22:00:31 +0100 Subject: [PATCH 02/10] add changeset --- .changeset/spotty-flies-knock.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/spotty-flies-knock.md diff --git a/.changeset/spotty-flies-knock.md b/.changeset/spotty-flies-knock.md new file mode 100644 index 00000000..7dc64674 --- /dev/null +++ b/.changeset/spotty-flies-knock.md @@ -0,0 +1,5 @@ +--- +"openapi-react-query": patch +--- + +Fixed empty value check in queryFn and mutationFn: only throws error for undefined, other falsy values are allowed now From 56bba9ff3b39abfb96e08746f0ae84b91e2a541f Mon Sep 17 00:00:00 2001 From: Evgenii Perminov Date: Tue, 29 Oct 2024 22:29:58 +0100 Subject: [PATCH 03/10] add biome as default formatter for typescript react --- .vscode/settings.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 47555f79..6c78a8f7 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -8,7 +8,7 @@ "[markdown]": { "editor.defaultFormatter": "esbenp.prettier-vscode" }, - "[typescript]": { + "[typescript][typescriptreact]": { "editor.defaultFormatter": "biomejs.biome" } } From 9f8282b5a57743a0f7350bdc18b53c1ad5f609b0 Mon Sep 17 00:00:00 2001 From: Evgenii Perminov Date: Wed, 30 Oct 2024 20:40:44 +0100 Subject: [PATCH 04/10] improve data checks in query/mutation fns --- packages/openapi-react-query/src/index.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/openapi-react-query/src/index.ts b/packages/openapi-react-query/src/index.ts index 03a39b7b..b3ab2356 100644 --- a/packages/openapi-react-query/src/index.ts +++ b/packages/openapi-react-query/src/index.ts @@ -115,9 +115,10 @@ export default function createClient; const fn = client[mth] as ClientMethod; const { data, error } = await fn(path, { signal, ...(init as any) }); // TODO: find a way to avoid as any - if (error || data === undefined) { + if (error) { throw error; } + return data; }; @@ -141,9 +142,14 @@ export default function createClient; const fn = client[mth] as ClientMethod; const { data, error } = await fn(path, init as InitWithUnknowns); - if (error || data === undefined) { + if (error) { throw error; } + + if (data === undefined) { + throw new Error("Unexpected undefined response"); + } + return data; }, ...options, From 371de60b04a65d3e278784f4d3e3fe279e56f558 Mon Sep 17 00:00:00 2001 From: Evgenii Perminov Date: Wed, 30 Oct 2024 20:40:50 +0100 Subject: [PATCH 05/10] add tests --- .../openapi-react-query/test/index.test.tsx | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/packages/openapi-react-query/test/index.test.tsx b/packages/openapi-react-query/test/index.test.tsx index 97550d44..294175df 100644 --- a/packages/openapi-react-query/test/index.test.tsx +++ b/packages/openapi-react-query/test/index.test.tsx @@ -283,6 +283,50 @@ describe("client", () => { expect(data).toBeUndefined(); }); + it("should resolve data properly and have error as null when queryFn returns null", async () => { + const fetchClient = createFetchClient({ baseUrl }); + const client = createClient(fetchClient); + + useMockRequestHandler({ + baseUrl, + method: "get", + path: "/string-array", + status: 200, + body: null, + }); + + const { result } = renderHook(() => client.useQuery("get", "/string-array"), { wrapper }); + + await waitFor(() => expect(result.current.isFetching).toBe(false)); + + const { data, error } = result.current; + + expect(data).toBeNull(); + expect(error).toBeNull(); + }); + + it("should resolve error properly and have undefined data when queryFn returns undefined", async () => { + const fetchClient = createFetchClient({ baseUrl }); + const client = createClient(fetchClient); + + useMockRequestHandler({ + baseUrl, + method: "get", + path: "/string-array", + status: 200, + 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).toBeInstanceOf(Error); + expect(data).toBeUndefined(); + }); + it("should infer correct data and error type", async () => { const fetchClient = createFetchClient({ baseUrl, fetch: fetchInfinite }); const client = createClient(fetchClient); @@ -560,6 +604,54 @@ describe("client", () => { expect(error?.message).toBe("Something went wrong"); }); + it("should resolve data properly and have error as null when mutationFn returns null", async () => { + const fetchClient = createFetchClient({ baseUrl }); + const client = createClient(fetchClient); + + useMockRequestHandler({ + baseUrl, + method: "put", + path: "/comment", + status: 200, + body: null, + }); + + const { result } = renderHook(() => client.useMutation("put", "/comment"), { wrapper }); + + result.current.mutate({ body: { message: "Hello", replied_at: 0 } }); + + await waitFor(() => expect(result.current.isPending).toBe(false)); + + const { data, error } = result.current; + + expect(data).toBeNull(); + expect(error).toBeNull(); + }); + + it("should resolve data properly and have error as null when mutationFn returns undefined", async () => { + const fetchClient = createFetchClient({ baseUrl }); + const client = createClient(fetchClient); + + useMockRequestHandler({ + baseUrl, + method: "put", + path: "/comment", + status: 200, + body: undefined, + }); + + const { result } = renderHook(() => client.useMutation("put", "/comment"), { wrapper }); + + result.current.mutate({ body: { message: "Hello", replied_at: 0 } }); + + await waitFor(() => expect(result.current.isPending).toBe(false)); + + const { data, error } = result.current; + + expect(error).toBeNull(); + expect(data).toBeUndefined(); + }); + it("should use provided custom queryClient", async () => { const fetchClient = createFetchClient({ baseUrl }); const client = createClient(fetchClient); From d320d89f0abf1d3fae44a01b00fa6dd5893acbfd Mon Sep 17 00:00:00 2001 From: Evgenii Perminov Date: Wed, 30 Oct 2024 20:50:59 +0100 Subject: [PATCH 06/10] test fix --- 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 294175df..82930d2b 100644 --- a/packages/openapi-react-query/test/index.test.tsx +++ b/packages/openapi-react-query/test/index.test.tsx @@ -628,7 +628,7 @@ describe("client", () => { expect(error).toBeNull(); }); - it("should resolve data properly and have error as null when mutationFn returns undefined", async () => { + it("should resolve error properly and have undefined data when mutationFn returns undefined", async () => { const fetchClient = createFetchClient({ baseUrl }); const client = createClient(fetchClient); @@ -648,7 +648,7 @@ describe("client", () => { const { data, error } = result.current; - expect(error).toBeNull(); + expect(error).toBeInstanceOf(Error); expect(data).toBeUndefined(); }); From e348b6704c3dd2b70347e1e65b86e4ae1c9b07e0 Mon Sep 17 00:00:00 2001 From: Evgenii Perminov Date: Wed, 6 Nov 2024 19:59:53 +0100 Subject: [PATCH 07/10] add hugeletters to contributs list of react-query package --- docs/scripts/update-contributors.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/scripts/update-contributors.js b/docs/scripts/update-contributors.js index b627ef75..f93d43a6 100644 --- a/docs/scripts/update-contributors.js +++ b/docs/scripts/update-contributors.js @@ -182,7 +182,7 @@ const CONTRIBUTORS = { "armandabric", "illright", ]), - "openapi-react-query": new Set(["drwpow", "kerwanp", "yoshi2no"]), + "openapi-react-query": new Set(["drwpow", "kerwanp", "yoshi2no", "HugeLetters"]), "swr-openapi": new Set(["htunnicliff"]), "openapi-metadata": new Set(["kerwanp", "drwpow"]), }; From a8f2fb0cf9a2151cbf3b1b2bea4523df162dbfef Mon Sep 17 00:00:00 2001 From: Evgenii Perminov Date: Wed, 6 Nov 2024 22:57:44 +0100 Subject: [PATCH 08/10] dont throw in mutations on undefined --- packages/openapi-react-query/src/index.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/openapi-react-query/src/index.ts b/packages/openapi-react-query/src/index.ts index b3ab2356..cbee066c 100644 --- a/packages/openapi-react-query/src/index.ts +++ b/packages/openapi-react-query/src/index.ts @@ -146,11 +146,7 @@ export default function createClient; }, ...options, }, From 2e06f7a83e87cd011da8c7abe4a986bdd390fac0 Mon Sep 17 00:00:00 2001 From: Evgenii Perminov Date: Wed, 6 Nov 2024 22:59:13 +0100 Subject: [PATCH 09/10] fix changeset --- .changeset/spotty-flies-knock.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.changeset/spotty-flies-knock.md b/.changeset/spotty-flies-knock.md index 7dc64674..302c097a 100644 --- a/.changeset/spotty-flies-knock.md +++ b/.changeset/spotty-flies-knock.md @@ -2,4 +2,5 @@ "openapi-react-query": patch --- -Fixed empty value check in queryFn and mutationFn: only throws error for undefined, other falsy values are allowed now +- Fixed empty value check in queryFn: only throws error for undefined, other falsy values are allowed +- Fixed empty value check in mutationFn: allow falsy values From 204d5fdf4eb4026f1056c7318f2fcd2b9e952736 Mon Sep 17 00:00:00 2001 From: Evgenii Perminov Date: Wed, 6 Nov 2024 23:00:43 +0100 Subject: [PATCH 10/10] fix mutation tests --- 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 82930d2b..294175df 100644 --- a/packages/openapi-react-query/test/index.test.tsx +++ b/packages/openapi-react-query/test/index.test.tsx @@ -628,7 +628,7 @@ describe("client", () => { expect(error).toBeNull(); }); - it("should resolve error properly and have undefined data when mutationFn returns undefined", async () => { + it("should resolve data properly and have error as null when mutationFn returns undefined", async () => { const fetchClient = createFetchClient({ baseUrl }); const client = createClient(fetchClient); @@ -648,7 +648,7 @@ describe("client", () => { const { data, error } = result.current; - expect(error).toBeInstanceOf(Error); + expect(error).toBeNull(); expect(data).toBeUndefined(); });