1 | |
2 | type Confluence = { |
3 | email: string |
4 | apiToken: string |
5 | domain: string |
6 | } |
7 | |
8 | * Asynchronously convert content body |
9 | * Converts a content body from one format to another format asynchronously. |
10 | */ |
11 | export async function main( |
12 | auth: Confluence, |
13 | to: 'export_view', |
14 | expand: string | undefined, |
15 | spaceKeyContext: string | undefined, |
16 | contentIdContext: string | undefined, |
17 | allowCache: string | undefined, |
18 | embeddedContentRender: 'current' | 'version-at-save' | undefined, |
19 | body: { |
20 | value: string |
21 | representation: |
22 | | 'view' |
23 | | 'export_view' |
24 | | 'styled_view' |
25 | | 'storage' |
26 | | 'editor' |
27 | | 'editor2' |
28 | | 'anonymous_export_view' |
29 | | 'wiki' |
30 | | 'atlas_doc_format' |
31 | | 'plain' |
32 | | 'raw' |
33 | } |
34 | ) { |
35 | const url = new URL(`https://${auth.domain}/wiki/rest/api/contentbody/convert/async/${to}`) |
36 | for (const [k, v] of [ |
37 | ['expand', expand], |
38 | ['spaceKeyContext', spaceKeyContext], |
39 | ['contentIdContext', contentIdContext], |
40 | ['allowCache', allowCache], |
41 | ['embeddedContentRender', embeddedContentRender] |
42 | ]) { |
43 | if (v !== undefined && v !== '' && k !== undefined) { |
44 | url.searchParams.append(k, v) |
45 | } |
46 | } |
47 | const response = await fetch(url, { |
48 | method: 'POST', |
49 | headers: { |
50 | 'Content-Type': 'application/json', |
51 | Authorization: 'Basic ' + btoa(`${auth.email}:${auth.apiToken}`) |
52 | }, |
53 | body: JSON.stringify(body) |
54 | }) |
55 | if (!response.ok) { |
56 | const text = await response.text() |
57 | throw new Error(`${response.status} ${text}`) |
58 | } |
59 | return await response.json() |
60 | } |
61 |
|