Start page test

Starts a test for a specific webpage, in a specific region.

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
 * Start page test
8
 * Starts a test for a specific webpage, in a specific region.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  zone_identifier: string,
13
  url: string,
14
  body: {
15
    region?:
16
      | "asia-east1"
17
      | "asia-northeast1"
18
      | "asia-northeast2"
19
      | "asia-south1"
20
      | "asia-southeast1"
21
      | "australia-southeast1"
22
      | "europe-north1"
23
      | "europe-southwest1"
24
      | "europe-west1"
25
      | "europe-west2"
26
      | "europe-west3"
27
      | "europe-west4"
28
      | "europe-west8"
29
      | "europe-west9"
30
      | "me-west1"
31
      | "southamerica-east1"
32
      | "us-central1"
33
      | "us-east1"
34
      | "us-east4"
35
      | "us-south1"
36
      | "us-west1";
37
    [k: string]: unknown;
38
  }
39
) {
40
  const url_ = new URL(
41
    `https://api.cloudflare.com/client/v4/zones/${zone_identifier}/speed_api/pages/${url}/tests`
42
  );
43

44
  const response = await fetch(url_, {
45
    method: "POST",
46
    headers: {
47
      "X-AUTH-EMAIL": auth.email,
48
      "X-AUTH-KEY": auth.key,
49
      "Content-Type": "application/json",
50
      Authorization: "Bearer " + auth.token,
51
    },
52
    body: JSON.stringify(body),
53
  });
54
  if (!response.ok) {
55
    const text = await response.text();
56
    throw new Error(`${response.status} ${text}`);
57
  }
58
  return await response.json();
59
}
60