Created
December 21, 2022 18:55
-
-
Save mauritsl/7d47ca55562f38813827ef5f0baacd70 to your computer and use it in GitHub Desktop.
Airtable webhook payloads
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 async function script1({baseId, webhookId, secret}: { | |
baseId: string; | |
webhookId: string; | |
secret: string; | |
}): Promise<{ | |
payloads: { | |
timestamp?: string; | |
baseTransactionNumber?: number; | |
actionMetadata?: { | |
source: string; | |
sourceMetadata: { | |
user: { | |
id: string; | |
email: string; | |
permissionLevel: string; | |
name: string; | |
profilePicUrl: string; | |
}; | |
}; | |
}; | |
payloadFormat?: string; | |
changedTablesById?: { | |
[tableId: string]: { | |
createdRecordsById?: { | |
[recordId: string]: { | |
createdTime: string; | |
cellValuesByFieldId: { | |
[fieldId: string]: any; | |
}; | |
}; | |
}; | |
changedRecordsById?: { | |
[recordId: string]: { | |
current: { | |
cellValuesByFieldId: { | |
[fieldId: string]: any; | |
}; | |
}; | |
}; | |
}; | |
destroyedRecordIds?: string[]; | |
}; | |
}; | |
}[] | |
}> { | |
type Payload = { | |
timestamp?: string; | |
baseTransactionNumber?: number; | |
actionMetadata?: { | |
source: string; | |
sourceMetadata: { | |
user: { | |
id: string; | |
email: string; | |
permissionLevel: string; | |
name: string; | |
profilePicUrl: string; | |
}; | |
}; | |
}; | |
payloadFormat?: string; | |
changedTablesById?: { | |
[tableId: string]: { | |
createdRecordsById?: { | |
[recordId: string]: { | |
createdTime: string; | |
cellValuesByFieldId: { | |
[fieldId: string]: any; | |
}; | |
}; | |
}; | |
changedRecordsById?: { | |
[recordId: string]: { | |
current: { | |
cellValuesByFieldId: { | |
[fieldId: string]: any; | |
}; | |
}; | |
}; | |
}; | |
destroyedRecordIds?: string[]; | |
}; | |
}; | |
}; | |
const accessToken = await secrets.getAccessToken(secret); | |
const fetchPayloads = async (cursor = 1) => { | |
const url = `https://api.airtable.com/v0/bases/${ | |
baseId | |
}/webhooks/${ | |
webhookId | |
}/payloads?limit=50&cursor=${cursor}`; | |
const response = await fetch(url, { | |
headers: { | |
Authorization: `Bearer ${accessToken}` | |
} | |
}); | |
return await response.json() as { | |
cursor: number; | |
mightHaveMore: boolean; | |
payloads: Payload[] | |
}; | |
}; | |
const payloads: Payload[] = []; | |
let hasMore = true; | |
let cursor = 1; | |
while (hasMore) { | |
const body = await fetchPayloads(cursor); | |
hasMore = body.mightHaveMore; | |
cursor = body.cursor; | |
await Promise.all(body.payloads.map(async payload => { | |
const {baseTransactionNumber} = payload; | |
console.log(baseTransactionNumber); | |
const item = await db.listAirtableWebhook({baseTransactionNumber}); | |
if (!item.count) { | |
await db.createAirtableWebhook({ | |
baseTransactionNumber, | |
payload | |
}); | |
payloads.push(payload); | |
} | |
})); | |
} | |
return { | |
payloads | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment