1 | |
2 | type Actimo = { |
3 | apiKey: string |
4 | } |
5 |
|
6 | export async function main( |
7 | auth: Actimo, |
8 | id: string, |
9 | code: string | undefined, |
10 | body: { allow_email?: false | true; allow_sms?: false | true } |
11 | ) { |
12 | const url = new URL(`https://actimo.com/api/v1/contacts/${id}/channel-preferences'`) |
13 |
|
14 | for (const [k, v] of [['code', code]]) { |
15 | if (v !== undefined && v !== '' && k !== undefined) { |
16 | url.searchParams.append(k, v) |
17 | } |
18 | } |
19 |
|
20 | const response = await fetch(url, { |
21 | method: 'PUT', |
22 | headers: { |
23 | 'api-key': auth.apiKey, |
24 | 'Content-Type': 'application/json' |
25 | }, |
26 | body: JSON.stringify(body) |
27 | }) |
28 |
|
29 | if (!response.ok) { |
30 | const text = await response.text() |
31 | throw new Error(`${response.status} ${text}`) |
32 | } |
33 |
|
34 | return await response.json() |
35 | } |
36 |
|