0

Get macro body by macro ID and convert representation Asynchronously

by
Published Oct 17, 2025

Returns Async Id of the conversion task which will convert the macro into a content body of the desired format.

Script confluence
  • Submitted by hugo697 Bun
    Created 284 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 representation Asynchronously
    9
     * Returns Async Id of the conversion task which will convert the macro into a content body of the desired format.
    10
     */
    11
    export async function main(
    12
    	auth: Confluence,
    13
    	id: string,
    14
    	version: string,
    15
    	macroId: string,
    16
    	to: 'export_view' | 'view' | 'styled_view',
    17
    	expand: string | undefined,
    18
    	allowCache: string | undefined,
    19
    	spaceKeyContext: string | undefined,
    20
    	embeddedContentRender: 'current' | 'version-at-save' | undefined
    21
    ) {
    22
    	const url = new URL(
    23
    		`https://${auth.domain}/wiki/rest/api/content/${id}/history/${version}/macro/id/${macroId}/convert/async/${to}`
    24
    	)
    25
    	for (const [k, v] of [
    26
    		['expand', expand],
    27
    		['allowCache', allowCache],
    28
    		['spaceKeyContext', spaceKeyContext],
    29
    		['embeddedContentRender', embeddedContentRender]
    30
    	]) {
    31
    		if (v !== undefined && v !== '' && k !== undefined) {
    32
    			url.searchParams.append(k, v)
    33
    		}
    34
    	}
    35
    	const response = await fetch(url, {
    36
    		method: 'GET',
    37
    		headers: {
    38
    			Authorization: 'Basic ' + btoa(`${auth.email}:${auth.apiToken}`)
    39
    		},
    40
    		body: undefined
    41
    	})
    42
    	if (!response.ok) {
    43
    		const text = await response.text()
    44
    		throw new Error(`${response.status} ${text}`)
    45
    	}
    46
    	return await response.json()
    47
    }
    48