Update a portfolio

An existing portfolio can be updated by making a PUT request on the URL for that portfolio. Only the fields provided in the `data` block will be updated; any unspecified fields will remain unchanged. Returns the complete updated portfolio record.

Script asana Verified

by hugo697 ยท 10/31/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 383 days ago
1
type Asana = {
2
  token: string;
3
};
4
/**
5
 * Update a portfolio
6
 * An existing portfolio can be updated by making a PUT request on the URL for
7
that portfolio. Only the fields provided in the `data` block will be updated;
8
any unspecified fields will remain unchanged.
9

10
Returns the complete updated portfolio record.
11
 */
12
export async function main(
13
  auth: Asana,
14
  portfolio_gid: string,
15
  opt_pretty: string | undefined,
16
  opt_fields: string | undefined,
17
  body: {
18
    data?: (({ gid?: string; resource_type?: string; [k: string]: unknown } & {
19
      name?: string;
20
      [k: string]: unknown;
21
    }) & {
22
      color?:
23
        | "dark-pink"
24
        | "dark-green"
25
        | "dark-blue"
26
        | "dark-red"
27
        | "dark-teal"
28
        | "dark-brown"
29
        | "dark-orange"
30
        | "dark-purple"
31
        | "dark-warm-gray"
32
        | "light-pink"
33
        | "light-green"
34
        | "light-blue"
35
        | "light-red"
36
        | "light-teal"
37
        | "light-brown"
38
        | "light-orange"
39
        | "light-purple"
40
        | "light-warm-gray";
41
      [k: string]: unknown;
42
    }) & {
43
      members?: string[];
44
      public?: boolean;
45
      workspace?: string;
46
      [k: string]: unknown;
47
    };
48
    [k: string]: unknown;
49
  }
50
) {
51
  const url = new URL(
52
    `https://app.asana.com/api/1.0/portfolios/${portfolio_gid}`
53
  );
54
  for (const [k, v] of [
55
    ["opt_pretty", opt_pretty],
56
    ["opt_fields", opt_fields],
57
  ]) {
58
    if (v !== undefined && v !== "") {
59
      url.searchParams.append(k, v);
60
    }
61
  }
62
  const response = await fetch(url, {
63
    method: "PUT",
64
    headers: {
65
      "Content-Type": "application/json",
66
      Authorization: "Bearer " + auth.token,
67
    },
68
    body: JSON.stringify(body),
69
  });
70
  if (!response.ok) {
71
    const text = await response.text();
72
    throw new Error(`${response.status} ${text}`);
73
  }
74
  return await response.json();
75
}
76