Edits history of script submission #23491 for ' List Tables (Get Base Schema) (airtable)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    export type DynSelect_base_id = 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
    }
    
    /**
     * List Tables (Get Base Schema)
     * Get the schema of a base: every table with its fields (IDs, names, types, options) and views. Requires the schema.bases:read scope.
     */
    export async function main(auth: RT.Airtable, base_id: DynSelect_base_id) {
      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()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 3 hours ago