0

Search content

by
Published Oct 17, 2025

Searches for content using the [Confluence Query Language (CQL)](https://developer.

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
     * Search content
    9
     * Searches for content using the
    10
    [Confluence Query Language (CQL)](https://developer.
    11
     */
    12
    export async function main(
    13
    	auth: Confluence,
    14
    	cql: string | undefined,
    15
    	cqlcontext: string | undefined,
    16
    	cursor: string | undefined,
    17
    	next: string | undefined,
    18
    	prev: string | undefined,
    19
    	limit: string | undefined,
    20
    	start: string | undefined,
    21
    	includeArchivedSpaces: string | undefined,
    22
    	excludeCurrentSpaces: string | undefined,
    23
    	excerpt:
    24
    		| 'highlight'
    25
    		| 'indexed'
    26
    		| 'none'
    27
    		| 'highlight_unescaped'
    28
    		| 'indexed_unescaped'
    29
    		| undefined,
    30
    	sitePermissionTypeFilter: 'all' | 'externalCollaborator' | 'none' | undefined,
    31
    	_: string | undefined,
    32
    	expand: string | undefined
    33
    ) {
    34
    	const url = new URL(`https://${auth.domain}/wiki/rest/api/search`)
    35
    	for (const [k, v] of [
    36
    		['cql', cql],
    37
    		['cqlcontext', cqlcontext],
    38
    		['cursor', cursor],
    39
    		['next', next],
    40
    		['prev', prev],
    41
    		['limit', limit],
    42
    		['start', start],
    43
    		['includeArchivedSpaces', includeArchivedSpaces],
    44
    		['excludeCurrentSpaces', excludeCurrentSpaces],
    45
    		['excerpt', excerpt],
    46
    		['sitePermissionTypeFilter', sitePermissionTypeFilter],
    47
    		['_', _],
    48
    		['expand', expand]
    49
    	]) {
    50
    		if (v !== undefined && v !== '' && k !== undefined) {
    51
    			url.searchParams.append(k, v)
    52
    		}
    53
    	}
    54
    	const response = await fetch(url, {
    55
    		method: 'GET',
    56
    		headers: {
    57
    			Authorization: 'Basic ' + btoa(`${auth.email}:${auth.apiToken}`)
    58
    		},
    59
    		body: undefined
    60
    	})
    61
    	if (!response.ok) {
    62
    		const text = await response.text()
    63
    		throw new Error(`${response.status} ${text}`)
    64
    	}
    65
    	return await response.json()
    66
    }
    67