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