0

List Organizations

by
Published 4 days ago

List the organizations the authenticated user belongs to. Private memberships need the read:org scope (classic tokens).

Script github Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 4 days ago
1
//native
2

3
/**
4
 * List Organizations
5
 * List the organizations the authenticated user belongs to. Private memberships need the read:org scope (classic tokens).
6
 */
7
export async function main(
8
  auth: RT.Github,
9
  per_page: number = 30,
10
  page: number = 1
11
) {
12
  const url = new URL("https://api.github.com/user/orgs")
13
  for (const [k, v] of Object.entries({ per_page, page })) {
14
    if (v != null) url.searchParams.append(k, String(v))
15
  }
16

17
  const response = await fetch(url, {
18
    headers: {
19
      Authorization: `Bearer ${auth.token}`,
20
      Accept: "application/vnd.github+json",
21
      "X-GitHub-Api-Version": "2022-11-28",
22
    },
23
  })
24

25
  if (!response.ok) {
26
    throw new Error(`${response.status} ${await response.text()}`)
27
  }
28

29
  return await response.json()
30
}
31