Edits history of script submission #15329 for ' Removes the content state of a content and publishes a new version. (confluence)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Confluence = {
    	email: string
    	apiToken: string
    	domain: string
    }
    /**
     * Removes the content state of a content and publishes a new version.
     * Removes 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 status.
    
    **[Permissions](https://confluence.atlassian.com/x/_AozKw) required**:
    Permission to edit the content.
     */
    export async function main(auth: Confluence, id: string, status: 'current' | 'draft' | undefined) {
    	const url = new URL(`https://${auth.domain}/wiki/rest/api/content/${id}/state`)
    	for (const [k, v] of [['status', status]]) {
    		if (v !== undefined && v !== '' && k !== undefined) {
    			url.searchParams.append(k, v)
    		}
    	}
    	const response = await fetch(url, {
    		method: 'DELETE',
    		headers: {
    			Authorization: 'Basic ' + btoa(`${auth.email}:${auth.apiToken}`)
    		},
    		body: undefined
    	})
    	if (!response.ok) {
    		const text = await response.text()
    		throw new Error(`${response.status} ${text}`)
    	}
    	return await response.json()
    }
    

    Submitted by hugo697 235 days ago