0
Create Webhooks for Token
One script reply has been approved by the moderators Verified

Create a new webhook for a Token.

Created by hugo697 606 days ago Viewed 22384 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 606 days ago
1
type Trello = {
2
  key: string;
3
  token: string;
4
};
5
/**
6
 * Create Webhooks for Token
7
 * Create a new webhook for a Token.
8
 */
9
export async function main(
10
  auth: Trello,
11
  token: string,
12
  description: string | undefined,
13
  callbackURL: string | undefined,
14
  idModel: string | undefined
15
) {
16
  const url = new URL(`https://api.trello.com/1/tokens/${token}/webhooks`);
17
  for (const [k, v] of [
18
    ["description", description],
19
    ["callbackURL", callbackURL],
20
    ["idModel", idModel],
21
    ["key", auth.key],
22
    ["token", auth.token],
23
  ]) {
24
    if (v !== undefined && v !== "") {
25
      url.searchParams.append(k, v);
26
    }
27
  }
28
  const response = await fetch(url, {
29
    method: "POST",
30
    headers: {
31
      Authorization: undefined,
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