1 | |
2 | type Persona = { |
3 | apiKey: string; |
4 | }; |
5 | |
6 | * Create a Generic Document |
7 | * Creates a new generic document. |
8 | */ |
9 | export async function main( |
10 | auth: Persona, |
11 | body: { |
12 | data: { |
13 | attributes?: { |
14 | files?: { data: string; filename: string }[]; |
15 | "inquiry-id"?: string; |
16 | "account-id"?: string; |
17 | kind: string; |
18 | }; |
19 | }; |
20 | }, |
21 | include?: string, |
22 | fields?: string, |
23 | Key_Inflection?: string, |
24 | Idempotency_Key?: string, |
25 | Persona_Version?: string, |
26 | ) { |
27 | const url = new URL(`https://api.withpersona.com/api/v1/document/generics`); |
28 | for (const [k, v] of [ |
29 | ["include", include], |
30 | ["fields", fields], |
31 | ]) { |
32 | if (v !== undefined && v !== "" && k !== undefined) { |
33 | url.searchParams.append(k, v); |
34 | } |
35 | } |
36 | const headers: Record<string, string> = { |
37 | Authorization: `Bearer ${auth.apiKey}`, |
38 | "Content-Type": "application/json", |
39 | }; |
40 | if (Key_Inflection) { |
41 | headers["Key-Inflection"] = Key_Inflection; |
42 | } |
43 | if (Idempotency_Key) { |
44 | headers["Idempotency-Key"] = Idempotency_Key; |
45 | } |
46 | if (Persona_Version) { |
47 | headers["Persona-Version"] = Persona_Version; |
48 | } |
49 | const response = await fetch(url, { |
50 | method: "POST", |
51 | headers, |
52 | body: JSON.stringify(body), |
53 | }); |
54 | if (!response.ok) { |
55 | const text = await response.text(); |
56 | throw new Error(`${response.status} ${text}`); |
57 | } |
58 | return await response.json(); |
59 | } |
60 |
|