Get objects via typeahead

Retrieves objects in the workspace based via an auto-completion/typeahead search algorithm.

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 objects via typeahead
6
 * Retrieves objects in the workspace based via an auto-completion/typeahead
7
search algorithm.
8
 */
9
export async function main(
10
  auth: Asana,
11
  workspace_gid: string,
12
  resource_type:
13
    | "custom_field"
14
    | "project"
15
    | "project_template"
16
    | "portfolio"
17
    | "tag"
18
    | "task"
19
    | "user"
20
    | undefined,
21
  type:
22
    | "custom_field"
23
    | "portfolio"
24
    | "project"
25
    | "tag"
26
    | "task"
27
    | "user"
28
    | undefined,
29
  query: string | undefined,
30
  count: string | undefined,
31
  opt_pretty: string | undefined,
32
  opt_fields: string | undefined
33
) {
34
  const url = new URL(
35
    `https://app.asana.com/api/1.0/workspaces/${workspace_gid}/typeahead`
36
  );
37
  for (const [k, v] of [
38
    ["resource_type", resource_type],
39
    ["type", type],
40
    ["query", query],
41
    ["count", count],
42
    ["opt_pretty", opt_pretty],
43
    ["opt_fields", opt_fields],
44
  ]) {
45
    if (v !== undefined && v !== "") {
46
      url.searchParams.append(k, v);
47
    }
48
  }
49
  const response = await fetch(url, {
50
    method: "GET",
51
    headers: {
52
      Authorization: "Bearer " + auth.token,
53
    },
54
    body: undefined,
55
  });
56
  if (!response.ok) {
57
    const text = await response.text();
58
    throw new Error(`${response.status} ${text}`);
59
  }
60
  return await response.json();
61
}
62