Update custom profile

Updates a DLP custom 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 custom profile
8
 * Updates a DLP custom profile.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  profile_id: string,
13
  account_identifier: string,
14
  body: {
15
    allowed_match_count?: number;
16
    description?: string;
17
    entries?: {
18
      created_at?: string;
19
      enabled?: boolean;
20
      id?: string;
21
      name?: string;
22
      pattern?: { regex: string; validation?: "luhn"; [k: string]: unknown };
23
      profile_id?: { [k: string]: unknown };
24
      updated_at?: string;
25
      [k: string]: unknown;
26
    }[];
27
    name?: string;
28
    shared_entries?: (
29
      | { enabled?: boolean; entry_id?: string; [k: string]: unknown }
30
      | { enabled?: boolean; entry_id?: string; [k: string]: unknown }
31
    )[];
32
    [k: string]: unknown;
33
  }
34
) {
35
  const url = new URL(
36
    `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/dlp/profiles/custom/${profile_id}`
37
  );
38

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