Skip to content

Commit

Permalink
People page hitting API record length limit #187
Browse files Browse the repository at this point in the history
  • Loading branch information
joepio committed Jul 21, 2024
1 parent 4b5c954 commit 9ee09e5
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions src/routes/api/people/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,37 @@ const filter = (p: Person) => {
return p.image && !p.privacy && p.checked && p.org?.includes(currentOrg)
}

async function fetchAllPages(fetch: any, url: any) {
let allRecords: any[] = []
// https://airtable.com/developers/web/api/list-records#query-pagesize
let offset

do {
const fullUrl = offset ? `${url}?offset=${offset}` : url
const response = await fetch(fullUrl, options)
if (!response.ok) {
throw new Error('Failed to fetch data from Airtable')
}
const data: any = await response.json()
allRecords = allRecords.concat(data.records)
offset = data.offset
} while (offset)

return allRecords
}

export async function GET({ fetch }) {
const url = `https://api.airtable.com/v0/appWPTGqZmUcs3NWu/tblZhQc49PkCz3yHd`

const response = await fetch(url, options)
if (!response.ok) {
throw new Error('Failed to fetch data from Airtable')
}
const data = await response.json()
try {
const out: Person[] = data.records
const records = await fetchAllPages(fetch, url)
console.log('data len', records.length)
const out: Person[] = records
.map(recordToPerson)
.filter(filter)
// Shuffle the array, although not truly random
.sort(() => 0.5 - Math.random())
console.log('out', out.length)
return json(out)
} catch (e) {
console.log('err while transforming airtable data', e)
Expand Down

0 comments on commit 9ee09e5

Please sign in to comment.