List codespaces in a repository for the authenticated user

Lists the codespaces associated to a specified repository and the authenticated user. You must authenticate using an access token with the `codespace` scope to use this endpoint. GitHub Apps must have read access to the `codespaces` repository permission to use this endpoint.

Script github Verified

by hugo697 ยท 10/25/2023

The script

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

8
You must authenticate using an access token with the `codespace` scope to use this endpoint.
9

10
GitHub Apps must have read access to the `codespaces` repository permission to use this endpoint.
11
 */
12
export async function main(
13
  auth: Github,
14
  owner: string,
15
  repo: string,
16
  per_page: string | undefined,
17
  page: string | undefined
18
) {
19
  const url = new URL(
20
    `https://api.github.com/repos/${owner}/${repo}/codespaces`
21
  );
22
  for (const [k, v] of [
23
    ["per_page", per_page],
24
    ["page", page],
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
      Authorization: "Bearer " + auth.token,
34
    },
35
    body: undefined,
36
  });
37
  if (!response.ok) {
38
    const text = await response.text();
39
    throw new Error(`${response.status} ${text}`);
40
  }
41
  return await response.json();
42
}
43