1 | |
2 | type Klaviyo = { |
3 | apiKey: string; |
4 | }; |
5 | |
6 | * Get Variants for Catalog Item |
7 | * Get all variants related to the given item ID. |
8 |
|
9 | Variants can be sorted by the following fields, in ascending and descending order: |
10 | `created` |
11 |
|
12 | Returns a maximum of 100 variants per request.*Rate limits*:Burst: `350/s`Steady: `3500/m` |
13 |
|
14 | */ |
15 | export async function main( |
16 | auth: Klaviyo, |
17 | id: string, |
18 | fields_catalog_variant_: string | undefined, |
19 | filter: string | undefined, |
20 | page_cursor_: string | undefined, |
21 | sort: "created" | "-created" | undefined, |
22 | revision: string, |
23 | ) { |
24 | const url = new URL(`https://a.klaviyo.com/api/catalog-items/${id}/variants`); |
25 | for (const [k, v] of [ |
26 | ["fields[catalog-variant]", fields_catalog_variant_], |
27 | ["filter", filter], |
28 | ["page[cursor]", page_cursor_], |
29 | ["sort", sort], |
30 | ]) { |
31 | if (v !== undefined && v !== "" && k !== undefined) { |
32 | url.searchParams.append(k, v); |
33 | } |
34 | } |
35 | const response = await fetch(url, { |
36 | method: "GET", |
37 | headers: { |
38 | revision: revision, |
39 | "Accept": "application/vnd.api+json", |
40 | Authorization: "Klaviyo-API-Key " + auth.apiKey, |
41 | }, |
42 | body: undefined, |
43 | }); |
44 | if (!response.ok) { |
45 | const text = await response.text(); |
46 | throw new Error(`${response.status} ${text}`); |
47 | } |
48 | return await response.json(); |
49 | } |
50 |
|