0

Get a field of a Notification

by
Published Oct 30, 2023

Get a specific property of a notification

Script trello Verified

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 398 days ago
1
type Trello = {
2
  key: string;
3
  token: string;
4
};
5
/**
6
 * Get a field of a Notification
7
 * Get a specific property of a notification
8
 */
9
export async function main(
10
  auth: Trello,
11
  id: string,
12
  field:
13
    | "id"
14
    | "unread"
15
    | "type"
16
    | "date"
17
    | "dateRead"
18
    | "data"
19
    | "card"
20
    | "board"
21
    | "idMemberCreator"
22
    | "idAction"
23
    | "reactions"
24
) {
25
  const url = new URL(`https://api.trello.com/1/notifications/${id}/${field}`);
26
  for (const [k, v] of [
27
    ["key", auth.key],
28
    ["token", auth.token],
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: undefined,
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