Get default attributes for a codespace

Gets the default attributes for codespaces created by the user with the repository. You must authenticate using an access token with the `codespace` scope to use this endpoint. GitHub Apps must have write 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
 * Get default attributes for a codespace
6
 * Gets the default attributes for codespaces created by the user with the repository.
7

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

10
GitHub Apps must have write 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
  ref: string | undefined,
17
  client_ip: string | undefined
18
) {
19
  const url = new URL(
20
    `https://api.github.com/repos/${owner}/${repo}/codespaces/new`
21
  );
22
  for (const [k, v] of [
23
    ["ref", ref],
24
    ["client_ip", client_ip],
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