Create scheduled page test

Creates a scheduled test for a page.

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