Skip to content

Pagination issue with cursor reference #2639

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

Open
1 task done
dambrogia opened this issue May 12, 2025 · 0 comments
Open
1 task done

Pagination issue with cursor reference #2639

dambrogia opened this issue May 12, 2025 · 0 comments

Comments

@dambrogia
Copy link

dambrogia commented May 12, 2025

Confirm this is a Typescript library issue and not an underlying Cloudflare API issue

  • This is an issue with the Typescript library

Describe the bug

I am attempting to paginate all pages of my list items in cloudflare:

import Cloudflare from 'cloudflare';

// ...

function createClient(): Cloudflare {
    return new Cloudflare({
        apiToken: getCloudflareApiToken(),
    });
}

// ...

export async function* iterateListItems() {
    const client = createClient();
    const listId = getCloudflareIpListId();
    const accountId = getCloudflareAccountId();

    let response = await client.rules.lists.items.list(listId, {
        account_id: accountId,
        per_page: 10,
    });

    yield response

    while (response.hasNextPage()) {
        yield response = await response.getNextPage();
    }
}

When running response.hasNextPage(), under the hood the CursorPagination class is referencing this.result_info?.cursor when the data is set within the CursorPagination class under this.result_info.cursors. This can be seen below:

Image

To Reproduce

  1. Install cloudflare at version 4.2.0.
  2. Request to list all list items with client.rules.lists.items.list where client is an instance of Cloudflare.
  3. Set page size to less than total list size.
  4. Attempt to call response.hasNextPage() and response.getNextPage() with expected functionality to paginate through results.
  5. Unexpectedly skip pages of results.

Code snippets

After further testing, the public iterPages method also produces the same result, because of the same issue:

    // this will only result in one iteration, when it should result in more
    for await (const page of response.iterPages()) {
        const items = page.getPaginatedItems();
    }

I have also tested the following change is in the code above is a successful work around. The only odd (likely expected) issue I have run into with this is that the per_page needs to be the same when paginating or else you'll run into an error. And per_page also seems to be required to be <= 500.

export async function* iterateListItems() {
    const client = createClient();
    const listId = getCloudflareIpListId();
    const accountId = getCloudflareAccountId();

    let response = await client.rules.lists.items.list(listId, {
        account_id: accountId,
        per_page: 10, 
        cursor: undefined,
    });

    yield response;

    while ('after' in (response.result_info as any).cursors) {
        yield response = await client.rules.lists.items.list(listId, {
            account_id: accountId,
            per_page: 10,
            cursor: (response.result_info as any).cursors.after,
        });
    }
}

OS

macOS

Runtime version

Typescript 5.6.2

Library version

v4.2.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant