Edits history of script submission #23679 for ' Search Objects by Name (asana)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    export type DynSelect_workspace = string
    
    // Resolver: the workspaces and organizations the token can access.
    export async function workspace(auth: RT.Asana) {
      const response = await fetch(
        "https://app.asana.com/api/1.0/workspaces?limit=100",
        {
          headers: {
            Authorization: `Bearer ${auth.token}`,
            Accept: "application/json",
          },
        }
      )
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
      const { data } = await response.json()
      return data.map((w: { gid: string; name: string }) => ({
        value: w.gid,
        label: w.name,
      }))
    }
    
    /**
     * Search Objects by Name
     * 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.
     */
    export async function main(
      auth: RT.Asana,
      workspace: DynSelect_workspace,
      resource_type:
        | "task"
        | "project"
        | "user"
        | "tag"
        | "team"
        | "portfolio"
        | "goal"
        | "custom_field"
        | "project_template",
      query: string | undefined,
      count: number | undefined,
      opt_fields: string | undefined
    ) {
      const url = new URL(
        `https://app.asana.com/api/1.0/workspaces/${workspace}/typeahead`
      )
      const params = { resource_type, query, count, opt_fields }
      for (const [key, value] of Object.entries(params)) {
        if (value != null && value !== "") {
          url.searchParams.append(key, String(value))
        }
      }
    
      const response = await fetch(url, {
        headers: {
          Authorization: `Bearer ${auth.token}`,
          Accept: "application/json",
        },
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 3 hours ago