1 | |
2 |
|
3 | export type DynSelect_base_id = string |
4 | export type DynSelect_table_id = string |
5 | export type DynSelect_field_id = string |
6 |
|
7 | |
8 | export async function base_id(auth: RT.Airtable) { |
9 | const options: { value: string; label: string }[] = [] |
10 | let offset: string | undefined = undefined |
11 | do { |
12 | const url = new URL("https://api.airtable.com/v0/meta/bases") |
13 | if (offset) { |
14 | url.searchParams.append("offset", offset) |
15 | } |
16 | const response = await fetch(url, { |
17 | headers: { |
18 | Authorization: `Bearer ${auth.apiKey}`, |
19 | Accept: "application/json", |
20 | }, |
21 | }) |
22 | if (!response.ok) { |
23 | throw new Error(`${response.status} ${await response.text()}`) |
24 | } |
25 | const page = (await response.json()) as { |
26 | bases: { id: string; name: string }[] |
27 | offset?: string |
28 | } |
29 | options.push(...page.bases.map((b) => ({ value: b.id, label: b.name }))) |
30 | offset = page.offset |
31 | } while (offset) |
32 | return options |
33 | } |
34 |
|
35 | async function listTables(auth: RT.Airtable, base_id: string) { |
36 | if (!base_id) { |
37 | return [] |
38 | } |
39 | const response = await fetch( |
40 | `https://api.airtable.com/v0/meta/bases/${base_id}/tables`, |
41 | { |
42 | headers: { |
43 | Authorization: `Bearer ${auth.apiKey}`, |
44 | Accept: "application/json", |
45 | }, |
46 | } |
47 | ) |
48 | if (!response.ok) { |
49 | throw new Error(`${response.status} ${await response.text()}`) |
50 | } |
51 | const { tables } = (await response.json()) as { |
52 | tables: { |
53 | id: string |
54 | name: string |
55 | fields: { id: string; name: string; type: string }[] |
56 | views: { id: string; name: string }[] |
57 | }[] |
58 | } |
59 | return tables |
60 | } |
61 |
|
62 | |
63 | export async function table_id(auth: RT.Airtable, base_id: DynSelect_base_id) { |
64 | return (await listTables(auth, base_id)).map((t) => ({ |
65 | value: t.id, |
66 | label: t.name, |
67 | })) |
68 | } |
69 |
|
70 | |
71 | export async function field_id( |
72 | auth: RT.Airtable, |
73 | base_id: DynSelect_base_id, |
74 | table_id: DynSelect_table_id |
75 | ) { |
76 | const table = (await listTables(auth, base_id)).find((t) => t.id === table_id) |
77 | return (table?.fields ?? []).map((f) => ({ |
78 | value: f.id, |
79 | label: `${f.name} (${f.type})`, |
80 | })) |
81 | } |
82 |
|
83 | |
84 | * Update Field |
85 | * Rename a field or change its description. Only the values you provide are changed; a field's type and options cannot be changed through the API. Requires the schema.bases:write scope. |
86 | */ |
87 | export async function main( |
88 | auth: RT.Airtable, |
89 | base_id: DynSelect_base_id, |
90 | table_id: DynSelect_table_id, |
91 | field_id: DynSelect_field_id, |
92 | name: string | undefined, |
93 | description: string | undefined |
94 | ) { |
95 | const body: { [key: string]: any } = {} |
96 | if (name != null && name !== "") { |
97 | body.name = name |
98 | } |
99 | if (description != null && description !== "") { |
100 | body.description = description |
101 | } |
102 |
|
103 | const response = await fetch( |
104 | `https://api.airtable.com/v0/meta/bases/${base_id}/tables/${table_id}/fields/${field_id}`, |
105 | { |
106 | method: "PATCH", |
107 | headers: { |
108 | Authorization: `Bearer ${auth.apiKey}`, |
109 | "Content-Type": "application/json", |
110 | Accept: "application/json", |
111 | }, |
112 | body: JSON.stringify(body), |
113 | } |
114 | ) |
115 | if (!response.ok) { |
116 | throw new Error(`${response.status} ${await response.text()}`) |
117 | } |
118 |
|
119 | return await response.json() |
120 | } |
121 |
|