0
Instantiate a project from a project template
One script reply has been approved by the moderators Verified

Creates and returns a job that will asynchronously handle the project instantiation.

Created by hugo697 353 days ago Viewed 8996 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 353 days ago
1
type Asana = {
2
  token: string;
3
};
4
/**
5
 * Instantiate a project from a project template
6
 * Creates and returns a job that will asynchronously handle the project instantiation.
7
 */
8
export async function main(
9
  auth: Asana,
10
  project_template_gid: string,
11
  opt_pretty: string | undefined,
12
  opt_fields: string | undefined,
13
  body: {
14
    data?: {
15
      is_strict?: boolean;
16
      name: string;
17
      public: boolean;
18
      requested_dates?: {
19
        gid?: string;
20
        value?: string;
21
        [k: string]: unknown;
22
      }[];
23
      team?: string;
24
      workspace?: string;
25
      [k: string]: unknown;
26
    };
27
    [k: string]: unknown;
28
  }
29
) {
30
  const url = new URL(
31
    `https://app.asana.com/api/1.0/project_templates/${project_template_gid}/instantiateProject`
32
  );
33
  for (const [k, v] of [
34
    ["opt_pretty", opt_pretty],
35
    ["opt_fields", opt_fields],
36
  ]) {
37
    if (v !== undefined && v !== "") {
38
      url.searchParams.append(k, v);
39
    }
40
  }
41
  const response = await fetch(url, {
42
    method: "POST",
43
    headers: {
44
      "Content-Type": "application/json",
45
      Authorization: "Bearer " + auth.token,
46
    },
47
    body: JSON.stringify(body),
48
  });
49
  if (!response.ok) {
50
    const text = await response.text();
51
    throw new Error(`${response.status} ${text}`);
52
  }
53
  return await response.json();
54
}
55