0

List Spaces

by
Published today

Retrieve a paginated list of spaces on the site, with optional filtering by key, type, and status.

Script confluence Verified

The script

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

3
/**
4
 * List Spaces
5
 * Retrieve a paginated list of spaces on the site, with optional filtering by key, type, and status.
6
 */
7
export async function main(
8
  auth: RT.Confluence,
9
  keys: string[] | undefined,
10
  type: "global" | "collaboration" | "knowledge_base" | "personal" | undefined,
11
  status: "current" | "archived" | undefined,
12
  sort: "id" | "-id" | "key" | "-key" | "name" | "-name" | undefined,
13
  cursor: string | undefined,
14
  limit: number | undefined
15
) {
16
  const base = auth.baseUrl.replace(/\/$/, "")
17
  const url = new URL(`${base}/wiki/api/v2/spaces`)
18
  if (keys !== undefined && keys.length > 0)
19
    url.searchParams.append("keys", keys.join(","))
20
  if (type !== undefined) url.searchParams.append("type", type)
21
  if (status !== undefined) url.searchParams.append("status", status)
22
  if (sort !== undefined) url.searchParams.append("sort", sort)
23
  if (cursor !== undefined && cursor !== "")
24
    url.searchParams.append("cursor", cursor)
25
  if (limit !== undefined) url.searchParams.append("limit", String(limit))
26

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

35
  if (!response.ok) {
36
    throw new Error(`${response.status} ${await response.text()}`)
37
  }
38

39
  return await response.json()
40
}
41