0

Search Content (CQL)

by
Published today

Search for content using the Confluence Query Language (CQL).

Script confluence Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 3 hours ago
1
//native
2

3
/**
4
 * Search Content (CQL)
5
 * Search for content using the Confluence Query Language (CQL), e.g. `type=page AND space=ENG AND text ~ "release notes"`.
6
 */
7
export async function main(
8
  auth: RT.Confluence,
9
  cql: string,
10
  cursor: string | undefined,
11
  limit: number | undefined,
12
  expand: string | undefined
13
) {
14
  const base = auth.baseUrl.replace(/\/$/, "")
15
  const url = new URL(`${base}/wiki/rest/api/search`)
16
  url.searchParams.append("cql", cql)
17
  if (cursor !== undefined && cursor !== "")
18
    url.searchParams.append("cursor", cursor)
19
  if (limit !== undefined) url.searchParams.append("limit", String(limit))
20
  if (expand !== undefined && expand !== "")
21
    url.searchParams.append("expand", expand)
22

23
  const response = await fetch(url, {
24
    method: "GET",
25
    headers: {
26
      Authorization: "Basic " + btoa(`${auth.email}:${auth.apiToken}`),
27
      Accept: "application/json",
28
    },
29
  })
30

31
  if (!response.ok) {
32
    throw new Error(`${response.status} ${await response.text()}`)
33
  }
34

35
  return await response.json()
36
}
37