Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix invalid data check in query/mutation fn #1975

Merged
merged 10 commits into from
Nov 13, 2024
Merged
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
6 changes: 6 additions & 0 deletions .changeset/spotty-flies-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"openapi-react-query": patch
---

- 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
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"[markdown]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[typescript]": {
"[typescript][typescriptreact]": {
HugeLetters marked this conversation as resolved.
Show resolved Hide resolved
"editor.defaultFormatter": "biomejs.biome"
}
}
2 changes: 1 addition & 1 deletion docs/scripts/update-contributors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"]),
};
Expand Down
8 changes: 5 additions & 3 deletions packages/openapi-react-query/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,10 @@ 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 } = await fn(path, { signal, ...(init as any) }); // TODO: find a way to avoid as any
if (error || !data) {
HugeLetters marked this conversation as resolved.
Show resolved Hide resolved
if (error) {
throw error;
}

return data;
};

Expand All @@ -141,10 +142,11 @@ 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 } = await fn(path, init as InitWithUnknowns<typeof init>);
if (error || !data) {
if (error) {
throw error;
}
return data;

return data as Exclude<typeof data, undefined>;
Copy link
Contributor Author

@HugeLetters HugeLetters Nov 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't matter since the type is dictated by return type of createClient anyway, its not inferred from here - just checked that what we return is valid

},
...options,
},
Expand Down
92 changes: 92 additions & 0 deletions packages/openapi-react-query/test/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<paths>({ 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<paths>({ 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<paths>({ baseUrl, fetch: fetchInfinite });
const client = createClient(fetchClient);
Expand Down Expand Up @@ -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<paths>({ 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<paths>({ 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<paths>({ baseUrl });
const client = createClient(fetchClient);
Expand Down