Edits history of script submission #23692 for ' List My Tasks (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,
      }))
    }
    
    /**
     * List My Tasks
     * List the tasks in a user's My Tasks list in a workspace (`me` by default, other users only in organizations you share). Set `completed_since` to `now` to get only incomplete tasks. Pass `next_page.offset` from a response as `offset` to get the next page.
     */
    export async function main(
      auth: RT.Asana,
      workspace: DynSelect_workspace,
      user: string | undefined,
      completed_since: string | undefined,
      opt_fields: string | undefined,
      limit: number | undefined,
      offset: string | undefined
    ) {
      const headers = {
        Authorization: `Bearer ${auth.token}`,
        Accept: "application/json",
      }
    
      const listUrl = new URL(
        `https://app.asana.com/api/1.0/users/${user || "me"}/user_task_list`
      )
      listUrl.searchParams.append("workspace", workspace)
      const listResponse = await fetch(listUrl, { headers })
      if (!listResponse.ok) {
        throw new Error(`${listResponse.status} ${await listResponse.text()}`)
      }
      const { data: taskList } = await listResponse.json()
    
      const url = new URL(
        `https://app.asana.com/api/1.0/user_task_lists/${taskList.gid}/tasks`
      )
      const params = { completed_since, opt_fields, limit: limit ?? 100, offset }
      for (const [key, value] of Object.entries(params)) {
        if (value != null && value !== "") {
          url.searchParams.append(key, String(value))
        }
      }
    
      const response = await fetch(url, { headers })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 3 hours ago