0
List Hostname Associations
One script reply has been approved by the moderators Verified

List Hostname Associations

Created by hugo697 174 days ago Viewed 5851 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 174 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * List Hostname Associations
8
 * List Hostname Associations
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  zone_identifier: string,
13
  mtls_certificate_id: string | undefined
14
) {
15
  const url = new URL(
16
    `https://api.cloudflare.com/client/v4/zones/${zone_identifier}/certificate_authorities/hostname_associations`
17
  );
18
  for (const [k, v] of [["mtls_certificate_id", mtls_certificate_id]]) {
19
    if (v !== undefined && v !== "") {
20
      url.searchParams.append(k, v);
21
    }
22
  }
23
  const response = await fetch(url, {
24
    method: "GET",
25
    headers: {
26
      "X-AUTH-EMAIL": auth.email,
27
      "X-AUTH-KEY": auth.key,
28
      Authorization: "Bearer " + auth.token,
29
    },
30
    body: undefined,
31
  });
32
  if (!response.ok) {
33
    const text = await response.text();
34
    throw new Error(`${response.status} ${text}`);
35
  }
36
  return await response.json();
37
}
38