0

Get events on a resource

by
Published Oct 31, 2023

Returns the full record for all events that have occurred since the sync token was created.

Script asana Verified

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 403 days ago
1
type Asana = {
2
  token: string;
3
};
4
/**
5
 * Get events on a resource
6
 * Returns the full record for all events that have occurred since the sync
7
token was created.
8
 */
9
export async function main(
10
  auth: Asana,
11
  resource: string | undefined,
12
  sync: string | undefined,
13
  opt_pretty: string | undefined,
14
  opt_fields: string | undefined
15
) {
16
  const url = new URL(`https://app.asana.com/api/1.0/events`);
17
  for (const [k, v] of [
18
    ["resource", resource],
19
    ["sync", sync],
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: "GET",
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