0

Get Pages in Space

by
Published today

Retrieve a paginated list of pages within a given space.

Script confluence Verified

The script

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

3
/**
4
 * Get Pages in Space
5
 * Retrieve a paginated list of pages within a given space.
6
 */
7
export async function main(
8
  auth: RT.Confluence,
9
  space_id: string,
10
  status: ("current" | "archived" | "deleted" | "trashed")[] | undefined,
11
  title: string | undefined,
12
  sort:
13
    | "id"
14
    | "-id"
15
    | "created-date"
16
    | "-created-date"
17
    | "modified-date"
18
    | "-modified-date"
19
    | "title"
20
    | "-title"
21
    | undefined,
22
  body_format: "storage" | "atlas_doc_format" | undefined,
23
  cursor: string | undefined,
24
  limit: number | undefined
25
) {
26
  const base = auth.baseUrl.replace(/\/$/, "")
27
  const url = new URL(`${base}/wiki/api/v2/spaces/${space_id}/pages`)
28
  for (const s of status ?? []) url.searchParams.append("status", s)
29
  if (title !== undefined && title !== "")
30
    url.searchParams.append("title", title)
31
  if (sort !== undefined) url.searchParams.append("sort", sort)
32
  if (body_format !== undefined)
33
    url.searchParams.append("body-format", body_format)
34
  if (cursor !== undefined && cursor !== "")
35
    url.searchParams.append("cursor", cursor)
36
  if (limit !== undefined) url.searchParams.append("limit", String(limit))
37

38
  const response = await fetch(url, {
39
    method: "GET",
40
    headers: {
41
      Authorization: "Basic " + btoa(`${auth.email}:${auth.apiToken}`),
42
      Accept: "application/json",
43
    },
44
  })
45

46
  if (!response.ok) {
47
    throw new Error(`${response.status} ${await response.text()}`)
48
  }
49

50
  return await response.json()
51
}
52