Get attachments from an object

Returns the compact records for all attachments on the object. There are three possible `parent` values for this request: `project`, `project_brief`, and `task`. For a project, an attachment refers to a file uploaded to the "Key resources" section in the project Overview. For a project brief, an attachment refers to inline files in the project brief itself. For a task, an attachment refers to a file directly associated to that task.

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
 * Get attachments from an object
6
 * Returns the compact records for all attachments on the object.
7

8
There are three possible `parent` values for this request: `project`, `project_brief`, and `task`. For a project, an attachment refers to a file uploaded to the "Key resources" section in the project Overview. For a project brief, an attachment refers to inline files in the project brief itself. For a task, an attachment refers to a file directly associated to that task.
9
 */
10
export async function main(
11
  auth: Asana,
12
  opt_pretty: string | undefined,
13
  opt_fields: string | undefined,
14
  limit: string | undefined,
15
  offset: string | undefined,
16
  parent: string | undefined
17
) {
18
  const url = new URL(`https://app.asana.com/api/1.0/attachments`);
19
  for (const [k, v] of [
20
    ["opt_pretty", opt_pretty],
21
    ["opt_fields", opt_fields],
22
    ["limit", limit],
23
    ["offset", offset],
24
    ["parent", parent],
25
  ]) {
26
    if (v !== undefined && v !== "") {
27
      url.searchParams.append(k, v);
28
    }
29
  }
30
  const response = await fetch(url, {
31
    method: "GET",
32
    headers: {
33
      Authorization: "Bearer " + auth.token,
34
    },
35
    body: undefined,
36
  });
37
  if (!response.ok) {
38
    const text = await response.text();
39
    throw new Error(`${response.status} ${text}`);
40
  }
41
  return await response.json();
42
}
43