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