List team members (Legacy)

**Deprecation Notice:** This endpoint route is deprecated and will be removed from the Teams API. We recommend migrating your existing code to use the new [`List team members`](https://docs.github.com/rest/reference/teams#list-team-members) endpoint. Team members will include the members of child teams.

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 team members (Legacy)
6
 * **Deprecation Notice:** This endpoint route is deprecated and will be removed from the Teams API. We recommend migrating your existing code to use the new [`List team members`](https://docs.github.com/rest/reference/teams#list-team-members) endpoint.
7

8
Team members will include the members of child teams.
9
 */
10
export async function main(
11
  auth: Github,
12
  team_id: string,
13
  role: "member" | "maintainer" | "all" | undefined,
14
  per_page: string | undefined,
15
  page: string | undefined
16
) {
17
  const url = new URL(`https://api.github.com/teams/${team_id}/members`);
18
  for (const [k, v] of [
19
    ["role", role],
20
    ["per_page", per_page],
21
    ["page", page],
22
  ]) {
23
    if (v !== undefined && v !== "") {
24
      url.searchParams.append(k, v);
25
    }
26
  }
27
  const response = await fetch(url, {
28
    method: "GET",
29
    headers: {
30
      Authorization: "Bearer " + auth.token,
31
    },
32
    body: undefined,
33
  });
34
  if (!response.ok) {
35
    const text = await response.text();
36
    throw new Error(`${response.status} ${text}`);
37
  }
38
  return await response.json();
39
}
40