1 | |
2 | type Persona = { |
3 | apiKey: string; |
4 | }; |
5 | |
6 | * Import Email Address Lists |
7 | * Bulk import email address List Items by uploading a CSV file. |
8 |
|
9 | Each row should be the details for a new List Item. The columns we allow are: |
10 | - value |
11 | - match_type (either 'email_address' or 'domain') |
12 |
|
13 | A match_type of 'email_address' will need to match the entire email address of an individual, while a match_type of 'domain' will match on the email address domain of an individual (i.e. all email addresses with domain 'gmail.com'). |
14 | */ |
15 | export async function main( |
16 | auth: Persona, |
17 | body: { |
18 | data: { |
19 | attributes: { |
20 | file: { data?: string; filename?: string }; |
21 | "list-id": string; |
22 | }; |
23 | }; |
24 | }, |
25 | include?: string, |
26 | fields?: string, |
27 | Key_Inflection?: string, |
28 | Idempotency_Key?: string, |
29 | Persona_Version?: string, |
30 | ) { |
31 | const url = new URL( |
32 | `https://api.withpersona.com/api/v1/importer/list-item/email-addresses`, |
33 | ); |
34 | for (const [k, v] of [ |
35 | ["include", include], |
36 | ["fields", fields], |
37 | ]) { |
38 | if (v !== undefined && v !== "" && k !== undefined) { |
39 | url.searchParams.append(k, v); |
40 | } |
41 | } |
42 | const formData = new FormData(); |
43 | for (const [k, v] of Object.entries(body)) { |
44 | if (v !== undefined) { |
45 | formData.append(k, String(v)); |
46 | } |
47 | } |
48 | const headers: Record<string, string> = { |
49 | Authorization: `Bearer ${auth.apiKey}`, |
50 | "Content-Type": "application/json", |
51 | }; |
52 | if (Key_Inflection) { |
53 | headers["Key-Inflection"] = Key_Inflection; |
54 | } |
55 | if (Idempotency_Key) { |
56 | headers["Idempotency-Key"] = Idempotency_Key; |
57 | } |
58 | if (Persona_Version) { |
59 | headers["Persona-Version"] = Persona_Version; |
60 | } |
61 | const response = await fetch(url, { |
62 | method: "POST", |
63 | headers, |
64 | body: JSON.stringify(body), |
65 | }); |
66 | if (!response.ok) { |
67 | const text = await response.text(); |
68 | throw new Error(`${response.status} ${text}`); |
69 | } |
70 | return await response.json(); |
71 | } |
72 |
|