//native
export type DynSelect_base_id = string
export type DynSelect_table_id = string
export type DynSelect_view = string
export type DynSelect_sort_field = string
// Dropdown of the bases the token can access.
export async function base_id(auth: RT.Airtable) {
const options: { value: string; label: string }[] = []
let offset: string | undefined = undefined
do {
const url = new URL("https://api.airtable.com/v0/meta/bases")
if (offset) {
url.searchParams.append("offset", offset)
}
const response = await fetch(url, {
headers: {
Authorization: `Bearer ${auth.apiKey}`,
Accept: "application/json",
},
})
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
const page = (await response.json()) as {
bases: { id: string; name: string }[]
offset?: string
}
options.push(...page.bases.map((b) => ({ value: b.id, label: b.name })))
offset = page.offset
} while (offset)
return options
}
async function listTables(auth: RT.Airtable, base_id: string) {
if (!base_id) {
return []
}
const response = await fetch(
`https://api.airtable.com/v0/meta/bases/${base_id}/tables`,
{
headers: {
Authorization: `Bearer ${auth.apiKey}`,
Accept: "application/json",
},
}
)
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
const { tables } = (await response.json()) as {
tables: {
id: string
name: string
fields: { id: string; name: string; type: string }[]
views: { id: string; name: string }[]
}[]
}
return tables
}
// Dropdown of the tables in the chosen base.
export async function table_id(auth: RT.Airtable, base_id: DynSelect_base_id) {
return (await listTables(auth, base_id)).map((t) => ({
value: t.id,
label: t.name,
}))
}
// Dropdown of the views in the chosen table.
export async function view(
auth: RT.Airtable,
base_id: DynSelect_base_id,
table_id: DynSelect_table_id
) {
const table = (await listTables(auth, base_id)).find((t) => t.id === table_id)
return (table?.views ?? []).map((v) => ({ value: v.id, label: v.name }))
}
// Dropdown of the fields in the chosen table.
export async function sort_field(
auth: RT.Airtable,
base_id: DynSelect_base_id,
table_id: DynSelect_table_id
) {
const table = (await listTables(auth, base_id)).find((t) => t.id === table_id)
return (table?.fields ?? []).map((f) => ({
value: f.id,
label: `${f.name} (${f.type})`,
}))
}
/**
* List Records
* List the records of a table or of one of its views, with optional formula filter and sort. Follows Airtable's offset pagination to return every record (100 per page) up to max_records.
*/
export async function main(
auth: RT.Airtable,
base_id: DynSelect_base_id,
table_id: DynSelect_table_id,
view: DynSelect_view | undefined,
filter_by_formula: string | undefined,
sort_field: DynSelect_sort_field | undefined,
sort_direction: "asc" | "desc" | undefined,
max_records: number | undefined,
return_fields_by_field_id: boolean | undefined
) {
const records: any[] = []
let offset: string | undefined = undefined
do {
const url = new URL(`https://api.airtable.com/v0/${base_id}/${table_id}`)
if (view != null && view !== "") {
url.searchParams.append("view", view)
}
if (filter_by_formula != null && filter_by_formula !== "") {
url.searchParams.append("filterByFormula", filter_by_formula)
}
if (sort_field != null && sort_field !== "") {
url.searchParams.append("sort[0][field]", sort_field)
url.searchParams.append("sort[0][direction]", sort_direction || "asc")
}
if (max_records != null) {
url.searchParams.append("maxRecords", String(max_records))
}
if (return_fields_by_field_id === true) {
url.searchParams.append("returnFieldsByFieldId", "true")
}
if (offset) {
url.searchParams.append("offset", offset)
}
const response = await fetch(url, {
headers: {
Authorization: `Bearer ${auth.apiKey}`,
Accept: "application/json",
},
})
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
const page = await response.json()
records.push(...(page.records ?? []))
offset = page.offset
} while (offset)
return { records }
}
Submitted by hugo989 5 days ago
//native
type Airtable = {
apiKey: string;
};
type AirtableTable = {
baseId: string;
tableName: string;
};
/**
* List Records
* List records in a table, following Airtable's `offset` pagination to return
* every record (the API returns at most 100 per page). Optionally cap the
* number of records returned with `maxRecords`.
*/
export async function main(
atCon: Airtable,
atTable: AirtableTable,
maxRecords?: number,
) {
const records: any[] = [];
let offset: string | undefined = undefined;
do {
const url = new URL(
`https://api.airtable.com/v0/${atTable.baseId}/${encodeURIComponent(
atTable.tableName,
)}`,
);
if (offset !== undefined) {
url.searchParams.append("offset", offset);
}
const response = await fetch(url, {
method: "GET",
headers: {
Authorization: `Bearer ${atCon.apiKey}`,
},
});
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`);
}
const page = await response.json();
records.push(...(page.records ?? []));
offset = page.offset;
} while (
offset !== undefined &&
(maxRecords === undefined || records.length < maxRecords)
);
return {
records:
maxRecords !== undefined ? records.slice(0, maxRecords) : records,
};
}
Submitted by hugo989 137 days ago