0

List All Images

by
Published Dec 20, 2024

To list all of the images available on your account, send a GET request to /v2/images.

Script digitalocean Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Digitalocean = {
3
  token: string;
4
};
5
/**
6
 * List All Images
7
 * To list all of the images available on your account, send a GET request to /v2/images.
8
 */
9
export async function main(
10
  auth: Digitalocean,
11
  type: "application" | "distribution" | undefined,
12
  showPrivate: string | undefined,
13
  tag_name: string | undefined,
14
  per_page: string | undefined,
15
  page: string | undefined,
16
) {
17
  const url = new URL(`https://api.digitalocean.com/v2/images`);
18
  for (const [k, v] of [
19
    ["type", type],
20
    ["private", showPrivate],
21
    ["tag_name", tag_name],
22
    ["per_page", per_page],
23
    ["page", page],
24
  ]) {
25
    if (v !== undefined && v !== "" && k !== undefined) {
26
      url.searchParams.append(k, v);
27
    }
28
  }
29
  const response = await fetch(url, {
30
    method: "GET",
31
    headers: {
32
      Authorization: "Bearer " + auth.token,
33
    },
34
    body: undefined,
35
  });
36
  if (!response.ok) {
37
    const text = await response.text();
38
    throw new Error(`${response.status} ${text}`);
39
  }
40
  return await response.json();
41
}
42