Update a device settings profile

Updates a configured device settings profile.

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 a device settings profile
8
 * Updates a configured device settings profile.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  uuid: string,
13
  identifier: string,
14
  body: {
15
    allow_mode_switch?: boolean;
16
    allow_updates?: boolean;
17
    allowed_to_leave?: boolean;
18
    auto_connect?: number;
19
    captive_portal?: number;
20
    description?: string;
21
    disable_auto_fallback?: boolean;
22
    enabled?: boolean;
23
    exclude_office_ips?: boolean;
24
    match?: string;
25
    name?: string;
26
    precedence?: number;
27
    service_mode_v2?: { mode?: string; port?: number; [k: string]: unknown };
28
    support_url?: string;
29
    switch_locked?: boolean;
30
    [k: string]: unknown;
31
  }
32
) {
33
  const url = new URL(
34
    `https://api.cloudflare.com/client/v4/accounts/${identifier}/devices/policy/${uuid}`
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