0
Duplicate a project
One script reply has been approved by the moderators Verified

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

Created by hugo697 191 days ago Viewed 5939 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 191 days ago
1
type Asana = {
2
  token: string;
3
};
4
/**
5
 * Duplicate a project
6
 * Creates and returns a job that will asynchronously handle the duplication.
7
 */
8
export async function main(
9
  auth: Asana,
10
  project_gid: string,
11
  opt_pretty: string | undefined,
12
  opt_fields: string | undefined,
13
  body: {
14
    data?: {
15
      include?:
16
        | "members"
17
        | "notes"
18
        | "forms"
19
        | "task_notes"
20
        | "task_assignee"
21
        | "task_subtasks"
22
        | "task_attachments"
23
        | "task_dates"
24
        | "task_dependencies"
25
        | "task_followers"
26
        | "task_tags"
27
        | "task_projects";
28
      name: string;
29
      schedule_dates?: {
30
        due_on?: string;
31
        should_skip_weekends: boolean;
32
        start_on?: string;
33
        [k: string]: unknown;
34
      };
35
      team?: string;
36
      [k: string]: unknown;
37
    };
38
    [k: string]: unknown;
39
  }
40
) {
41
  const url = new URL(
42
    `https://app.asana.com/api/1.0/projects/${project_gid}/duplicate`
43
  );
44
  for (const [k, v] of [
45
    ["opt_pretty", opt_pretty],
46
    ["opt_fields", opt_fields],
47
  ]) {
48
    if (v !== undefined && v !== "") {
49
      url.searchParams.append(k, v);
50
    }
51
  }
52
  const response = await fetch(url, {
53
    method: "POST",
54
    headers: {
55
      "Content-Type": "application/json",
56
      Authorization: "Bearer " + auth.token,
57
    },
58
    body: JSON.stringify(body),
59
  });
60
  if (!response.ok) {
61
    const text = await response.text();
62
    throw new Error(`${response.status} ${text}`);
63
  }
64
  return await response.json();
65
}
66