0

List repository notifications for the authenticated user

by
Published Oct 25, 2023

Lists all notifications for the current user in the specified repository.

Script github Verified

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 398 days ago
1
type Github = {
2
  token: string;
3
};
4
/**
5
 * List repository notifications for the authenticated user
6
 * Lists all notifications for the current user in the specified repository.
7
 */
8
export async function main(
9
  auth: Github,
10
  owner: string,
11
  repo: string,
12
  all: string | undefined,
13
  participating: string | undefined,
14
  since: string | undefined,
15
  before: string | undefined,
16
  per_page: string | undefined,
17
  page: string | undefined
18
) {
19
  const url = new URL(
20
    `https://api.github.com/repos/${owner}/${repo}/notifications`
21
  );
22
  for (const [k, v] of [
23
    ["all", all],
24
    ["participating", participating],
25
    ["since", since],
26
    ["before", before],
27
    ["per_page", per_page],
28
    ["page", page],
29
  ]) {
30
    if (v !== undefined && v !== "") {
31
      url.searchParams.append(k, v);
32
    }
33
  }
34
  const response = await fetch(url, {
35
    method: "GET",
36
    headers: {
37
      Authorization: "Bearer " + auth.token,
38
    },
39
    body: undefined,
40
  });
41
  if (!response.ok) {
42
    const text = await response.text();
43
    throw new Error(`${response.status} ${text}`);
44
  }
45
  return await response.json();
46
}
47