Edits history of script submission #22441 for ' Get List (mailchimp)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * @param fields *(optional)* A list of fields to return in the response.
     * Reference parameters of sub-objects with dot notation.
     *
     * @param exclude_fields *(optional)* A list of fields to exclude from the response.
     * Reference parameters of sub-objects with dot notation. If both `fields` and `exclude_fields`
     * are present, then only `exclude_fields` will be used.
     */
    type Mailchimp = {
      api_key: string;
      server: string;
    };
    export async function main(
      auth: Mailchimp,
      list_id: string,
      fields?: string[],
      exclude_fields?: string[],
      include_total_contacts?: boolean,
    ) {
      const url = new URL(
        `https://${auth.server}.api.mailchimp.com/3.0/lists/${list_id}`,
      );
      const params = {
        fields,
        exclude_fields,
        include_total_contacts,
      };
      for (const key in params) {
        const value = params[<keyof typeof params>key];
        if (value) {
          url.searchParams.append(
            key,
            Array.isArray(value) ? value.join(",") : "" + value,
          );
        }
      }
    
      const response = await fetch(url, {
        method: "GET",
        headers: {
          Authorization: `Bearer ${auth.api_key}`,
        },
      });
    
      if (!response.ok) {
        throw Error(await response.text());
      }
      return await response.json();
    }
    

    Submitted by hugo989 6 days ago