Get autonomous systems

Gets a list of autonomous systems (AS).

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