* Lists all Airtable bases, following the `offset` continuation token the Meta * API returns (an opaque string, passed back verbatim as `?offset=`) so the * full set is returned. Bounded by `AIRTABLE_MAX_BASES_PAGES`; logs a warning * rather than silently dropping bases when the cap is hit.
(accessToken: string)
| 25 | * rather than silently dropping bases when the cap is hit. |
| 26 | */ |
| 27 | async function fetchAllBases(accessToken: string): Promise<AirtableBase[]> { |
| 28 | const bases: AirtableBase[] = [] |
| 29 | let offset: string | undefined |
| 30 | |
| 31 | for (let page = 0; page < AIRTABLE_MAX_BASES_PAGES; page++) { |
| 32 | const url = new URL('https://api.airtable.com/v0/meta/bases') |
| 33 | if (offset) { |
| 34 | url.searchParams.set('offset', offset) |
| 35 | } |
| 36 | |
| 37 | const response = await fetch(url.toString(), { |
| 38 | headers: { |
| 39 | Authorization: `Bearer ${accessToken}`, |
| 40 | 'Content-Type': 'application/json', |
| 41 | }, |
| 42 | }) |
| 43 | |
| 44 | if (!response.ok) { |
| 45 | const errorData = await response.json().catch(() => ({})) |
| 46 | throw new AirtableFetchError(response.status, errorData) |
| 47 | } |
| 48 | |
| 49 | const data = (await response.json()) as { bases?: AirtableBase[]; offset?: string } |
| 50 | if (Array.isArray(data.bases)) { |
| 51 | bases.push(...data.bases) |
| 52 | } |
| 53 | |
| 54 | offset = data.offset || undefined |
| 55 | if (!offset) { |
| 56 | return bases |
| 57 | } |
| 58 | |
| 59 | if (page === AIRTABLE_MAX_BASES_PAGES - 1) { |
| 60 | logger.warn('Airtable bases listing hit pagination cap; base list may be incomplete', { |
| 61 | pages: AIRTABLE_MAX_BASES_PAGES, |
| 62 | }) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | return bases |
| 67 | } |
| 68 | |
| 69 | class AirtableFetchError extends Error { |
| 70 | constructor( |