List codespaces for the organization

Lists the codespaces associated to a specified organization. You must authenticate using an access token with the `admin:org` scope to use this endpoint.

Script github Verified

by hugo697 ยท 10/25/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 366 days ago
1
type Github = {
2
  token: string;
3
};
4
/**
5
 * List codespaces for the organization
6
 * Lists the codespaces associated to a specified organization.
7

8
You must authenticate using an access token with the `admin:org` scope to use this endpoint.
9
 */
10
export async function main(
11
  auth: Github,
12
  org: string,
13
  per_page: string | undefined,
14
  page: string | undefined
15
) {
16
  const url = new URL(`https://api.github.com/orgs/${org}/codespaces`);
17
  for (const [k, v] of [
18
    ["per_page", per_page],
19
    ["page", page],
20
  ]) {
21
    if (v !== undefined && v !== "") {
22
      url.searchParams.append(k, v);
23
    }
24
  }
25
  const response = await fetch(url, {
26
    method: "GET",
27
    headers: {
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