0

Get a field on a Card

by
Published Oct 30, 2023

Get a specific property of a card

Script trello Verified

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 399 days ago
1
type Trello = {
2
  key: string;
3
  token: string;
4
};
5
/**
6
 * Get a field on a Card
7
 * Get a specific property of a card
8
 */
9
export async function main(
10
  auth: Trello,
11
  id: string,
12
  field:
13
    | "id"
14
    | "address"
15
    | "badges"
16
    | "checkItemStates"
17
    | "closed"
18
    | "coordinates"
19
    | "creationMethod"
20
    | "dueComplete"
21
    | "dateLastActivity"
22
    | "desc"
23
    | "descData"
24
    | "due"
25
    | "dueReminder"
26
    | "email"
27
    | "idBoard"
28
    | "idChecklists"
29
    | "idLabels"
30
    | "idList"
31
    | "idMembers"
32
    | "idMembersVoted"
33
    | "idShort"
34
    | "idAttachmentCover"
35
    | "labels"
36
    | "limits"
37
    | "locationName"
38
    | "manualCoverAttachment"
39
    | "name"
40
    | "pos"
41
    | "shortLink"
42
    | "shortUrl"
43
    | "subscribed"
44
    | "url"
45
    | "cover"
46
    | "isTemplate"
47
) {
48
  const url = new URL(`https://api.trello.com/1/cards/${id}/${field}`);
49
  for (const [k, v] of [
50
    ["key", auth.key],
51
    ["token", auth.token],
52
  ]) {
53
    if (v !== undefined && v !== "") {
54
      url.searchParams.append(k, v);
55
    }
56
  }
57
  const response = await fetch(url, {
58
    method: "GET",
59
    headers: {
60
      Authorization: undefined,
61
    },
62
    body: undefined,
63
  });
64
  if (!response.ok) {
65
    const text = await response.text();
66
    throw new Error(`${response.status} ${text}`);
67
  }
68
  return await response.json();
69
}
70