0

Get macro body by macro ID and convert the representation synchronously

by
Published Oct 17, 2025

Returns the body of a macro in format specified in path, for the given macro ID.

Script confluence Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Confluence = {
3
	email: string
4
	apiToken: string
5
	domain: string
6
}
7
/**
8
 * Get macro body by macro ID and convert the representation synchronously
9
 * Returns the body of a macro in format specified in path, for the given macro ID.
10
 */
11
export async function main(
12
	auth: Confluence,
13
	id: string,
14
	version: string,
15
	macroId: string,
16
	to: string,
17
	expand: string | undefined,
18
	spaceKeyContext: string | undefined,
19
	embeddedContentRender: 'current' | 'version-at-save' | undefined
20
) {
21
	const url = new URL(
22
		`https://${auth.domain}/wiki/rest/api/content/${id}/history/${version}/macro/id/${macroId}/convert/${to}`
23
	)
24
	for (const [k, v] of [
25
		['expand', expand],
26
		['spaceKeyContext', spaceKeyContext],
27
		['embeddedContentRender', embeddedContentRender]
28
	]) {
29
		if (v !== undefined && v !== '' && k !== undefined) {
30
			url.searchParams.append(k, v)
31
		}
32
	}
33
	const response = await fetch(url, {
34
		method: 'GET',
35
		headers: {
36
			Authorization: 'Basic ' + btoa(`${auth.email}:${auth.apiToken}`)
37
		},
38
		body: undefined
39
	})
40
	if (!response.ok) {
41
		const text = await response.text()
42
		throw new Error(`${response.status} ${text}`)
43
	}
44
	return await response.json()
45
}
46