From d041899186c52a0d5c393ab354d41e7b19123889 Mon Sep 17 00:00:00 2001 From: stainless-bot Date: Thu, 3 Oct 2024 23:07:14 +0000 Subject: [PATCH] docs: add pagination example --- README.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/README.md b/README.md index e0ceefea..ce621660 100644 --- a/README.md +++ b/README.md @@ -168,6 +168,37 @@ On timeout, an `APIConnectionTimeoutError` is thrown. Note that requests which time out will be [retried twice by default](#retries). +## Auto-pagination + +List methods in the Writer API are paginated. +You can use `for await … of` syntax to iterate through items across all pages: + +```ts +async function fetchAllGraphs(params) { + const allGraphs = []; + // Automatically fetches more pages as needed. + for await (const graph of client.graphs.list()) { + allGraphs.push(graph); + } + return allGraphs; +} +``` + +Alternatively, you can make request a single page at a time: + +```ts +let page = await client.graphs.list(); +for (const graph of page.data) { + console.log(graph); +} + +// Convenience methods are provided for manually paginating: +while (page.hasNextPage()) { + page = page.getNextPage(); + // ... +} +``` + ## Advanced Usage ### Accessing raw Response data (e.g., headers)