Update an IP Access rule

Updates an IP Access rule defined at the account level. Note: This operation will affect all zones in the account.

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
 * Update an IP Access rule
8
 * Updates an IP Access rule defined at the account level.
9

10
Note: This operation will affect all zones in the account.
11
 */
12
export async function main(
13
  auth: Cloudflare,
14
  identifier: string,
15
  account_identifier: string,
16
  body: {
17
    allowed_modes: (
18
      | "block"
19
      | "challenge"
20
      | "whitelist"
21
      | "js_challenge"
22
      | "managed_challenge"
23
    )[];
24
    configuration:
25
      | { target?: "ip"; value?: string; [k: string]: unknown }
26
      | { target?: "ip6"; value?: string; [k: string]: unknown }
27
      | { target?: "ip_range"; value?: string; [k: string]: unknown }
28
      | { target?: "asn"; value?: string; [k: string]: unknown }
29
      | { target?: "country"; value?: string; [k: string]: unknown };
30
    created_on?: string;
31
    id: string;
32
    mode:
33
      | "block"
34
      | "challenge"
35
      | "whitelist"
36
      | "js_challenge"
37
      | "managed_challenge";
38
    modified_on?: string;
39
    notes?: string;
40
    [k: string]: unknown;
41
  } & {
42
    scope?: {
43
      email?: string;
44
      id?: string;
45
      type?: "user" | "organization";
46
      [k: string]: unknown;
47
    };
48
    [k: string]: unknown;
49
  }
50
) {
51
  const url = new URL(
52
    `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/firewall/access_rules/rules/${identifier}`
53
  );
54

55
  const response = await fetch(url, {
56
    method: "PATCH",
57
    headers: {
58
      "X-AUTH-EMAIL": auth.email,
59
      "X-AUTH-KEY": auth.key,
60
      "Content-Type": "application/json",
61
      Authorization: "Bearer " + auth.token,
62
    },
63
    body: JSON.stringify(body),
64
  });
65
  if (!response.ok) {
66
    const text = await response.text();
67
    throw new Error(`${response.status} ${text}`);
68
  }
69
  return await response.json();
70
}
71