Update Spectrum application configuration using a name for the origin

Updates a previously existing application's configuration that uses a name for the origin.

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 Spectrum application configuration using a name for the origin
8
 * Updates a previously existing application's configuration that uses a name for the origin.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  app_id: string,
13
  zone: string,
14
  body: {
15
    argo_smart_routing?: boolean;
16
    dns: { name?: string; type?: "CNAME" | "ADDRESS"; [k: string]: unknown };
17
    edge_ips?:
18
      | {
19
          connectivity?: "all" | "ipv4" | "ipv6";
20
          type?: "dynamic";
21
          [k: string]: unknown;
22
        }
23
      | { ips?: string[]; type?: "static"; [k: string]: unknown };
24
    ip_firewall?: boolean;
25
    origin_dns: {
26
      name?: string;
27
      ttl?: number;
28
      type?: "" | "A" | "AAAA" | "SRV";
29
      [k: string]: unknown;
30
    };
31
    origin_port: number | string;
32
    protocol: string;
33
    proxy_protocol?: "off" | "v1" | "v2" | "simple";
34
    tls?: "off" | "flexible" | "full" | "strict";
35
    traffic_type?: "direct" | "http" | "https";
36
    [k: string]: unknown;
37
  }
38
) {
39
  const url = new URL(
40
    `https://api.cloudflare.com/client/v4/zones/${zone}/spectrum/apps/${app_id}`
41
  );
42

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