0
Batch Requests
One script reply has been approved by the moderators Verified

Make up to 10 GET requests in a single, batched API call.

Created by hugo697 625 days ago Viewed 22466 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 625 days ago
1
type Trello = {
2
  key: string;
3
  token: string;
4
};
5
/**
6
 * Batch Requests
7
 * Make up to 10 GET requests in a single, batched API call.
8
 */
9
export async function main(auth: Trello, urls: string | undefined) {
10
  const url = new URL(`https://api.trello.com/1/batch`);
11
  for (const [k, v] of [
12
    ["urls", urls],
13
    ["key", auth.key],
14
    ["token", auth.token],
15
  ]) {
16
    if (v !== undefined && v !== "") {
17
      url.searchParams.append(k, v);
18
    }
19
  }
20
  const response = await fetch(url, {
21
    method: "GET",
22
    headers: {
23
      Authorization: undefined,
24
    },
25
    body: undefined,
26
  });
27
  if (!response.ok) {
28
    const text = await response.text();
29
    throw new Error(`${response.status} ${text}`);
30
  }
31
  return await response.text();
32
}
33