1 | |
2 |
|
3 | export type DynSelect_view_id = string |
4 |
|
5 | |
6 | export async function view_id(auth: RT.Zendesk) { |
7 | const response = await fetch( |
8 | `https://${auth.subdomain}.zendesk.com/api/v2/views?active=true&page[size]=100`, |
9 | { |
10 | headers: { |
11 | Authorization: auth.token |
12 | ? `Bearer ${auth.token}` |
13 | : `Basic ${btoa(`${auth.username}:${auth.password}`)}`, |
14 | Accept: "application/json", |
15 | }, |
16 | } |
17 | ) |
18 | if (!response.ok) { |
19 | throw new Error(`${response.status} ${await response.text()}`) |
20 | } |
21 | const { views } = await response.json() |
22 | return views.map((v: { id: number; title: string }) => ({ |
23 | value: String(v.id), |
24 | label: v.title, |
25 | })) |
26 | } |
27 |
|
28 | |
29 | * List View Tickets |
30 | * List the tickets in a view, in the view's own order. Cursor-paged: pass `meta.after_cursor` as `cursor` while `meta.has_more` is true. |
31 | */ |
32 | export async function main( |
33 | auth: RT.Zendesk, |
34 | view_id: DynSelect_view_id, |
35 | page_size: number | undefined, |
36 | cursor: string | undefined |
37 | ) { |
38 | const url = new URL( |
39 | `https://${auth.subdomain}.zendesk.com/api/v2/views/${view_id}/tickets` |
40 | ) |
41 | url.searchParams.append("page[size]", String(page_size || 100)) |
42 | if (cursor) { |
43 | url.searchParams.append("page[after]", cursor) |
44 | } |
45 |
|
46 | const response = await fetch(url, { |
47 | headers: { |
48 | Authorization: auth.token |
49 | ? `Bearer ${auth.token}` |
50 | : `Basic ${btoa(`${auth.username}:${auth.password}`)}`, |
51 | Accept: "application/json", |
52 | }, |
53 | }) |
54 |
|
55 | if (!response.ok) { |
56 | throw new Error(`${response.status} ${await response.text()}`) |
57 | } |
58 |
|
59 | return await response.json() |
60 | } |
61 |
|