0

Get content descendants by type

by
Published Oct 17, 2025

Returns all descendants of a given type, for a piece of content.

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 content descendants by type
    9
     * Returns all descendants of a given type, for a piece of content.
    10
     */
    11
    export async function main(
    12
    	auth: Confluence,
    13
    	id: string,
    14
    	_type: 'page' | 'comment' | 'attachment',
    15
    	depth: 'all' | 'root' | '<any positive integer argument in the range of 1 and 100>' | undefined,
    16
    	expand: string | undefined,
    17
    	start: string | undefined,
    18
    	limit: string | undefined
    19
    ) {
    20
    	const url = new URL(`https://${auth.domain}/wiki/rest/api/content/${id}/descendant/${_type}`)
    21
    	for (const [k, v] of [
    22
    		['depth', depth],
    23
    		['expand', expand],
    24
    		['start', start],
    25
    		['limit', limit]
    26
    	]) {
    27
    		if (v !== undefined && v !== '' && k !== undefined) {
    28
    			url.searchParams.append(k, v)
    29
    		}
    30
    	}
    31
    	const response = await fetch(url, {
    32
    		method: 'GET',
    33
    		headers: {
    34
    			Authorization: 'Basic ' + btoa(`${auth.email}:${auth.apiToken}`)
    35
    		},
    36
    		body: undefined
    37
    	})
    38
    	if (!response.ok) {
    39
    		const text = await response.text()
    40
    		throw new Error(`${response.status} ${text}`)
    41
    	}
    42
    	return await response.json()
    43
    }
    44