0
Get tasks from a project
One script reply has been approved by the moderators Verified

Returns the compact task records for all tasks within the given project, ordered by their priority within the project. Tasks can exist in more than one project at a time.

Created by hugo697 192 days ago Viewed 5975 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 192 days ago
1
type Asana = {
2
  token: string;
3
};
4
/**
5
 * Get tasks from a project
6
 * Returns the compact task records for all tasks within the given project, ordered by their priority within the project. Tasks can exist in more than one project at a time.
7
 */
8
export async function main(
9
  auth: Asana,
10
  project_gid: string,
11
  completed_since: string | undefined,
12
  opt_pretty: string | undefined,
13
  opt_fields: string | undefined,
14
  limit: string | undefined,
15
  offset: string | undefined
16
) {
17
  const url = new URL(
18
    `https://app.asana.com/api/1.0/projects/${project_gid}/tasks`
19
  );
20
  for (const [k, v] of [
21
    ["completed_since", completed_since],
22
    ["opt_pretty", opt_pretty],
23
    ["opt_fields", opt_fields],
24
    ["limit", limit],
25
    ["offset", offset],
26
  ]) {
27
    if (v !== undefined && v !== "") {
28
      url.searchParams.append(k, v);
29
    }
30
  }
31
  const response = await fetch(url, {
32
    method: "GET",
33
    headers: {
34
      Authorization: "Bearer " + auth.token,
35
    },
36
    body: undefined,
37
  });
38
  if (!response.ok) {
39
    const text = await response.text();
40
    throw new Error(`${response.status} ${text}`);
41
  }
42
  return await response.json();
43
}
44