1 | |
2 | type Klaviyo = { |
3 | apiKey: string; |
4 | }; |
5 | |
6 | * Get Items for Catalog Category |
7 | * Get all items in a category with the given category ID. |
8 |
|
9 | Items can be sorted by the following fields, in ascending and descending order: |
10 | `created` |
11 |
|
12 | Returns a maximum of 100 items 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_item_: string | undefined, |
19 | fields_catalog_variant_: string | undefined, |
20 | filter: string | undefined, |
21 | include: string | undefined, |
22 | page_cursor_: string | undefined, |
23 | sort: "created" | "-created" | undefined, |
24 | revision: string, |
25 | ) { |
26 | const url = new URL( |
27 | `https://a.klaviyo.com/api/catalog-categories/${id}/items`, |
28 | ); |
29 | for (const [k, v] of [ |
30 | ["fields[catalog-item]", fields_catalog_item_], |
31 | ["fields[catalog-variant]", fields_catalog_variant_], |
32 | ["filter", filter], |
33 | ["include", include], |
34 | ["page[cursor]", page_cursor_], |
35 | ["sort", sort], |
36 | ]) { |
37 | if (v !== undefined && v !== "" && k !== undefined) { |
38 | url.searchParams.append(k, v); |
39 | } |
40 | } |
41 | const response = await fetch(url, { |
42 | method: "GET", |
43 | headers: { |
44 | revision: revision, |
45 | "Accept": "application/vnd.api+json", |
46 | Authorization: "Klaviyo-API-Key " + auth.apiKey, |
47 | }, |
48 | body: undefined, |
49 | }); |
50 | if (!response.ok) { |
51 | const text = await response.text(); |
52 | throw new Error(`${response.status} ${text}`); |
53 | } |
54 | return await response.json(); |
55 | } |
56 |
|