0
Get portfolio items
One script reply has been approved by the moderators Verified

Get a list of the items in compact form in a portfolio.

Created by hugo697 418 days ago Viewed 9122 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 418 days ago
1
type Asana = {
2
  token: string;
3
};
4
/**
5
 * Get portfolio items
6
 * Get a list of the items in compact form in a portfolio.
7
 */
8
export async function main(
9
  auth: Asana,
10
  portfolio_gid: string,
11
  opt_pretty: string | undefined,
12
  opt_fields: string | undefined,
13
  limit: string | undefined,
14
  offset: string | undefined
15
) {
16
  const url = new URL(
17
    `https://app.asana.com/api/1.0/portfolios/${portfolio_gid}/items`
18
  );
19
  for (const [k, v] of [
20
    ["opt_pretty", opt_pretty],
21
    ["opt_fields", opt_fields],
22
    ["limit", limit],
23
    ["offset", offset],
24
  ]) {
25
    if (v !== undefined && v !== "") {
26
      url.searchParams.append(k, v);
27
    }
28
  }
29
  const response = await fetch(url, {
30
    method: "GET",
31
    headers: {
32
      Authorization: "Bearer " + auth.token,
33
    },
34
    body: undefined,
35
  });
36
  if (!response.ok) {
37
    const text = await response.text();
38
    throw new Error(`${response.status} ${text}`);
39
  }
40
  return await response.json();
41
}
42