0
Establish a webhook
One script reply has been approved by the moderators Verified

Establishing a webhook is a two-part process.

Created by hugo697 191 days ago Viewed 5931 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 191 days ago
1
type Asana = {
2
  token: string;
3
};
4
/**
5
 * Establish a webhook
6
 * Establishing a webhook is a two-part process.
7
 */
8
export async function main(
9
  auth: Asana,
10
  opt_pretty: string | undefined,
11
  opt_fields: string | undefined,
12
  body: {
13
    data?: {
14
      filters?: ({
15
        action?: string;
16
        fields?: string[];
17
        resource_subtype?: string;
18
        resource_type?: string;
19
        [k: string]: unknown;
20
      } & { [k: string]: unknown } & { [k: string]: unknown })[];
21
      resource: string;
22
      target: string;
23
      [k: string]: unknown;
24
    };
25
    [k: string]: unknown;
26
  }
27
) {
28
  const url = new URL(`https://app.asana.com/api/1.0/webhooks`);
29
  for (const [k, v] of [
30
    ["opt_pretty", opt_pretty],
31
    ["opt_fields", opt_fields],
32
  ]) {
33
    if (v !== undefined && v !== "") {
34
      url.searchParams.append(k, v);
35
    }
36
  }
37
  const response = await fetch(url, {
38
    method: "POST",
39
    headers: {
40
      "Content-Type": "application/json",
41
      Authorization: "Bearer " + auth.token,
42
    },
43
    body: JSON.stringify(body),
44
  });
45
  if (!response.ok) {
46
    const text = await response.text();
47
    throw new Error(`${response.status} ${text}`);
48
  }
49
  return await response.json();
50
}
51