Created
September 19, 2023 22:09
-
-
Save thisanimus/03143e7e1d52bca619fce8be299a77ba to your computer and use it in GitHub Desktop.
Recursive Fetch via Airtable API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default class Airtable { | |
constructor(token, base) { | |
this.token = token; | |
this.base = base; | |
this.baseUrl = 'https://api.airtable.com/v0'; | |
this.tables = []; | |
this.tmp = []; | |
} | |
async get(table) { | |
const url = `${this.baseUrl}/${this.base}/${table}`; | |
const data = await this.fetchRequest(url); | |
return data; | |
} | |
async fetchRequest(url) { | |
try { | |
// Fetch request and parse as JSON | |
const response = await fetch(url, { | |
method: 'GET', | |
headers: { | |
'Content-Type': 'application/json', | |
Authorization: `Bearer ${this.token}`, | |
}, | |
}); | |
const json = await response.json(); | |
let records = json.records; | |
// If another page exists, merge its output into the array recursively | |
if (json.offset) { | |
let href = new URL(url); | |
href.searchParams.set('offset', json.offset); | |
// delay this to avoid airtable's api limits | |
//await this.timeout(1000); | |
records = records.concat(await this.fetchRequest(href.toString())); | |
} | |
return records; | |
} catch (err) { | |
return console.error(err); | |
} | |
} | |
timeout(ms) { | |
return new Promise((resolve) => setTimeout(resolve, ms)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment