Edits history of script submission #20797 for ' SearchLoyaltyRewards (square)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Square = {
      token: string;
    };
    /**
     * SearchLoyaltyRewards
     * Searches for loyalty rewards. This endpoint accepts a request with no query filters and returns results for all loyalty accounts.
    If you include a `query` object, `loyalty_account_id` is required and `status` is  optional.
    
    If you know a reward ID, use the
    [RetrieveLoyaltyReward]($e/Loyalty/RetrieveLoyaltyReward) endpoint.
    
    Search results are sorted by `updated_at` in descending order.
     */
    export async function main(
      auth: Square,
      body: {
        query?: {
          loyalty_account_id: string;
          status?: "ISSUED" | "REDEEMED" | "DELETED";
        };
        limit?: number;
        cursor?: string;
      },
    ) {
      const url = new URL(`https://connect.squareup.com/v2/loyalty/rewards/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