Edits history of script submission #23833 for ' List Tickets (zendesk)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * List Tickets
     * List the account's tickets, one page at a time (cursor pagination). Pass `meta.after_cursor` from the previous response as `cursor` while `meta.has_more` is true. Archived tickets are not listed; use Search Tickets to filter by status, assignee, dates...
     */
    export async function main(
      auth: RT.Zendesk,
      sort:
        | "id"
        | "-id"
        | "updated_at"
        | "-updated_at"
        | "status"
        | "-status"
        | undefined,
      page_size: number | undefined,
      cursor: string | undefined
    ) {
      const url = new URL(`https://${auth.subdomain}.zendesk.com/api/v2/tickets`)
      url.searchParams.append("page[size]", String(page_size || 100))
      if (sort) {
        url.searchParams.append("sort", sort)
      }
      if (cursor) {
        url.searchParams.append("page[after]", cursor)
      }
    
      const response = await fetch(url, {
        headers: {
          Authorization: auth.token
            ? `Bearer ${auth.token}`
            : `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
          Accept: "application/json",
        },
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 5 days ago