0
Update a task
One script reply has been approved by the moderators Verified

A specific, existing task can be updated by making a PUT request on the URL for that task. Only the fields provided in the data block will be updated; any unspecified fields will remain unchanged.

When using this method, it is best to specify only those fields you wish to change, or else you may overwrite changes made by another user since you last retrieved the task.

Returns the complete updated task record.

Created by hugo697 192 days ago Viewed 5982 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 192 days ago
1
type Asana = {
2
  token: string;
3
};
4
/**
5
 * Update a task
6
 * A specific, existing task can be updated by making a PUT request on the
7
URL for that task. Only the fields provided in the `data` block will be
8
updated; any unspecified fields will remain unchanged.
9

10
When using this method, it is best to specify only those fields you wish
11
to change, or else you may overwrite changes made by another user since
12
you last retrieved the task.
13

14
Returns the complete updated task record.
15
 */
16
export async function main(
17
  auth: Asana,
18
  task_gid: string,
19
  opt_pretty: string | undefined,
20
  opt_fields: string | undefined,
21
  body: {
22
    data?: (({ gid?: string; resource_type?: string; [k: string]: unknown } & {
23
      name?: string;
24
      resource_subtype?: "default_task" | "milestone" | "section" | "approval";
25
      [k: string]: unknown;
26
    }) & {
27
      actual_time_minutes?: number;
28
      approval_status?:
29
        | "pending"
30
        | "approved"
31
        | "rejected"
32
        | "changes_requested";
33
      assignee_status?: "today" | "upcoming" | "later" | "new" | "inbox";
34
      completed?: boolean;
35
      completed_at?: string;
36
      completed_by?: {
37
        gid?: string;
38
        resource_type?: string;
39
        [k: string]: unknown;
40
      } & { name?: string; [k: string]: unknown };
41
      created_at?: string;
42
      dependencies?: {
43
        gid?: string;
44
        resource_type?: string;
45
        [k: string]: unknown;
46
      }[];
47
      dependents?: {
48
        gid?: string;
49
        resource_type?: string;
50
        [k: string]: unknown;
51
      }[];
52
      due_at?: string;
53
      due_on?: string;
54
      external?: { data?: string; gid?: string; [k: string]: unknown };
55
      hearted?: boolean;
56
      hearts?: {
57
        gid?: string;
58
        user?: {
59
          gid?: string;
60
          resource_type?: string;
61
          [k: string]: unknown;
62
        } & { name?: string; [k: string]: unknown };
63
        [k: string]: unknown;
64
      }[];
65
      html_notes?: string;
66
      is_rendered_as_separator?: boolean;
67
      liked?: boolean;
68
      likes?: {
69
        gid?: string;
70
        user?: {
71
          gid?: string;
72
          resource_type?: string;
73
          [k: string]: unknown;
74
        } & { name?: string; [k: string]: unknown };
75
        [k: string]: unknown;
76
      }[];
77
      memberships?: {
78
        project?: {
79
          gid?: string;
80
          resource_type?: string;
81
          [k: string]: unknown;
82
        } & { name?: string; [k: string]: unknown };
83
        section?: {
84
          gid?: string;
85
          resource_type?: string;
86
          [k: string]: unknown;
87
        } & { name?: string; [k: string]: unknown };
88
        [k: string]: unknown;
89
      }[];
90
      modified_at?: string;
91
      name?: string;
92
      notes?: string;
93
      num_hearts?: number;
94
      num_likes?: number;
95
      num_subtasks?: number;
96
      start_at?: string;
97
      start_on?: string;
98
      [k: string]: unknown;
99
    }) & {
100
      assignee?: string;
101
      assignee_section?: string;
102
      custom_fields?: { [k: string]: string };
103
      followers?: string[];
104
      parent?: string;
105
      projects?: string[];
106
      tags?: string[];
107
      workspace?: string;
108
      [k: string]: unknown;
109
    };
110
    [k: string]: unknown;
111
  }
112
) {
113
  const url = new URL(`https://app.asana.com/api/1.0/tasks/${task_gid}`);
114
  for (const [k, v] of [
115
    ["opt_pretty", opt_pretty],
116
    ["opt_fields", opt_fields],
117
  ]) {
118
    if (v !== undefined && v !== "") {
119
      url.searchParams.append(k, v);
120
    }
121
  }
122
  const response = await fetch(url, {
123
    method: "PUT",
124
    headers: {
125
      "Content-Type": "application/json",
126
      Authorization: "Bearer " + auth.token,
127
    },
128
    body: JSON.stringify(body),
129
  });
130
  if (!response.ok) {
131
    const text = await response.text();
132
    throw new Error(`${response.status} ${text}`);
133
  }
134
  return await response.json();
135
}
136