1 | |
2 | type Persona = { |
3 | apiKey: string; |
4 | }; |
5 | |
6 | * List all Cases |
7 | * Returns a list of your organization's cases. |
8 |
|
9 | Note that this endpoint aggregates cases across all >(s). See [Pagination](https://docs.withpersona.com/reference/pagination)for more details about handling the response. |
10 | */ |
11 | export async function main( |
12 | auth: Persona, |
13 | page: any, |
14 | fields: string | undefined, |
15 | filter: any, |
16 | Key_Inflection?: string, |
17 | Idempotency_Key?: string, |
18 | Persona_Version?: string, |
19 | ) { |
20 | const url = new URL(`https://api.withpersona.com/api/v1/cases`); |
21 | for (const [k, v] of [["fields", fields]]) { |
22 | if (v !== undefined && v !== "" && k !== undefined) { |
23 | url.searchParams.append(k, v); |
24 | } |
25 | } |
26 | encodeParams({ page, filter }).forEach((v, k) => { |
27 | if (v !== undefined && v !== "") { |
28 | url.searchParams.append(k, v); |
29 | } |
30 | }); |
31 | const headers: Record<string, string> = { |
32 | Authorization: `Bearer ${auth.apiKey}`, |
33 | }; |
34 | if (Key_Inflection) { |
35 | headers["Key-Inflection"] = Key_Inflection; |
36 | } |
37 | if (Idempotency_Key) { |
38 | headers["Idempotency-Key"] = Idempotency_Key; |
39 | } |
40 | if (Persona_Version) { |
41 | headers["Persona-Version"] = Persona_Version; |
42 | } |
43 | const response = await fetch(url, { |
44 | method: "GET", |
45 | headers, |
46 | body: undefined, |
47 | }); |
48 | if (!response.ok) { |
49 | const text = await response.text(); |
50 | throw new Error(`${response.status} ${text}`); |
51 | } |
52 | return await response.json(); |
53 | } |
54 |
|
55 | function encodeParams(o: any) { |
56 | function iter(o: any, path: string) { |
57 | if (Array.isArray(o)) { |
58 | o.forEach(function (a) { |
59 | iter(a, path + "[]"); |
60 | }); |
61 | return; |
62 | } |
63 | if (o !== null && typeof o === "object") { |
64 | Object.keys(o).forEach(function (k) { |
65 | iter(o[k], path + "[" + k + "]"); |
66 | }); |
67 | return; |
68 | } |
69 | data.push(path + "=" + o); |
70 | } |
71 | const data: string[] = []; |
72 | Object.keys(o).forEach(function (k) { |
73 | if (o[k] !== undefined) { |
74 | iter(o[k], k); |
75 | } |
76 | }); |
77 | return new URLSearchParams(data.join("&")); |
78 | } |
79 |
|