0

Create a Label

by
Published Oct 30, 2023

Create a new Label on a Board.

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
 * Create a Label
7
 * Create a new Label on a Board.
8
 */
9
export async function main(
10
  auth: Trello,
11
  name: string | undefined,
12
  color:
13
    | "yellow"
14
    | "purple"
15
    | "blue"
16
    | "red"
17
    | "green"
18
    | "orange"
19
    | "black"
20
    | "sky"
21
    | "pink"
22
    | "lime"
23
    | undefined,
24
  idBoard: string | undefined
25
) {
26
  const url = new URL(`https://api.trello.com/1/labels`);
27
  for (const [k, v] of [
28
    ["name", name],
29
    ["color", color],
30
    ["idBoard", idBoard],
31
    ["key", auth.key],
32
    ["token", auth.token],
33
  ]) {
34
    if (v !== undefined && v !== "") {
35
      url.searchParams.append(k, v);
36
    }
37
  }
38
  const response = await fetch(url, {
39
    method: "POST",
40
    headers: {
41
      Authorization: undefined,
42
    },
43
    body: undefined,
44
  });
45
  if (!response.ok) {
46
    const text = await response.text();
47
    throw new Error(`${response.status} ${text}`);
48
  }
49
  return await response.text();
50
}
51