1 | |
2 |
|
3 | type Airtable = { |
4 | apiKey: string; |
5 | }; |
6 |
|
7 | type AirtableTable = { |
8 | baseId: string; |
9 | tableName: string; |
10 | }; |
11 | export async function main( |
12 | atCon: Airtable, |
13 | atTable: AirtableTable, |
14 | recordId?: string, |
15 | ) { |
16 | const baseUrl = `https://api.airtable.com/v0/${atTable.baseId}/${encodeURIComponent( |
17 | atTable.tableName, |
18 | )}`; |
19 |
|
20 | if (recordId) { |
21 | const response = await fetch(`${baseUrl}/${recordId}`, { |
22 | method: "GET", |
23 | headers: { |
24 | Authorization: `Bearer ${atCon.apiKey}`, |
25 | }, |
26 | }); |
27 | if (!response.ok) { |
28 | throw new Error(`${response.status} ${await response.text()}`); |
29 | } |
30 | const record = await response.json(); |
31 | return { result: record }; |
32 | } else { |
33 | const response = await fetch(baseUrl, { |
34 | method: "GET", |
35 | headers: { |
36 | Authorization: `Bearer ${atCon.apiKey}`, |
37 | }, |
38 | }); |
39 | if (!response.ok) { |
40 | throw new Error(`${response.status} ${await response.text()}`); |
41 | } |
42 | const records = await response.json(); |
43 | return { result: records }; |
44 | } |
45 | } |
46 |
|