0
List notifications for the authenticated user
One script reply has been approved by the moderators Verified

List all notifications for the current user, sorted by most recently updated.

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