List fleet status devices

List details for devices using WARP

Script cloudflare Verified

by hugo697 ยท 11/16/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 383 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * List fleet status devices
8
 * List details for devices using WARP
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  account_identifier: string,
13
  time_end: string | undefined,
14
  time_start: string | undefined,
15
  page: string | undefined,
16
  per_page: string | undefined,
17
  sort_by:
18
    | "colo"
19
    | "device_id"
20
    | "mode"
21
    | "platform"
22
    | "status"
23
    | "timestamp"
24
    | "version"
25
    | undefined,
26
  colo: string | undefined,
27
  device_id: string | undefined,
28
  mode: string | undefined,
29
  status: string | undefined,
30
  platform: string | undefined,
31
  version: string | undefined
32
) {
33
  const url = new URL(
34
    `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/dex/fleet-status/devices`
35
  );
36
  for (const [k, v] of [
37
    ["time_end", time_end],
38
    ["time_start", time_start],
39
    ["page", page],
40
    ["per_page", per_page],
41
    ["sort_by", sort_by],
42
    ["colo", colo],
43
    ["device_id", device_id],
44
    ["mode", mode],
45
    ["status", status],
46
    ["platform", platform],
47
    ["version", version],
48
  ]) {
49
    if (v !== undefined && v !== "") {
50
      url.searchParams.append(k, v);
51
    }
52
  }
53
  const response = await fetch(url, {
54
    method: "GET",
55
    headers: {
56
      "X-AUTH-EMAIL": auth.email,
57
      "X-AUTH-KEY": auth.key,
58
      Authorization: "Bearer " + auth.token,
59
    },
60
    body: undefined,
61
  });
62
  if (!response.ok) {
63
    const text = await response.text();
64
    throw new Error(`${response.status} ${text}`);
65
  }
66
  return await response.json();
67
}
68