1 |
|
2 | type Adhook = { |
3 | token: string |
4 | } |
5 |
|
6 | export async function main( |
7 | auth: Adhook, |
8 | id: string, |
9 | start: string | undefined, |
10 | end: string | undefined, |
11 | fetchMode: 'AGGREGATED' | 'DAILY' | undefined, |
12 | adhookToken: string |
13 | ) { |
14 | const url = new URL(`https://app.adhook.io/v1/promotions/${id}/placementStatistic`) |
15 |
|
16 | for (const [k, v] of [ |
17 | ['start', start], |
18 | ['end', end], |
19 | ['fetchMode', fetchMode] |
20 | ]) { |
21 | if (v !== undefined && v !== '' && k !== undefined) { |
22 | url.searchParams.append(k, v) |
23 | } |
24 | } |
25 |
|
26 | const response = await fetch(url, { |
27 | method: 'GET', |
28 | headers: { |
29 | adhookToken: adhookToken, |
30 | Authorization: `Bearer ${auth.token}` |
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 |
|