1 | |
2 | type Brevo = { |
3 | apiKey: string; |
4 | }; |
5 | |
6 | * Create contact attribute |
7 | * |
8 | */ |
9 | export async function main( |
10 | auth: Brevo, |
11 | attributeCategory: |
12 | | "normal" |
13 | | "transactional" |
14 | | "category" |
15 | | "calculated" |
16 | | "global", |
17 | attributeName: string, |
18 | body: { |
19 | value?: string; |
20 | isRecurring?: false | true; |
21 | enumeration?: { value: number; label: string }[]; |
22 | multiCategoryOptions?: string[]; |
23 | type?: |
24 | | "boolean" |
25 | | "text" |
26 | | "date" |
27 | | "float" |
28 | | "id" |
29 | | "category" |
30 | | "multiple-choice"; |
31 | }, |
32 | ) { |
33 | const url = new URL( |
34 | `https://api.brevo.com/v3/contacts/attributes/${attributeCategory}/${attributeName}`, |
35 | ); |
36 |
|
37 | const response = await fetch(url, { |
38 | method: "POST", |
39 | headers: { |
40 | "Content-Type": "application/json", |
41 | "api-key": auth.apiKey, |
42 | }, |
43 | body: JSON.stringify(body), |
44 | }); |
45 | if (!response.ok) { |
46 | const text = await response.text(); |
47 | throw new Error(`${response.status} ${text}`); |
48 | } |
49 | return await response.text(); |
50 | } |
51 |
|