Set the parent of a task

parent, or no parent task at all. Returns an empty data block. When using `insert_before` and `insert_after`, at most one of those two options can be specified, and they must already be subtasks of the parent.

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
 * Set the parent of a task
6
 * parent, or no parent task at all. Returns an empty data block. When using `insert_before` and `insert_after`, at most one of those two options can be specified, and they must already be subtasks of the parent.
7
 */
8
export async function main(
9
  auth: Asana,
10
  task_gid: string,
11
  opt_pretty: string | undefined,
12
  opt_fields: string | undefined,
13
  body: {
14
    data?: {
15
      insert_after?: string;
16
      insert_before?: string;
17
      parent: string;
18
      [k: string]: unknown;
19
    };
20
    [k: string]: unknown;
21
  }
22
) {
23
  const url = new URL(
24
    `https://app.asana.com/api/1.0/tasks/${task_gid}/setParent`
25
  );
26
  for (const [k, v] of [
27
    ["opt_pretty", opt_pretty],
28
    ["opt_fields", opt_fields],
29
  ]) {
30
    if (v !== undefined && v !== "") {
31
      url.searchParams.append(k, v);
32
    }
33
  }
34
  const response = await fetch(url, {
35
    method: "POST",
36
    headers: {
37
      "Content-Type": "application/json",
38
      Authorization: "Bearer " + auth.token,
39
    },
40
    body: JSON.stringify(body),
41
  });
42
  if (!response.ok) {
43
    const text = await response.text();
44
    throw new Error(`${response.status} ${text}`);
45
  }
46
  return await response.json();
47
}
48