1 | |
2 | type Persona = { |
3 | apiKey: string; |
4 | }; |
5 | |
6 | * Create an Email Address List Item |
7 | * Create a new item in a >. |
8 | */ |
9 | export async function main( |
10 | auth: Persona, |
11 | body: { |
12 | data?: { |
13 | attributes?: { |
14 | "list-id"?: string; |
15 | "match-type"?: string; |
16 | value?: string; |
17 | }; |
18 | }; |
19 | }, |
20 | include: string | undefined, |
21 | fields: string | undefined, |
22 | Key_Inflection: string, |
23 | Idempotency_Key: string, |
24 | Persona_Version: string, |
25 | ) { |
26 | const url = new URL( |
27 | `https://api.withpersona.com/api/v1/list-item/email-addresses`, |
28 | ); |
29 | for (const [k, v] of [ |
30 | ["include", include], |
31 | ["fields", fields], |
32 | ]) { |
33 | if (v !== undefined && v !== "" && k !== undefined) { |
34 | url.searchParams.append(k, v); |
35 | } |
36 | } |
37 | const headers: Record<string, string> = { |
38 | Authorization: `Bearer ${auth.apiKey}`, |
39 | "Content-Type": "application/json", |
40 | }; |
41 | if (Key_Inflection) { |
42 | headers["Key-Inflection"] = Key_Inflection; |
43 | } |
44 | if (Idempotency_Key) { |
45 | headers["Idempotency-Key"] = Idempotency_Key; |
46 | } |
47 | if (Persona_Version) { |
48 | headers["Persona-Version"] = Persona_Version; |
49 | } |
50 | const response = await fetch(url, { |
51 | method: "POST", |
52 | headers, |
53 | body: JSON.stringify(body), |
54 | }); |
55 | if (!response.ok) { |
56 | const text = await response.text(); |
57 | throw new Error(`${response.status} ${text}`); |
58 | } |
59 | return await response.json(); |
60 | } |
61 |
|