1 | |
2 |
|
3 | |
4 | * Update Page |
5 | * Update a page's title, body, or status. The current version is fetched automatically and incremented; omitted fields keep their existing values. |
6 | */ |
7 | export async function main( |
8 | auth: RT.Confluence, |
9 | page_id: string, |
10 | title: string | undefined, |
11 | body: string | undefined, |
12 | status: "current" | "draft" | "archived" | undefined, |
13 | representation: "storage" | "atlas_doc_format" | undefined, |
14 | version_message: string | undefined |
15 | ) { |
16 | const base = auth.baseUrl.replace(/\/$/, "") |
17 | const repr = representation ?? "storage" |
18 | const authHeader = "Basic " + btoa(`${auth.email}:${auth.apiToken}`) |
19 |
|
20 | |
21 | const currentRes = await fetch( |
22 | `${base}/wiki/api/v2/pages/${page_id}?body-format=${repr}`, |
23 | { |
24 | method: "GET", |
25 | headers: { Authorization: authHeader, Accept: "application/json" }, |
26 | } |
27 | ) |
28 | if (!currentRes.ok) { |
29 | throw new Error(`${currentRes.status} ${await currentRes.text()}`) |
30 | } |
31 | const current = (await currentRes.json()) as { |
32 | title: string |
33 | status: string |
34 | version: { number: number } |
35 | body?: { [key: string]: { value?: string } } |
36 | } |
37 |
|
38 | const payload = { |
39 | id: page_id, |
40 | status: status ?? current.status, |
41 | title: title ?? current.title, |
42 | body: { |
43 | representation: repr, |
44 | value: body ?? current.body?.[repr]?.value ?? "", |
45 | }, |
46 | version: { |
47 | number: current.version.number + 1, |
48 | message: version_message, |
49 | }, |
50 | } |
51 |
|
52 | const response = await fetch(`${base}/wiki/api/v2/pages/${page_id}`, { |
53 | method: "PUT", |
54 | headers: { |
55 | Authorization: authHeader, |
56 | "Content-Type": "application/json", |
57 | Accept: "application/json", |
58 | }, |
59 | body: JSON.stringify(payload), |
60 | }) |
61 |
|
62 | if (!response.ok) { |
63 | throw new Error(`${response.status} ${await response.text()}`) |
64 | } |
65 |
|
66 | return await response.json() |
67 | } |
68 |
|