1 | |
2 |
|
3 | |
4 | * Search Help Center |
5 | * 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. |
6 | */ |
7 | export async function main( |
8 | auth: RT.Zendesk, |
9 | locales: string[], |
10 | query: string | undefined, |
11 | content_types: ("ARTICLE" | "POST")[] | undefined, |
12 | brand_ids: number[] | undefined, |
13 | category_ids: number[] | undefined, |
14 | section_ids: number[] | undefined, |
15 | topic_ids: number[] | undefined, |
16 | external_source_ids: string[] | undefined, |
17 | page_size: number | undefined, |
18 | cursor: string | undefined |
19 | ) { |
20 | const url = new URL( |
21 | `https://${auth.subdomain}.zendesk.com/api/v2/guide/search` |
22 | ) |
23 | const params = { |
24 | query, |
25 | "filter[locales]": locales.join(","), |
26 | "filter[content_types]": content_types?.join(","), |
27 | "filter[brand_ids]": brand_ids?.join(","), |
28 | "filter[category_ids]": category_ids?.join(","), |
29 | "filter[section_ids]": section_ids?.join(","), |
30 | "filter[topic_ids]": topic_ids?.join(","), |
31 | "filter[external_source_ids]": external_source_ids?.join(","), |
32 | "page[size]": page_size || 10, |
33 | "page[after]": cursor, |
34 | } |
35 | for (const [key, value] of Object.entries(params)) { |
36 | if (value != null && value !== "") { |
37 | url.searchParams.append(key, String(value)) |
38 | } |
39 | } |
40 |
|
41 | const response = await fetch(url, { |
42 | headers: { |
43 | Authorization: auth.token |
44 | ? `Bearer ${auth.token}` |
45 | : `Basic ${btoa(`${auth.username}:${auth.password}`)}`, |
46 | Accept: "application/json", |
47 | }, |
48 | }) |
49 |
|
50 | if (!response.ok) { |
51 | throw new Error(`${response.status} ${await response.text()}`) |
52 | } |
53 |
|
54 | return await response.json() |
55 | } |
56 |
|