Update a section

A specific, existing section can be updated by making a PUT request on the URL for that project.

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
 * Update a section
6
 * A specific, existing section can be updated by making a PUT request on
7
the URL for that project.
8
 */
9
export async function main(
10
  auth: Asana,
11
  section_gid: string,
12
  opt_pretty: string | undefined,
13
  opt_fields: string | undefined,
14
  body: {
15
    data?: {
16
      insert_after?: string;
17
      insert_before?: string;
18
      name: string;
19
      [k: string]: unknown;
20
    };
21
    [k: string]: unknown;
22
  }
23
) {
24
  const url = new URL(`https://app.asana.com/api/1.0/sections/${section_gid}`);
25
  for (const [k, v] of [
26
    ["opt_pretty", opt_pretty],
27
    ["opt_fields", opt_fields],
28
  ]) {
29
    if (v !== undefined && v !== "") {
30
      url.searchParams.append(k, v);
31
    }
32
  }
33
  const response = await fetch(url, {
34
    method: "PUT",
35
    headers: {
36
      "Content-Type": "application/json",
37
      Authorization: "Bearer " + auth.token,
38
    },
39
    body: JSON.stringify(body),
40
  });
41
  if (!response.ok) {
42
    const text = await response.text();
43
    throw new Error(`${response.status} ${text}`);
44
  }
45
  return await response.json();
46
}
47