Retrieve all operations from a schema.

Retrieves all operations from the schema. Operations that already exist in API Shield Endpoint Management will be returned as full operations.

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
 * Retrieve all operations from a schema.
8
 * Retrieves all operations from the schema. Operations that already exist in API Shield Endpoint Management will be returned as full operations.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  schema_id: string,
13
  zone_id: string,
14
  feature: string | undefined,
15
  host: string | undefined,
16
  method: string | undefined,
17
  endpoint: string | undefined,
18
  page: string | undefined,
19
  per_page: string | undefined,
20
  operation_status: "new" | "existing" | undefined
21
) {
22
  const url = new URL(
23
    `https://api.cloudflare.com/client/v4/zones/${zone_id}/api_gateway/user_schemas/${schema_id}/operations`
24
  );
25
  for (const [k, v] of [
26
    ["feature", feature],
27
    ["host", host],
28
    ["method", method],
29
    ["endpoint", endpoint],
30
    ["page", page],
31
    ["per_page", per_page],
32
    ["operation_status", operation_status],
33
  ]) {
34
    if (v !== undefined && v !== "") {
35
      url.searchParams.append(k, v);
36
    }
37
  }
38
  const response = await fetch(url, {
39
    method: "GET",
40
    headers: {
41
      "X-AUTH-EMAIL": auth.email,
42
      "X-AUTH-KEY": auth.key,
43
      Authorization: "Bearer " + auth.token,
44
    },
45
    body: undefined,
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