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