1 | |
2 | type Klaviyo = { |
3 | apiKey: string; |
4 | }; |
5 | |
6 | * Get Coupon Codes for Coupon |
7 | * Gets a list of coupon codes associated with the given coupon id*Rate limits*:Burst: `75/s`Steady: `700/m` |
8 |
|
9 | */ |
10 | export async function main( |
11 | auth: Klaviyo, |
12 | id: string, |
13 | fields_coupon_code_: string | undefined, |
14 | filter: string | undefined, |
15 | page_cursor_: string | undefined, |
16 | revision: string, |
17 | ) { |
18 | const url = new URL(`https://a.klaviyo.com/api/coupons/${id}/coupon-codes`); |
19 | for (const [k, v] of [ |
20 | ["fields[coupon-code]", fields_coupon_code_], |
21 | ["filter", filter], |
22 | ["page[cursor]", page_cursor_], |
23 | ]) { |
24 | if (v !== undefined && v !== "" && k !== undefined) { |
25 | url.searchParams.append(k, v); |
26 | } |
27 | } |
28 | const response = await fetch(url, { |
29 | method: "GET", |
30 | headers: { |
31 | revision: revision, |
32 | "Accept": "application/vnd.api+json", |
33 | Authorization: "Klaviyo-API-Key " + auth.apiKey, |
34 | }, |
35 | body: undefined, |
36 | }); |
37 | if (!response.ok) { |
38 | const text = await response.text(); |
39 | throw new Error(`${response.status} ${text}`); |
40 | } |
41 | return await response.json(); |
42 | } |
43 |
|