1 | |
2 | type Klaviyo = { |
3 | apiKey: string; |
4 | }; |
5 | |
6 | * Get Profile |
7 | * Get the profile with the given profile ID. |
8 | */ |
9 | export async function main( |
10 | auth: Klaviyo, |
11 | id: string, |
12 | additional_fields_profile_: string | undefined, |
13 | fields_list_: string | undefined, |
14 | fields_profile_: string | undefined, |
15 | fields_segment_: string | undefined, |
16 | include: string | undefined, |
17 | revision: string, |
18 | ) { |
19 | const url = new URL(`https://a.klaviyo.com/api/profiles/${id}`); |
20 | for (const [k, v] of [ |
21 | ["additional-fields[profile]", additional_fields_profile_], |
22 | ["fields[list]", fields_list_], |
23 | ["fields[profile]", fields_profile_], |
24 | ["fields[segment]", fields_segment_], |
25 | ["include", include], |
26 | ]) { |
27 | if (v !== undefined && v !== "" && k !== undefined) { |
28 | url.searchParams.append(k, v); |
29 | } |
30 | } |
31 | const response = await fetch(url, { |
32 | method: "GET", |
33 | headers: { |
34 | revision: revision, |
35 | "Accept": "application/vnd.api+json", |
36 | Authorization: "Klaviyo-API-Key " + auth.apiKey, |
37 | }, |
38 | body: undefined, |
39 | }); |
40 | if (!response.ok) { |
41 | const text = await response.text(); |
42 | throw new Error(`${response.status} ${text}`); |
43 | } |
44 | return await response.json(); |
45 | } |
46 |
|