List page test history

Test history (list of tests) for a specific webpage.

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 page test history
8
 * Test history (list of tests) for a specific webpage.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  zone_identifier: string,
13
  url: string,
14
  page: string | undefined,
15
  per_page: string | undefined,
16
  region:
17
    | "asia-east1"
18
    | "asia-northeast1"
19
    | "asia-northeast2"
20
    | "asia-south1"
21
    | "asia-southeast1"
22
    | "australia-southeast1"
23
    | "europe-north1"
24
    | "europe-southwest1"
25
    | "europe-west1"
26
    | "europe-west2"
27
    | "europe-west3"
28
    | "europe-west4"
29
    | "europe-west8"
30
    | "europe-west9"
31
    | "me-west1"
32
    | "southamerica-east1"
33
    | "us-central1"
34
    | "us-east1"
35
    | "us-east4"
36
    | "us-south1"
37
    | "us-west1"
38
    | undefined
39
) {
40
  const url_ = new URL(
41
    `https://api.cloudflare.com/client/v4/zones/${zone_identifier}/speed_api/pages/${url}/tests`
42
  );
43
  for (const [k, v] of [
44
    ["page", page],
45
    ["per_page", per_page],
46
    ["region", region],
47
  ]) {
48
    if (v !== undefined && v !== "") {
49
      url_.searchParams.append(k, v);
50
    }
51
  }
52
  const response = await fetch(url_, {
53
    method: "GET",
54
    headers: {
55
      "X-AUTH-EMAIL": auth.email,
56
      "X-AUTH-KEY": auth.key,
57
      Authorization: "Bearer " + auth.token,
58
    },
59
    body: undefined,
60
  });
61
  if (!response.ok) {
62
    const text = await response.text();
63
    throw new Error(`${response.status} ${text}`);
64
  }
65
  return await response.json();
66
}
67