List accounts for a plan

Returns user and organization accounts associated with the specified plan, including free plans.

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 accounts for a plan
6
 * Returns user and organization accounts associated with the specified plan, including free plans.
7
 */
8
export async function main(
9
  auth: Github,
10
  plan_id: string,
11
  sort: "created" | "updated" | undefined,
12
  direction: "asc" | "desc" | undefined,
13
  per_page: string | undefined,
14
  page: string | undefined
15
) {
16
  const url = new URL(
17
    `https://api.github.com/marketplace_listing/plans/${plan_id}/accounts`
18
  );
19
  for (const [k, v] of [
20
    ["sort", sort],
21
    ["direction", direction],
22
    ["per_page", per_page],
23
    ["page", page],
24
  ]) {
25
    if (v !== undefined && v !== "") {
26
      url.searchParams.append(k, v);
27
    }
28
  }
29
  const response = await fetch(url, {
30
    method: "GET",
31
    headers: {
32
      Authorization: "Bearer " + auth.token,
33
    },
34
    body: undefined,
35
  });
36
  if (!response.ok) {
37
    const text = await response.text();
38
    throw new Error(`${response.status} ${text}`);
39
  }
40
  return await response.json();
41
}
42