1 | |
2 | type Actimo = { |
3 | apiKey: string |
4 | } |
5 |
|
6 | export async function main( |
7 | auth: Actimo, |
8 | targetId: string, |
9 | type: string, |
10 | sourceId: string | undefined, |
11 | sourceAuthCode: string | undefined, |
12 | messageId: string | undefined, |
13 | before: string | undefined |
14 | ) { |
15 | const url = new URL(`https://actimo.com/api/v1/contacts/${targetId}/insights/${type}`) |
16 |
|
17 | for (const [k, v] of [ |
18 | ['sourceId', sourceId], |
19 | ['sourceAuthCode', sourceAuthCode], |
20 | ['messageId', messageId], |
21 | ['before', before] |
22 | ]) { |
23 | if (v !== undefined && v !== '' && k !== undefined) { |
24 | url.searchParams.append(k, v) |
25 | } |
26 | } |
27 |
|
28 | const response = await fetch(url, { |
29 | method: 'GET', |
30 | headers: { |
31 | 'api-key': auth.apiKey |
32 | }, |
33 | body: undefined |
34 | }) |
35 |
|
36 | if (!response.ok) { |
37 | const text = await response.text() |
38 | throw new Error(`${response.status} ${text}`) |
39 | } |
40 |
|
41 | return await response.json() |
42 | } |
43 |
|