1 | |
2 |
|
3 | |
4 | * List Articles |
5 | * 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. |
6 | */ |
7 | export async function main( |
8 | auth: RT.Zendesk, |
9 | locale: string | undefined, |
10 | category_id: number | undefined, |
11 | section_id: number | undefined, |
12 | user_id: number | undefined, |
13 | sort_by: "position" | "title" | "created_at" | "updated_at" | undefined, |
14 | sort_order: "asc" | "desc" | undefined, |
15 | page_size: number | undefined, |
16 | cursor: string | undefined |
17 | ) { |
18 | if (user_id != null && (category_id != null || section_id != null)) { |
19 | throw new Error("user_id cannot be combined with category_id or section_id") |
20 | } |
21 | let scope = "" |
22 | if (category_id != null) scope = `/categories/${category_id}` |
23 | if (section_id != null) scope = `/sections/${section_id}` |
24 | if (user_id != null) scope = `/users/${user_id}` |
25 | const localePath = locale && user_id == null ? `/${locale.toLowerCase()}` : "" |
26 |
|
27 | const url = new URL( |
28 | `https://${auth.subdomain}.zendesk.com/api/v2/help_center${localePath}${scope}/articles` |
29 | ) |
30 | url.searchParams.append("page[size]", String(page_size || 100)) |
31 | if (sort_by) { |
32 | url.searchParams.append("sort_by", sort_by) |
33 | } |
34 | if (sort_order) { |
35 | url.searchParams.append("sort_order", sort_order) |
36 | } |
37 | if (cursor) { |
38 | url.searchParams.append("page[after]", cursor) |
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 |
|