1 | |
2 | type Persona = { |
3 | apiKey: string; |
4 | }; |
5 | |
6 | * Update a Government ID Document |
7 | * Updates an existing government ID document. Can only update `initiated` documents. |
8 | */ |
9 | export async function main( |
10 | auth: Persona, |
11 | document_id: string, |
12 | body: { |
13 | data: { |
14 | attributes?: { |
15 | "back-photo"?: { data?: { data: string; filename: string }[] }; |
16 | "front-photo"?: { data?: { data: string; filename: string }[] }; |
17 | "selected-country-code"?: string; |
18 | "selected-id-class"?: string | string[]; |
19 | }; |
20 | }; |
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( |
29 | `https://api.withpersona.com/api/v1/document/government-ids/${document_id}`, |
30 | ); |
31 | for (const [k, v] of [ |
32 | ["include", include], |
33 | ["fields", fields], |
34 | ]) { |
35 | if (v !== undefined && v !== "" && k !== undefined) { |
36 | url.searchParams.append(k, v); |
37 | } |
38 | } |
39 | const headers: Record<string, string> = { |
40 | Authorization: `Bearer ${auth.apiKey}`, |
41 | "Content-Type": "application/json", |
42 | }; |
43 | if (Key_Inflection) { |
44 | headers["Key-Inflection"] = Key_Inflection; |
45 | } |
46 | if (Idempotency_Key) { |
47 | headers["Idempotency-Key"] = Idempotency_Key; |
48 | } |
49 | if (Persona_Version) { |
50 | headers["Persona-Version"] = Persona_Version; |
51 | } |
52 | const response = await fetch(url, { |
53 | method: "PATCH", |
54 | headers, |
55 | body: JSON.stringify(body), |
56 | }); |
57 | if (!response.ok) { |
58 | const text = await response.text(); |
59 | throw new Error(`${response.status} ${text}`); |
60 | } |
61 | return await response.json(); |
62 | } |
63 |
|