Edits history of script submission #23688 for ' List Task Stories (asana)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * List Task Stories
     * List a task's stories: its comments plus the activity log (assignments, completions, field changes). Set `comments_only` to keep only comments; the filter applies to the returned page. Pass `next_page.offset` from a response as `offset` to get the next page.
     */
    export async function main(
      auth: RT.Asana,
      task_gid: string,
      comments_only: boolean | undefined,
      opt_fields: string | undefined,
      limit: number | undefined,
      offset: string | undefined
    ) {
      const url = new URL(`https://app.asana.com/api/1.0/tasks/${task_gid}/stories`)
      url.searchParams.append("limit", String(limit ?? 100))
      if (offset != null && offset !== "") {
        url.searchParams.append("offset", offset)
      }
      if (opt_fields != null && opt_fields !== "") {
        // the comments_only filter reads resource_subtype, so keep it in the response
        url.searchParams.append("opt_fields", `${opt_fields},resource_subtype`)
      }
    
      const response = await fetch(url, {
        headers: {
          Authorization: `Bearer ${auth.token}`,
          Accept: "application/json",
        },
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      const result = await response.json()
      if (comments_only) {
        result.data = result.data.filter(
          (story: { resource_subtype: string }) =>
            story.resource_subtype === "comment_added"
        )
      }
      return result
    }
    

    Submitted by hugo989 3 hours ago