0

Set the content state of a content and publishes a new version of the content.

by
Published Oct 17, 2025

Sets the content state of the content specified and creates a new version (publishes the content without changing the body) of the content with the new state.

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
     * Set the content state of a content and publishes a new version of the content.
    9
     * Sets the content state of the content specified and creates a new version
    10
    (publishes the content without changing the body) of the content with the new state.
    11
     */
    12
    export async function main(
    13
    	auth: Confluence,
    14
    	id: string,
    15
    	status: 'current' | 'draft' | undefined,
    16
    	body: { name?: string; color?: string; id?: number }
    17
    ) {
    18
    	const url = new URL(`https://${auth.domain}/wiki/rest/api/content/${id}/state`)
    19
    	for (const [k, v] of [['status', status]]) {
    20
    		if (v !== undefined && v !== '' && k !== undefined) {
    21
    			url.searchParams.append(k, v)
    22
    		}
    23
    	}
    24
    	const response = await fetch(url, {
    25
    		method: 'PUT',
    26
    		headers: {
    27
    			'Content-Type': 'application/json',
    28
    			Authorization: 'Basic ' + btoa(`${auth.email}:${auth.apiToken}`)
    29
    		},
    30
    		body: JSON.stringify(body)
    31
    	})
    32
    	if (!response.ok) {
    33
    		const text = await response.text()
    34
    		throw new Error(`${response.status} ${text}`)
    35
    	}
    36
    	return await response.json()
    37
    }
    38