0

Update a Generic Document

by
Published Apr 8, 2025

Updates an existing generic document. Can only update `initiated` documents.

Script persona Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Persona = {
3
  apiKey: string;
4
};
5
/**
6
 * Update a Generic Document
7
 * Updates an existing generic 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?: { files?: unknown[] | {}[]; kind?: string; fields?: {} };
15
    };
16
  },
17
  include?: string,
18
  fields?: string,
19
  Key_Inflection?: string,
20
  Idempotency_Key?: string,
21
  Persona_Version?: string,
22
) {
23
  const url = new URL(
24
    `https://api.withpersona.com/api/v1/document/generics/${document_id}`,
25
  );
26
  for (const [k, v] of [
27
    ["include", include],
28
    ["fields", fields],
29
  ]) {
30
    if (v !== undefined && v !== "" && k !== undefined) {
31
      url.searchParams.append(k, v);
32
    }
33
  }
34
  const headers: Record<string, string> = {
35
    Authorization: `Bearer ${auth.apiKey}`,
36
    "Content-Type": "application/json",
37
  };
38
  if (Key_Inflection) {
39
    headers["Key-Inflection"] = Key_Inflection;
40
  }
41
  if (Idempotency_Key) {
42
    headers["Idempotency-Key"] = Idempotency_Key;
43
  }
44
  if (Persona_Version) {
45
    headers["Persona-Version"] = Persona_Version;
46
  }
47
  const response = await fetch(url, {
48
    method: "PATCH",
49
    headers,
50
    body: JSON.stringify(body),
51
  });
52
  if (!response.ok) {
53
    const text = await response.text();
54
    throw new Error(`${response.status} ${text}`);
55
  }
56
  return await response.json();
57
}
58