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/early-cursors-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-react-query": patch
---

Fix `useInfiniteQuery` to omit the page parameter when `initialPageParam` is `undefined`.
4 changes: 2 additions & 2 deletions packages/openapi-react-query/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ export default function createClient<Paths extends {}, Media extends MediaType =
return useInfiniteQuery(
{
queryKey,
queryFn: async ({ queryKey: [method, path, init], pageParam = 0, signal }) => {
queryFn: async ({ queryKey: [method, path, init], pageParam, signal }) => {
const mth = method.toUpperCase() as Uppercase<typeof method>;
const fn = client[mth] as ClientMethod<Paths, typeof method, Media>;
const mergedInit = {
Expand All @@ -242,7 +242,7 @@ export default function createClient<Paths extends {}, Media extends MediaType =
...(init?.params || {}),
query: {
...(init?.params as { query?: DefaultParamsOption })?.query,
[pageParamName]: pageParam,
...(pageParam !== undefined ? { [pageParamName]: pageParam } : {}),
},
},
};
Expand Down
60 changes: 60 additions & 0 deletions packages/openapi-react-query/test/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,66 @@ describe("client", () => {
const allItems = result.current.data?.pages.flatMap((page) => page.items);
expect(allItems).toEqual([1, 2, 3, 4, 5, 6]);
});
it("should omit cursor from the initial request when initialPageParam is undefined", async () => {
const fetchClient = createFetchClient<paths>({ baseUrl });
const client = createClient(fetchClient);

const firstRequestHandler = useMockRequestHandler({
baseUrl,
method: "get",
path: "/paginated-data",
status: 200,
body: { items: [1, 2, 3], nextPage: 1 },
});

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

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

const firstRequestUrl = firstRequestHandler.getRequestUrl();
expect(firstRequestUrl?.searchParams.get("limit")).toBe("3");
expect(firstRequestUrl?.searchParams.get("cursor")).toBeNull();

const secondRequestHandler = useMockRequestHandler({
baseUrl,
method: "get",
path: "/paginated-data",
status: 200,
body: { items: [4, 5, 6], nextPage: 2 },
});

await act(async () => {
await result.current.fetchNextPage();
rerender();
});

await waitFor(() => {
expect(result.current.isFetching).toBe(false);
expect(result.current.data?.pages).toHaveLength(2);
});

const secondRequestUrl = secondRequestHandler.getRequestUrl();
expect(secondRequestUrl?.searchParams.get("limit")).toBe("3");
expect(secondRequestUrl?.searchParams.get("cursor")).toBe("1");
});
it("should reverse pages and pageParams when using the select option", async () => {
const fetchClient = createFetchClient<paths>({ baseUrl });
const client = createClient(fetchClient);
Expand Down
Loading