Create Health Check

Create a new health check.

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 Health Check
8
 * Create a new health check.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  zone_identifier: string,
13
  body: {
14
    address: string;
15
    check_regions?: (
16
      | "WNAM"
17
      | "ENAM"
18
      | "WEU"
19
      | "EEU"
20
      | "NSAM"
21
      | "SSAM"
22
      | "OC"
23
      | "ME"
24
      | "NAF"
25
      | "SAF"
26
      | "IN"
27
      | "SEAS"
28
      | "NEAS"
29
      | "ALL_REGIONS"
30
    )[];
31
    consecutive_fails?: number;
32
    consecutive_successes?: number;
33
    description?: string;
34
    http_config?: {
35
      allow_insecure?: boolean;
36
      expected_body?: string;
37
      expected_codes?: string & string[];
38
      follow_redirects?: boolean;
39
      header?: { [k: string]: unknown };
40
      method?: "GET" | "HEAD";
41
      path?: string;
42
      port?: number;
43
      [k: string]: unknown;
44
    };
45
    interval?: number;
46
    name: string;
47
    retries?: number;
48
    suspended?: boolean;
49
    tcp_config?: {
50
      method?: "connection_established";
51
      port?: number;
52
      [k: string]: unknown;
53
    };
54
    timeout?: number;
55
    type?: string;
56
    [k: string]: unknown;
57
  }
58
) {
59
  const url = new URL(
60
    `https://api.cloudflare.com/client/v4/zones/${zone_identifier}/healthchecks`
61
  );
62

63
  const response = await fetch(url, {
64
    method: "POST",
65
    headers: {
66
      "X-AUTH-EMAIL": auth.email,
67
      "X-AUTH-KEY": auth.key,
68
      "Content-Type": "application/json",
69
      Authorization: "Bearer " + auth.token,
70
    },
71
    body: JSON.stringify(body),
72
  });
73
  if (!response.ok) {
74
    const text = await response.text();
75
    throw new Error(`${response.status} ${text}`);
76
  }
77
  return await response.json();
78
}
79