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