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

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //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 10 days ago