Get contextual information for a user

Provides hovercard information when authenticated through basic auth or OAuth with the `repo` scope.

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
 * Get contextual information for a user
6
 * Provides hovercard information when authenticated through basic auth or OAuth with the `repo` scope.
7
 */
8
export async function main(
9
  auth: Github,
10
  username: string,
11
  subject_type:
12
    | "organization"
13
    | "repository"
14
    | "issue"
15
    | "pull_request"
16
    | undefined,
17
  subject_id: string | undefined
18
) {
19
  const url = new URL(`https://api.github.com/users/${username}/hovercard`);
20
  for (const [k, v] of [
21
    ["subject_type", subject_type],
22
    ["subject_id", subject_id],
23
  ]) {
24
    if (v !== undefined && v !== "") {
25
      url.searchParams.append(k, v);
26
    }
27
  }
28
  const response = await fetch(url, {
29
    method: "GET",
30
    headers: {
31
      Authorization: "Bearer " + auth.token,
32
    },
33
    body: undefined,
34
  });
35
  if (!response.ok) {
36
    const text = await response.text();
37
    throw new Error(`${response.status} ${text}`);
38
  }
39
  return await response.json();
40
}
41