Get locations

Get a list of locations.

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
 * Get locations
8
 * Get a list of locations.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  limit: string | undefined,
13
  offset: string | undefined,
14
  location: string | undefined,
15
  format: "JSON" | "CSV" | undefined
16
) {
17
  const url = new URL(
18
    `https://api.cloudflare.com/client/v4/radar/entities/locations`
19
  );
20
  for (const [k, v] of [
21
    ["limit", limit],
22
    ["offset", offset],
23
    ["location", location],
24
    ["format", format],
25
  ]) {
26
    if (v !== undefined && v !== "") {
27
      url.searchParams.append(k, v);
28
    }
29
  }
30
  const response = await fetch(url, {
31
    method: "GET",
32
    headers: {
33
      "X-AUTH-EMAIL": auth.email,
34
      "X-AUTH-KEY": auth.key,
35
      Authorization: "Bearer " + auth.token,
36
    },
37
    body: undefined,
38
  });
39
  if (!response.ok) {
40
    const text = await response.text();
41
    throw new Error(`${response.status} ${text}`);
42
  }
43
  return await response.json();
44
}
45