Skip to content

Commit 227f3db

Browse files
razor-xseambotphpnode
authored
feat: Add seam.createPaginator (#283)
* Add SeamPaginator * ci: Generate code * Update interface * Add nextPageCursor * Fix SeamPaginator types * Immutable SeamPaginator (#288) * Immutable SeamPaginator * Fix unused type * Fix type error * Update fake-seam-connect * Run npm update * Mark failing test * Rename to fetchResponse * Improve types * Rename page to request * Add more SeamPaginator tests * Handle pagination via get or post * ci: Format code * ci: Generate code * Export and document SeamPaginator * ci: Format code * Brand PageCursor * Fix request * Rename toArray to flattenToArray * ci: Format code * Use method version for #fetch * ci: Format code --------- Co-authored-by: Seam Bot <[email protected]> Co-authored-by: Charles Pick <[email protected]>
1 parent 51452a6 commit 227f3db

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+2020
-884
lines changed

README.md

+61
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,67 @@ try {
308308

309309
[action attempt]: https://docs.seam.co/latest/core-concepts/action-attempts
310310

311+
### Pagination
312+
313+
Some Seam API endpoints that return lists of resources support pagination.
314+
Use the `SeamPaginator` class to fetch and process resources across multiple pages.
315+
316+
#### Manually fetch pages with the nextPageCursor
317+
318+
```ts
319+
const pages = seam.createPaginator(
320+
seam.devices.list({
321+
limit: 20,
322+
}),
323+
)
324+
325+
const [devices, { hasNextPage, nextPageCursor }] = await pages.firstPage()
326+
327+
if (hasNextPage) {
328+
const [moreDevices] = await pages.nextPage(nextPageCursor)
329+
}
330+
```
331+
332+
#### Iterate over all pages
333+
334+
```ts
335+
const pages = seam.createPaginator(
336+
seam.devices.list({
337+
limit: 20,
338+
}),
339+
)
340+
341+
for await (const devices of pages) {
342+
console.log(`There are ${devices.length} devices on this page.`)
343+
}
344+
```
345+
346+
#### Iterate over all resources
347+
348+
```ts
349+
const pages = seam.createPaginator(
350+
seam.devices.list({
351+
limit: 20,
352+
}),
353+
)
354+
355+
for await (const device of pages.flatten()) {
356+
console.log(devices.name)
357+
}
358+
```
359+
360+
#### Return all resources across all pages as an array
361+
362+
```ts
363+
const pages = seam.createPaginator(
364+
seam.devices.list({
365+
limit: 20,
366+
}),
367+
)
368+
369+
const devices = await pages.toArray()
370+
```
371+
311372
### Interacting with Multiple Workspaces
312373

313374
Some Seam API endpoints interact with multiple workspaces.

generate-routes.ts

+1
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ import {
299299
resolveActionAttempt,
300300
} from 'lib/seam/connect/resolve-action-attempt.js'
301301
import { SeamHttpRequest } from 'lib/seam/connect/seam-http-request.js'
302+
import { SeamPaginator } from 'lib/seam/connect/seam-paginator.js'
302303
303304
${
304305
namespace === 'client_sessions'

0 commit comments

Comments
 (0)