1 | |
2 |
|
3 | export type DynSelect_base_id = string |
4 | export type DynSelect_table_id = string |
5 |
|
6 | |
7 | export async function base_id(auth: RT.Airtable) { |
8 | const options: { value: string; label: string }[] = [] |
9 | let offset: string | undefined = undefined |
10 | do { |
11 | const url = new URL("https://api.airtable.com/v0/meta/bases") |
12 | if (offset) { |
13 | url.searchParams.append("offset", offset) |
14 | } |
15 | const response = await fetch(url, { |
16 | headers: { |
17 | Authorization: `Bearer ${auth.apiKey}`, |
18 | Accept: "application/json", |
19 | }, |
20 | }) |
21 | if (!response.ok) { |
22 | throw new Error(`${response.status} ${await response.text()}`) |
23 | } |
24 | const page = (await response.json()) as { |
25 | bases: { id: string; name: string }[] |
26 | offset?: string |
27 | } |
28 | options.push(...page.bases.map((b) => ({ value: b.id, label: b.name }))) |
29 | offset = page.offset |
30 | } while (offset) |
31 | return options |
32 | } |
33 |
|
34 | async function listTables(auth: RT.Airtable, base_id: string) { |
35 | if (!base_id) { |
36 | return [] |
37 | } |
38 | const response = await fetch( |
39 | `https://api.airtable.com/v0/meta/bases/${base_id}/tables`, |
40 | { |
41 | headers: { |
42 | Authorization: `Bearer ${auth.apiKey}`, |
43 | Accept: "application/json", |
44 | }, |
45 | } |
46 | ) |
47 | if (!response.ok) { |
48 | throw new Error(`${response.status} ${await response.text()}`) |
49 | } |
50 | const { tables } = (await response.json()) as { |
51 | tables: { |
52 | id: string |
53 | name: string |
54 | fields: { id: string; name: string; type: string }[] |
55 | views: { id: string; name: string }[] |
56 | }[] |
57 | } |
58 | return tables |
59 | } |
60 |
|
61 | |
62 | export async function table_id(auth: RT.Airtable, base_id: DynSelect_base_id) { |
63 | return (await listTables(auth, base_id)).map((t) => ({ |
64 | value: t.id, |
65 | label: t.name, |
66 | })) |
67 | } |
68 |
|
69 | |
70 | * Create or Update Record |
71 | * Update the record with the given ID (partial update), or create a new record when record_id is empty. To match on field values instead of a record ID, use Upsert Records. |
72 | */ |
73 | export async function main( |
74 | auth: RT.Airtable, |
75 | base_id: DynSelect_base_id, |
76 | table_id: DynSelect_table_id, |
77 | record_id: string | undefined, |
78 | fields: { [key: string]: any }, |
79 | typecast: boolean | undefined, |
80 | return_fields_by_field_id: boolean | undefined |
81 | ) { |
82 | const update = record_id != null && record_id !== "" |
83 | const response = await fetch( |
84 | update |
85 | ? `https://api.airtable.com/v0/${base_id}/${table_id}/${record_id}` |
86 | : `https://api.airtable.com/v0/${base_id}/${table_id}`, |
87 | { |
88 | method: update ? "PATCH" : "POST", |
89 | headers: { |
90 | Authorization: `Bearer ${auth.apiKey}`, |
91 | "Content-Type": "application/json", |
92 | Accept: "application/json", |
93 | }, |
94 | body: JSON.stringify({ |
95 | fields, |
96 | typecast: typecast === true, |
97 | returnFieldsByFieldId: return_fields_by_field_id === true, |
98 | }), |
99 | } |
100 | ) |
101 | if (!response.ok) { |
102 | throw new Error(`${response.status} ${await response.text()}`) |
103 | } |
104 |
|
105 | return await response.json() |
106 | } |
107 |
|