0
Add task to section
One script reply has been approved by the moderators Verified

Add a task to a specific, existing section. This will remove the task from other sections of the project.

The task will be inserted at the top of a section unless an insert_before or insert_after parameter is declared.

This does not work for separators (tasks with the resource_subtype of section).

Created by hugo697 192 days ago Viewed 5963 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 192 days ago
1
type Asana = {
2
  token: string;
3
};
4
/**
5
 * Add task to section
6
 * Add a task to a specific, existing section. This will remove the task from other sections of the project.
7

8
The task will be inserted at the top of a section unless an insert_before or insert_after parameter is declared.
9

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