Get list items

Fetches all the items in the list.

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
 * Get list items
8
 * Fetches all the items in the list.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  list_id: string,
13
  account_identifier: string,
14
  cursor: string | undefined,
15
  per_page: string | undefined,
16
  search: string | undefined
17
) {
18
  const url = new URL(
19
    `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/rules/lists/${list_id}/items`
20
  );
21
  for (const [k, v] of [
22
    ["cursor", cursor],
23
    ["per_page", per_page],
24
    ["search", search],
25
  ]) {
26
    if (v !== undefined && v !== "") {
27
      url.searchParams.append(k, v);
28
    }
29
  }
30
  const response = await fetch(url, {
31
    method: "GET",
32
    headers: {
33
      "X-AUTH-EMAIL": auth.email,
34
      "X-AUTH-KEY": auth.key,
35
      Authorization: "Bearer " + auth.token,
36
    },
37
    body: undefined,
38
  });
39
  if (!response.ok) {
40
    const text = await response.text();
41
    throw new Error(`${response.status} ${text}`);
42
  }
43
  return await response.json();
44
}
45