Edits history of script submission #23851 for ' Search Help Center (zendesk)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Search Help Center
     * Unified Help Center search across articles, community posts and external content, in one or more locales (e.g. `["en-us"]`). Filters take ids; `content_types` takes `ARTICLE` and/or `POST`. Cursor-paged: pass `meta.after_cursor` as `cursor` while `meta.has_more` is true.
     */
    export async function main(
      auth: RT.Zendesk,
      locales: string[],
      query: string | undefined,
      content_types: ("ARTICLE" | "POST")[] | undefined,
      brand_ids: number[] | undefined,
      category_ids: number[] | undefined,
      section_ids: number[] | undefined,
      topic_ids: number[] | undefined,
      external_source_ids: string[] | undefined,
      page_size: number | undefined,
      cursor: string | undefined
    ) {
      const url = new URL(
        `https://${auth.subdomain}.zendesk.com/api/v2/guide/search`
      )
      const params = {
        query,
        "filter[locales]": locales.join(","),
        "filter[content_types]": content_types?.join(","),
        "filter[brand_ids]": brand_ids?.join(","),
        "filter[category_ids]": category_ids?.join(","),
        "filter[section_ids]": section_ids?.join(","),
        "filter[topic_ids]": topic_ids?.join(","),
        "filter[external_source_ids]": external_source_ids?.join(","),
        "page[size]": page_size || 10,
        "page[after]": cursor,
      }
      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: 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