1 |
|
2 | type Adhook = { |
3 | token: string |
4 | } |
5 |
|
6 | export async function main( |
7 | auth: Adhook, |
8 | id: string, |
9 | skip: string | undefined, |
10 | limit: string | undefined, |
11 | sortBy: string | undefined, |
12 | sortDirection: string | undefined, |
13 | start: string | undefined, |
14 | end: string | undefined, |
15 | fetchMode: 'AGGREGATED' | 'DAILY' | undefined, |
16 | statusFilter: 'ALL' | 'ACTIVE' | undefined, |
17 | adhookToken: string |
18 | ) { |
19 | const url = new URL(`https://app.adhook.io/v1/promotions/${id}/adStatistic`) |
20 |
|
21 | for (const [k, v] of [ |
22 | ['skip', skip], |
23 | ['limit', limit], |
24 | ['sortBy', sortBy], |
25 | ['sortDirection', sortDirection], |
26 | ['start', start], |
27 | ['end', end], |
28 | ['fetchMode', fetchMode], |
29 | ['statusFilter', statusFilter] |
30 | ]) { |
31 | if (v !== undefined && v !== '' && k !== undefined) { |
32 | url.searchParams.append(k, v) |
33 | } |
34 | } |
35 |
|
36 | const response = await fetch(url, { |
37 | method: 'GET', |
38 | headers: { |
39 | adhookToken: adhookToken, |
40 | Authorization: `Bearer ${auth.token}` |
41 | }, |
42 | body: undefined |
43 | }) |
44 |
|
45 | if (!response.ok) { |
46 | const text = await response.text() |
47 | throw new Error(`${response.status} ${text}`) |
48 | } |
49 |
|
50 | return await response.json() |
51 | } |
52 |
|