Edits history of script submission #20795 for ' SearchLoyaltyAccounts (square)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Square = {
      token: string;
    };
    /**
     * SearchLoyaltyAccounts
     * Searches for loyalty accounts in a loyalty program.
    
    You can search for a loyalty account using the phone number or customer ID associated with the account. To return all loyalty accounts, specify an empty `query` object or omit it entirely.
    
    Search results are sorted by `created_at` in ascending order.
     */
    export async function main(
      auth: Square,
      body: {
        query?: {
          mappings?: { id?: string; created_at?: string; phone_number?: string }[];
          customer_ids?: string[];
        };
        limit?: number;
        cursor?: string;
      },
    ) {
      const url = new URL(
        `https://connect.squareup.com/v2/loyalty/accounts/search`,
      );
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
          Authorization: "Bearer " + auth.token,
        },
        body: JSON.stringify(body),
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 235 days ago