0

Search Objects by Name

by
Published Oct 31, 2023

Find tasks, projects, users, tags and other objects in a workspace by name, the way Asana's quick search does. An empty query returns recently used objects.

Script asana Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 2 hours ago
1
//native
2

3
export type DynSelect_workspace = string
4

5
// Resolver: the workspaces and organizations the token can access.
6
export async function workspace(auth: RT.Asana) {
7
  const response = await fetch(
8
    "https://app.asana.com/api/1.0/workspaces?limit=100",
9
    {
10
      headers: {
11
        Authorization: `Bearer ${auth.token}`,
12
        Accept: "application/json",
13
      },
14
    }
15
  )
16
  if (!response.ok) {
17
    throw new Error(`${response.status} ${await response.text()}`)
18
  }
19
  const { data } = await response.json()
20
  return data.map((w: { gid: string; name: string }) => ({
21
    value: w.gid,
22
    label: w.name,
23
  }))
24
}
25

26
/**
27
 * Search Objects by Name
28
 * Find tasks, projects, users, tags and other objects in a workspace by name, the way Asana's quick search does. An empty query returns recently used objects.
29
 */
30
export async function main(
31
  auth: RT.Asana,
32
  workspace: DynSelect_workspace,
33
  resource_type:
34
    | "task"
35
    | "project"
36
    | "user"
37
    | "tag"
38
    | "team"
39
    | "portfolio"
40
    | "goal"
41
    | "custom_field"
42
    | "project_template",
43
  query: string | undefined,
44
  count: number | undefined,
45
  opt_fields: string | undefined
46
) {
47
  const url = new URL(
48
    `https://app.asana.com/api/1.0/workspaces/${workspace}/typeahead`
49
  )
50
  const params = { resource_type, query, count, opt_fields }
51
  for (const [key, value] of Object.entries(params)) {
52
    if (value != null && value !== "") {
53
      url.searchParams.append(key, String(value))
54
    }
55
  }
56

57
  const response = await fetch(url, {
58
    headers: {
59
      Authorization: `Bearer ${auth.token}`,
60
      Accept: "application/json",
61
    },
62
  })
63

64
  if (!response.ok) {
65
    throw new Error(`${response.status} ${await response.text()}`)
66
  }
67

68
  return await response.json()
69
}
70

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 528 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