1 | |
2 | type Ably = { |
3 | accessToken: string; |
4 | }; |
5 | |
6 | * Retrieve app statistics |
7 | * Retrieves application-level statistics for an app. |
8 | */ |
9 | export async function main( |
10 | auth: Ably, |
11 | id: string, |
12 | start: string | undefined, |
13 | end: string | undefined, |
14 | unit: "minute" | "hour" | "day" | "month" | undefined, |
15 | direction: "forwards" | "backwards" | undefined, |
16 | limit: string | undefined, |
17 | ) { |
18 | const url = new URL(`https://control.ably.net/v1/apps/${id}/stats`); |
19 | for (const [k, v] of [ |
20 | ["start", start], |
21 | ["end", end], |
22 | ["unit", unit], |
23 | ["direction", direction], |
24 | ["limit", limit], |
25 | ]) { |
26 | if (v !== undefined && v !== "" && k !== undefined) { |
27 | url.searchParams.append(k, v); |
28 | } |
29 | } |
30 | const response = await fetch(url, { |
31 | method: "GET", |
32 | headers: { |
33 | Authorization: "Bearer " + auth.accessToken, |
34 | }, |
35 | body: undefined, |
36 | }); |
37 | if (!response.ok) { |
38 | const text = await response.text(); |
39 | throw new Error(`${response.status} ${text}`); |
40 | } |
41 | return await response.json(); |
42 | } |
43 |
|