Update a goal relationship

An existing goal relationship can be updated by making a PUT request on the URL for that goal relationship. Only the fields provided in the `data` block will be updated; any unspecified fields will remain unchanged. Returns the complete updated goal relationship 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 goal relationship
6
 * An existing goal relationship can be updated by making a PUT request on the URL for
7
that goal relationship. Only the fields provided in the `data` block will be updated;
8
any unspecified fields will remain unchanged.
9

10
Returns the complete updated goal relationship record.
11
 */
12
export async function main(
13
  auth: Asana,
14
  goal_relationship_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
      contribution_weight?: number;
20
      resource_subtype?: "subgoal" | "supporting_work";
21
      supporting_resource?: ({
22
        gid?: string;
23
        resource_type?: string;
24
        [k: string]: unknown;
25
      } & { name?: string; [k: string]: unknown }) & { [k: string]: unknown };
26
      [k: string]: unknown;
27
    }) & {
28
      supported_goal?: ({
29
        gid?: string;
30
        resource_type?: string;
31
        [k: string]: unknown;
32
      } & {
33
        name?: string;
34
        owner?: ({
35
          gid?: string;
36
          resource_type?: string;
37
          [k: string]: unknown;
38
        } & { name?: string; [k: string]: unknown }) & { [k: string]: unknown };
39
        [k: string]: unknown;
40
      }) & { [k: string]: unknown };
41
      [k: string]: unknown;
42
    }) & { [k: string]: unknown };
43
    [k: string]: unknown;
44
  }
45
) {
46
  const url = new URL(
47
    `https://app.asana.com/api/1.0/goal_relationships/${goal_relationship_gid}`
48
  );
49
  for (const [k, v] of [
50
    ["opt_pretty", opt_pretty],
51
    ["opt_fields", opt_fields],
52
  ]) {
53
    if (v !== undefined && v !== "") {
54
      url.searchParams.append(k, v);
55
    }
56
  }
57
  const response = await fetch(url, {
58
    method: "PUT",
59
    headers: {
60
      "Content-Type": "application/json",
61
      Authorization: "Bearer " + auth.token,
62
    },
63
    body: JSON.stringify(body),
64
  });
65
  if (!response.ok) {
66
    const text = await response.text();
67
    throw new Error(`${response.status} ${text}`);
68
  }
69
  return await response.json();
70
}
71