1 | type Cloudflare = { |
2 | token: string; |
3 | email: string; |
4 | key: string; |
5 | }; |
6 | |
7 | * Billing History Details |
8 | * Accesses your billing history object. |
9 | */ |
10 | export async function main( |
11 | auth: Cloudflare, |
12 | page: string | undefined, |
13 | per_page: string | undefined, |
14 | order: "type" | "occured_at" | "action" | undefined, |
15 | occured_at: string | undefined, |
16 | occurred_at: string | undefined, |
17 | type: string | undefined, |
18 | action: string | undefined |
19 | ) { |
20 | const url = new URL( |
21 | `https://api.cloudflare.com/client/v4/user/billing/history` |
22 | ); |
23 | for (const [k, v] of [ |
24 | ["page", page], |
25 | ["per_page", per_page], |
26 | ["order", order], |
27 | ["occured_at", occured_at], |
28 | ["occurred_at", occurred_at], |
29 | ["type", type], |
30 | ["action", action], |
31 | ]) { |
32 | if (v !== undefined && v !== "") { |
33 | url.searchParams.append(k, v); |
34 | } |
35 | } |
36 | const response = await fetch(url, { |
37 | method: "GET", |
38 | headers: { |
39 | "X-AUTH-EMAIL": auth.email, |
40 | "X-AUTH-KEY": auth.key, |
41 | Authorization: "Bearer " + auth.token, |
42 | }, |
43 | body: undefined, |
44 | }); |
45 | if (!response.ok) { |
46 | const text = await response.text(); |
47 | throw new Error(`${response.status} ${text}`); |
48 | } |
49 | return await response.json(); |
50 | } |
51 |
|