Update a webhook

An existing webhook's filters can be updated by making a PUT request on the URL for that webhook. Note that the webhook's previous `filters` array will be completely overwritten by the `filters` sent in the PUT request.

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