* Lists all items in a Webflow collection using `offset`/`limit` pagination * (limit capped at 100), advancing the numeric `offset` until the accumulated * count reaches `pagination.total` so the full set is returned. Bounded by * `WEBFLOW_MAX_ITEMS_PAGES`; logs a warning rather than silently dro
(accessToken: string, collectionId: string)
| 41 | * when the cap is hit. |
| 42 | */ |
| 43 | async function fetchAllItems(accessToken: string, collectionId: string): Promise<WebflowItem[]> { |
| 44 | const items: WebflowItem[] = [] |
| 45 | let offset = 0 |
| 46 | |
| 47 | for (let page = 0; page < WEBFLOW_MAX_ITEMS_PAGES; page++) { |
| 48 | const url = new URL(`https://api.webflow.com/v2/collections/${collectionId}/items`) |
| 49 | url.searchParams.set('limit', String(WEBFLOW_PAGE_LIMIT)) |
| 50 | url.searchParams.set('offset', String(offset)) |
| 51 | |
| 52 | const response = await fetch(url.toString(), { |
| 53 | headers: { |
| 54 | Authorization: `Bearer ${accessToken}`, |
| 55 | accept: 'application/json', |
| 56 | }, |
| 57 | }) |
| 58 | |
| 59 | if (!response.ok) { |
| 60 | const errorData = await response.json().catch(() => ({})) |
| 61 | throw new WebflowFetchError(response.status, errorData) |
| 62 | } |
| 63 | |
| 64 | const data = (await response.json()) as WebflowItemsPage |
| 65 | const pageItems = data.items || [] |
| 66 | items.push(...pageItems) |
| 67 | |
| 68 | const total = data.pagination?.total |
| 69 | offset += pageItems.length |
| 70 | if (pageItems.length === 0 || (typeof total === 'number' && items.length >= total)) { |
| 71 | return items |
| 72 | } |
| 73 | |
| 74 | if (page === WEBFLOW_MAX_ITEMS_PAGES - 1) { |
| 75 | logger.warn('Webflow items listing hit pagination cap; item list may be incomplete', { |
| 76 | collectionId, |
| 77 | pages: WEBFLOW_MAX_ITEMS_PAGES, |
| 78 | }) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | return items |
| 83 | } |
| 84 | |
| 85 | class WebflowFetchError extends Error { |
| 86 | constructor( |