Duplicate a task

Creates and returns a job that will asynchronously handle the duplication.

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
 * Duplicate a task
6
 * Creates and returns a job that will asynchronously handle the duplication.
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
      include?:
16
        | "notes"
17
        | "assignee"
18
        | "subtasks"
19
        | "attachments"
20
        | "tags"
21
        | "followers"
22
        | "projects"
23
        | "dates"
24
        | "dependencies"
25
        | "parent";
26
      name?: string;
27
      [k: string]: unknown;
28
    };
29
    [k: string]: unknown;
30
  }
31
) {
32
  const url = new URL(
33
    `https://app.asana.com/api/1.0/tasks/${task_gid}/duplicate`
34
  );
35
  for (const [k, v] of [
36
    ["opt_pretty", opt_pretty],
37
    ["opt_fields", opt_fields],
38
  ]) {
39
    if (v !== undefined && v !== "") {
40
      url.searchParams.append(k, v);
41
    }
42
  }
43
  const response = await fetch(url, {
44
    method: "POST",
45
    headers: {
46
      "Content-Type": "application/json",
47
      Authorization: "Bearer " + auth.token,
48
    },
49
    body: JSON.stringify(body),
50
  });
51
  if (!response.ok) {
52
    const text = await response.text();
53
    throw new Error(`${response.status} ${text}`);
54
  }
55
  return await response.json();
56
}
57