0
Delete an attachment
One script reply has been approved by the moderators Verified

Deletes a specific, existing attachment.

Returns an empty data record.

Created by hugo697 202 days ago Viewed 6888 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 202 days ago
1
type Asana = {
2
  token: string;
3
};
4
/**
5
 * Delete an attachment
6
 * Deletes a specific, existing attachment.
7

8
Returns an empty data record.
9
 */
10
export async function main(
11
  auth: Asana,
12
  attachment_gid: string,
13
  opt_pretty: string | undefined,
14
  opt_fields: string | undefined
15
) {
16
  const url = new URL(
17
    `https://app.asana.com/api/1.0/attachments/${attachment_gid}`
18
  );
19
  for (const [k, v] of [
20
    ["opt_pretty", opt_pretty],
21
    ["opt_fields", opt_fields],
22
  ]) {
23
    if (v !== undefined && v !== "") {
24
      url.searchParams.append(k, v);
25
    }
26
  }
27
  const response = await fetch(url, {
28
    method: "DELETE",
29
    headers: {
30
      Authorization: "Bearer " + auth.token,
31
    },
32
    body: undefined,
33
  });
34
  if (!response.ok) {
35
    const text = await response.text();
36
    throw new Error(`${response.status} ${text}`);
37
  }
38
  return await response.json();
39
}
40