0
Patch Monitor
One script reply has been approved by the moderators Verified

Apply changes to an existing monitor, overwriting the supplied properties.

Created by hugo697 254 days ago Viewed 8900 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 254 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * Patch Monitor
8
 * Apply changes to an existing monitor, overwriting the supplied properties.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  identifier: string,
13
  body: {
14
    allow_insecure?: boolean;
15
    consecutive_down?: number;
16
    consecutive_up?: number;
17
    description?: string;
18
    expected_body?: string;
19
    expected_codes?: string;
20
    follow_redirects?: boolean;
21
    header?: { [k: string]: unknown };
22
    interval?: number;
23
    method?: string;
24
    path?: string;
25
    port?: number;
26
    probe_zone?: string;
27
    retries?: number;
28
    timeout?: number;
29
    type?: "http" | "https" | "tcp" | "udp_icmp" | "icmp_ping" | "smtp";
30
    [k: string]: unknown;
31
  } & { [k: string]: unknown }
32
) {
33
  const url = new URL(
34
    `https://api.cloudflare.com/client/v4/user/load_balancers/monitors/${identifier}`
35
  );
36

37
  const response = await fetch(url, {
38
    method: "PATCH",
39
    headers: {
40
      "X-AUTH-EMAIL": auth.email,
41
      "X-AUTH-KEY": auth.key,
42
      "Content-Type": "application/json",
43
      Authorization: "Bearer " + auth.token,
44
    },
45
    body: JSON.stringify(body),
46
  });
47
  if (!response.ok) {
48
    const text = await response.text();
49
    throw new Error(`${response.status} ${text}`);
50
  }
51
  return await response.json();
52
}
53