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