diff --git a/.changeset/early-cursors-sip.md b/.changeset/early-cursors-sip.md new file mode 100644 index 000000000..1a7f3754b --- /dev/null +++ b/.changeset/early-cursors-sip.md @@ -0,0 +1,5 @@ +--- +"openapi-react-query": patch +--- + +Fix `useInfiniteQuery` to omit the page parameter when `initialPageParam` is `undefined`. diff --git a/packages/openapi-react-query/src/index.ts b/packages/openapi-react-query/src/index.ts index fb9683164..a9e3febf3 100644 --- a/packages/openapi-react-query/src/index.ts +++ b/packages/openapi-react-query/src/index.ts @@ -232,7 +232,7 @@ export default function createClient { + queryFn: async ({ queryKey: [method, path, init], pageParam, signal }) => { const mth = method.toUpperCase() as Uppercase; const fn = client[mth] as ClientMethod; const mergedInit = { @@ -242,7 +242,7 @@ export default function createClient { 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({ 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({ baseUrl }); const client = createClient(fetchClient);