Edits history of script submission #22334 for ' Get Record (airtable)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    export type DynSelect_base_id = string
    export type DynSelect_table_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
    }
    
    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,
      }))
    }
    
    /**
     * Get Record
     * Retrieve one record by its ID (starts with rec).
     */
    export async function main(
      auth: RT.Airtable,
      base_id: DynSelect_base_id,
      table_id: DynSelect_table_id,
      record_id: string,
      return_fields_by_field_id: boolean | undefined
    ) {
      const url = new URL(
        `https://api.airtable.com/v0/${base_id}/${table_id}/${record_id}`
      )
      if (return_fields_by_field_id === true) {
        url.searchParams.append("returnFieldsByFieldId", "true")
      }
    
      const response = await fetch(url, {
        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 5 days ago

  • bunnative
    //native
    
    type Airtable = {
      apiKey: string;
    };
    
    type AirtableTable = {
      baseId: string;
      tableName: string;
    };
    export async function main(
      atCon: Airtable,
      atTable: AirtableTable,
      recordId?: string,
    ) {
      const baseUrl = `https://api.airtable.com/v0/${atTable.baseId}/${encodeURIComponent(
        atTable.tableName,
      )}`;
    
      if (recordId) {
        const response = await fetch(`${baseUrl}/${recordId}`, {
          method: "GET",
          headers: {
            Authorization: `Bearer ${atCon.apiKey}`,
          },
        });
        if (!response.ok) {
          throw new Error(`${response.status} ${await response.text()}`);
        }
        const record = await response.json();
        return { result: record };
      } else {
        const response = await fetch(baseUrl, {
          method: "GET",
          headers: {
            Authorization: `Bearer ${atCon.apiKey}`,
          },
        });
        if (!response.ok) {
          throw new Error(`${response.status} ${await response.text()}`);
        }
        const records = await response.json();
        return { result: records };
      }
    }
    

    Submitted by hugo989 137 days ago