Edits history of script submission #15334 for ' Search content by CQL (confluence)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Confluence = {
    	email: string
    	apiToken: string
    	domain: string
    }
    /**
     * Search content by CQL
     * Returns the list of content that matches a Confluence Query Language
    (CQL) query.
     */
    export async function main(
    	auth: Confluence,
    	cql: string | undefined,
    	cqlcontext: string | undefined,
    	expand: string | undefined,
    	cursor: string | undefined,
    	limit: string | undefined
    ) {
    	const url = new URL(`https://${auth.domain}/wiki/rest/api/content/search`)
    	for (const [k, v] of [
    		['cql', cql],
    		['cqlcontext', cqlcontext],
    		['expand', expand],
    		['cursor', cursor],
    		['limit', limit]
    	]) {
    		if (v !== undefined && v !== '' && k !== undefined) {
    			url.searchParams.append(k, v)
    		}
    	}
    	const response = await fetch(url, {
    		method: 'GET',
    		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