Add a supporting goal relationship

Creates a goal relationship by adding a supporting resource to a given goal. Returns the newly created 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
 * Add a supporting goal relationship
6
 * Creates a goal relationship by adding a supporting resource to a given goal.
7

8
Returns the newly created goal relationship record.
9
 */
10
export async function main(
11
  auth: Asana,
12
  goal_gid: string,
13
  opt_pretty: string | undefined,
14
  opt_fields: string | undefined,
15
  body: {
16
    data?: {
17
      contribution_weight?: number;
18
      insert_after?: string;
19
      insert_before?: string;
20
      supporting_resource: string;
21
      [k: string]: unknown;
22
    };
23
    [k: string]: unknown;
24
  }
25
) {
26
  const url = new URL(
27
    `https://app.asana.com/api/1.0/goals/${goal_gid}/addSupportingRelationship`
28
  );
29
  for (const [k, v] of [
30
    ["opt_pretty", opt_pretty],
31
    ["opt_fields", opt_fields],
32
  ]) {
33
    if (v !== undefined && v !== "") {
34
      url.searchParams.append(k, v);
35
    }
36
  }
37
  const response = await fetch(url, {
38
    method: "POST",
39
    headers: {
40
      "Content-Type": "application/json",
41
      Authorization: "Bearer " + auth.token,
42
    },
43
    body: JSON.stringify(body),
44
  });
45
  if (!response.ok) {
46
    const text = await response.text();
47
    throw new Error(`${response.status} ${text}`);
48
  }
49
  return await response.json();
50
}
51