1 | |
2 |
|
3 | |
4 | * List Pages |
5 | * Retrieve a paginated list of pages on the site, with optional filtering and sorting. |
6 | */ |
7 | export async function main( |
8 | auth: RT.Confluence, |
9 | status: ("current" | "archived" | "deleted" | "trashed")[] | undefined, |
10 | title: string | undefined, |
11 | sort: |
12 | | "id" |
13 | | "-id" |
14 | | "created-date" |
15 | | "-created-date" |
16 | | "modified-date" |
17 | | "-modified-date" |
18 | | "title" |
19 | | "-title" |
20 | | undefined, |
21 | body_format: "storage" | "atlas_doc_format" | undefined, |
22 | cursor: string | undefined, |
23 | limit: number | undefined |
24 | ) { |
25 | const base = auth.baseUrl.replace(/\/$/, "") |
26 | const url = new URL(`${base}/wiki/api/v2/pages`) |
27 | for (const s of status ?? []) url.searchParams.append("status", s) |
28 | if (title !== undefined && title !== "") |
29 | url.searchParams.append("title", title) |
30 | if (sort !== undefined) url.searchParams.append("sort", sort) |
31 | if (body_format !== undefined) |
32 | url.searchParams.append("body-format", body_format) |
33 | if (cursor !== undefined && cursor !== "") |
34 | url.searchParams.append("cursor", cursor) |
35 | if (limit !== undefined) url.searchParams.append("limit", String(limit)) |
36 |
|
37 | const response = await fetch(url, { |
38 | method: "GET", |
39 | headers: { |
40 | Authorization: "Basic " + btoa(`${auth.email}:${auth.apiToken}`), |
41 | Accept: "application/json", |
42 | }, |
43 | }) |
44 |
|
45 | if (!response.ok) { |
46 | throw new Error(`${response.status} ${await response.text()}`) |
47 | } |
48 |
|
49 | return await response.json() |
50 | } |
51 |
|