Edits history of script submission #12337 for ' Get Catalog Items (klaviyo)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Klaviyo = {
      apiKey: string;
    };
    /**
     * Get Catalog Items
     * Get all catalog items in an account.
    
    Catalog items can be sorted by the following fields, in ascending and descending order:
    `created`
    
    Currently, the only supported integration type is `$custom`, and the only supported catalog type is `$default`.
    
    Returns a maximum of 100 items per request.*Rate limits*:Burst: `350/s`Steady: `3500/m`
    
     */
    export async function main(
      auth: Klaviyo,
      fields_catalog_item_: string | undefined,
      fields_catalog_variant_: string | undefined,
      filter: string | undefined,
      include: string | undefined,
      page_cursor_: string | undefined,
      sort: "created" | "-created" | undefined,
      revision: string,
    ) {
      const url = new URL(`https://a.klaviyo.com/api/catalog-items`);
      for (const [k, v] of [
        ["fields[catalog-item]", fields_catalog_item_],
        ["fields[catalog-variant]", fields_catalog_variant_],
        ["filter", filter],
        ["include", include],
        ["page[cursor]", page_cursor_],
        ["sort", sort],
      ]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      const response = await fetch(url, {
        method: "GET",
        headers: {
          revision: revision,
          "Accept": "application/vnd.api+json",
          Authorization: "Klaviyo-API-Key " + auth.apiKey,
        },
        body: undefined,
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 428 days ago