* Fetches the table schema to build a field ID → field name mapping.
( accessToken: string, baseId: string, tableIdOrName: string, syncContext?: Record<string, unknown> )
| 290 | * Fetches the table schema to build a field ID → field name mapping. |
| 291 | */ |
| 292 | async function fetchFieldNames( |
| 293 | accessToken: string, |
| 294 | baseId: string, |
| 295 | tableIdOrName: string, |
| 296 | syncContext?: Record<string, unknown> |
| 297 | ): Promise<Map<string, string>> { |
| 298 | const cacheKey = `fieldNames:${baseId}/${tableIdOrName}` |
| 299 | if (syncContext?.[cacheKey]) return syncContext[cacheKey] as Map<string, string> |
| 300 | |
| 301 | const fieldNames = new Map<string, string>() |
| 302 | |
| 303 | try { |
| 304 | const url = `${AIRTABLE_API}/meta/bases/${baseId}/tables` |
| 305 | const response = await fetchWithRetry(url, { |
| 306 | method: 'GET', |
| 307 | headers: { |
| 308 | Authorization: `Bearer ${accessToken}`, |
| 309 | }, |
| 310 | }) |
| 311 | |
| 312 | if (!response.ok) { |
| 313 | logger.warn('Failed to fetch Airtable schema, using raw field keys', { |
| 314 | status: response.status, |
| 315 | }) |
| 316 | return fieldNames |
| 317 | } |
| 318 | |
| 319 | const data = (await response.json()) as { |
| 320 | tables: { id: string; name: string; fields: { id: string; name: string; type: string }[] }[] |
| 321 | } |
| 322 | |
| 323 | const table = data.tables.find((t) => t.id === tableIdOrName || t.name === tableIdOrName) |
| 324 | |
| 325 | if (table) { |
| 326 | for (const field of table.fields) { |
| 327 | fieldNames.set(field.id, field.name) |
| 328 | fieldNames.set(field.name, field.name) |
| 329 | } |
| 330 | } |
| 331 | } catch (error) { |
| 332 | logger.warn('Error fetching Airtable schema', { |
| 333 | error: toError(error).message, |
| 334 | }) |
| 335 | } |
| 336 | |
| 337 | if (syncContext) syncContext[cacheKey] = fieldNames |
| 338 | return fieldNames |
| 339 | } |
no test coverage detected