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