1 | |
2 | type Persona = { |
3 | apiKey: string; |
4 | }; |
5 | |
6 | * Create Access Token |
7 | * Creates an access token using an authorization code. |
8 | */ |
9 | export async function main( |
10 | auth: Persona, |
11 | body: { code: string; "grant-type": string }, |
12 | Key_Inflection?: string, |
13 | Idempotency_Key?: string, |
14 | Persona_Version?: string, |
15 | ) { |
16 | const url = new URL(`https://api.withpersona.com/api/v1/oauth/token`); |
17 |
|
18 | const headers: Record<string, string> = { |
19 | Authorization: `Bearer ${auth.apiKey}`, |
20 | "Content-Type": "application/x-www-form-urlencoded", |
21 | }; |
22 | if (Key_Inflection) { |
23 | headers["Key-Inflection"] = Key_Inflection; |
24 | } |
25 | if (Idempotency_Key) { |
26 | headers["Idempotency-Key"] = Idempotency_Key; |
27 | } |
28 | if (Persona_Version) { |
29 | headers["Persona-Version"] = Persona_Version; |
30 | } |
31 |
|
32 | const response = await fetch(url, { |
33 | method: "POST", |
34 | headers, |
35 | body: new URLSearchParams(body as Record<string, string>), |
36 | }); |
37 | if (!response.ok) { |
38 | const text = await response.text(); |
39 | throw new Error(`${response.status} ${text}`); |
40 | } |
41 | return await response.json(); |
42 | } |
43 |
|