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