Get a user's favorites

Returns all of a user's favorites in the given workspace, of the given type. Results are given in order (The same order as Asana's sidebar).

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
 * Get a user's favorites
6
 * Returns all of a user's favorites in the given workspace, of the given type.
7
Results are given in order (The same order as Asana's sidebar).
8
 */
9
export async function main(
10
  auth: Asana,
11
  user_gid: string,
12
  opt_pretty: string | undefined,
13
  opt_fields: string | undefined,
14
  resource_type:
15
    | "portfolio"
16
    | "project"
17
    | "tag"
18
    | "task"
19
    | "user"
20
    | "project_template"
21
    | undefined,
22
  workspace: string | undefined
23
) {
24
  const url = new URL(
25
    `https://app.asana.com/api/1.0/users/${user_gid}/favorites`
26
  );
27
  for (const [k, v] of [
28
    ["opt_pretty", opt_pretty],
29
    ["opt_fields", opt_fields],
30
    ["resource_type", resource_type],
31
    ["workspace", workspace],
32
  ]) {
33
    if (v !== undefined && v !== "") {
34
      url.searchParams.append(k, v);
35
    }
36
  }
37
  const response = await fetch(url, {
38
    method: "GET",
39
    headers: {
40
      Authorization: "Bearer " + auth.token,
41
    },
42
    body: undefined,
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