Create Spectrum application using a name for the origin

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

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