-
Notifications
You must be signed in to change notification settings - Fork 72
Open
Description
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:
To Reproduce
- Install cloudflare at version
4.2.0
. - Request to list all list items with
client.rules.lists.items.list
whereclient
is an instance ofCloudflare
. - Set page size to less than total list size.
- Attempt to call
response.hasNextPage()
andresponse.getNextPage()
with expected functionality to paginate through results. - 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
Metadata
Metadata
Assignees
Labels
No labels