Edits history of script submission #23846 for ' List Articles (zendesk)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * List Articles
     * List Help Center articles, all of them or those in one category, one section, or by one author (`user_id`, which cannot be combined with a category or section). Sorting by `title` needs a `locale`. Cursor-paged: pass `meta.after_cursor` as `cursor` while `meta.has_more` is true.
     */
    export async function main(
      auth: RT.Zendesk,
      locale: string | undefined,
      category_id: number | undefined,
      section_id: number | undefined,
      user_id: number | undefined,
      sort_by: "position" | "title" | "created_at" | "updated_at" | undefined,
      sort_order: "asc" | "desc" | undefined,
      page_size: number | undefined,
      cursor: string | undefined
    ) {
      if (user_id != null && (category_id != null || section_id != null)) {
        throw new Error("user_id cannot be combined with category_id or section_id")
      }
      let scope = ""
      if (category_id != null) scope = `/categories/${category_id}`
      if (section_id != null) scope = `/sections/${section_id}`
      if (user_id != null) scope = `/users/${user_id}`
      const localePath = locale && user_id == null ? `/${locale.toLowerCase()}` : ""
    
      const url = new URL(
        `https://${auth.subdomain}.zendesk.com/api/v2/help_center${localePath}${scope}/articles`
      )
      url.searchParams.append("page[size]", String(page_size || 100))
      if (sort_by) {
        url.searchParams.append("sort_by", sort_by)
      }
      if (sort_order) {
        url.searchParams.append("sort_order", sort_order)
      }
      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 2 hours ago