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