Add a project to a task

Adds the task to the specified project, in the optional location specified.

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 project to a task
6
 * Adds the task to the specified project, in the optional location
7
specified.
8
 */
9
export async function main(
10
  auth: Asana,
11
  task_gid: string,
12
  opt_pretty: string | undefined,
13
  opt_fields: string | undefined,
14
  body: {
15
    data?: {
16
      insert_after?: string;
17
      insert_before?: string;
18
      project: string;
19
      section?: string;
20
      [k: string]: unknown;
21
    };
22
    [k: string]: unknown;
23
  }
24
) {
25
  const url = new URL(
26
    `https://app.asana.com/api/1.0/tasks/${task_gid}/addProject`
27
  );
28
  for (const [k, v] of [
29
    ["opt_pretty", opt_pretty],
30
    ["opt_fields", opt_fields],
31
  ]) {
32
    if (v !== undefined && v !== "") {
33
      url.searchParams.append(k, v);
34
    }
35
  }
36
  const response = await fetch(url, {
37
    method: "POST",
38
    headers: {
39
      "Content-Type": "application/json",
40
      Authorization: "Bearer " + auth.token,
41
    },
42
    body: JSON.stringify(body),
43
  });
44
  if (!response.ok) {
45
    const text = await response.text();
46
    throw new Error(`${response.status} ${text}`);
47
  }
48
  return await response.json();
49
}
50