Patch Health Check

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

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