1 | |
2 |
|
3 | |
4 | * Create Page |
5 | * Create a new page in a space. The body is supplied as a content string in the chosen representation (default "storage", i.e. Confluence storage HTML). |
6 | */ |
7 | export async function main( |
8 | auth: RT.Confluence, |
9 | space_id: string, |
10 | title: string, |
11 | body: string, |
12 | status: "current" | "draft" | undefined, |
13 | parent_id: string | undefined, |
14 | representation: "storage" | "atlas_doc_format" | undefined |
15 | ) { |
16 | const base = auth.baseUrl.replace(/\/$/, "") |
17 | const url = new URL(`${base}/wiki/api/v2/pages`) |
18 |
|
19 | const payload = { |
20 | spaceId: space_id, |
21 | status: status ?? "current", |
22 | title, |
23 | parentId: parent_id, |
24 | body: { |
25 | representation: representation ?? "storage", |
26 | value: body, |
27 | }, |
28 | } |
29 |
|
30 | const response = await fetch(url, { |
31 | method: "POST", |
32 | headers: { |
33 | Authorization: "Basic " + btoa(`${auth.email}:${auth.apiToken}`), |
34 | "Content-Type": "application/json", |
35 | Accept: "application/json", |
36 | }, |
37 | body: JSON.stringify(payload), |
38 | }) |
39 |
|
40 | if (!response.ok) { |
41 | throw new Error(`${response.status} ${await response.text()}`) |
42 | } |
43 |
|
44 | return await response.json() |
45 | } |
46 |
|