1 | |
2 | type Persona = { |
3 | apiKey: string; |
4 | }; |
5 | |
6 | * Import Name Lists |
7 | * Bulk import name 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 | - name_first |
11 | - name_last |
12 | */ |
13 | export async function main( |
14 | auth: Persona, |
15 | body: { |
16 | data: { |
17 | attributes: { |
18 | file: { data?: string; filename?: string }; |
19 | "list-id": string; |
20 | }; |
21 | }; |
22 | }, |
23 | include?: string, |
24 | fields?: string, |
25 | Key_Inflection?: string, |
26 | Idempotency_Key?: string, |
27 | Persona_Version?: string, |
28 | ) { |
29 | const url = new URL( |
30 | `https://api.withpersona.com/api/v1/importer/list-item/names`, |
31 | ); |
32 | for (const [k, v] of [ |
33 | ["include", include], |
34 | ["fields", fields], |
35 | ]) { |
36 | if (v !== undefined && v !== "" && k !== undefined) { |
37 | url.searchParams.append(k, v); |
38 | } |
39 | } |
40 | const formData = new FormData(); |
41 | for (const [k, v] of Object.entries(body)) { |
42 | if (v !== undefined) { |
43 | formData.append(k, String(v)); |
44 | } |
45 | } |
46 | const headers: Record<string, string> = { |
47 | Authorization: `Bearer ${auth.apiKey}`, |
48 | "Content-Type": "application/json", |
49 | }; |
50 | if (Key_Inflection) { |
51 | headers["Key-Inflection"] = Key_Inflection; |
52 | } |
53 | if (Idempotency_Key) { |
54 | headers["Idempotency-Key"] = Idempotency_Key; |
55 | } |
56 | if (Persona_Version) { |
57 | headers["Persona-Version"] = Persona_Version; |
58 | } |
59 | const response = await fetch(url, { |
60 | method: "POST", |
61 | headers, |
62 | body: JSON.stringify(body), |
63 | }); |
64 | if (!response.ok) { |
65 | const text = await response.text(); |
66 | throw new Error(`${response.status} ${text}`); |
67 | } |
68 | return await response.json(); |
69 | } |
70 |
|