0

List Blog Posts

by
Published today

Retrieve a paginated list of blog posts on the site, with optional filtering and sorting.

Script confluence Verified

The script

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

3
/**
4
 * List Blog Posts
5
 * Retrieve a paginated list of blog posts 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
    | undefined,
19
  body_format: "storage" | "atlas_doc_format" | undefined,
20
  cursor: string | undefined,
21
  limit: number | undefined
22
) {
23
  const base = auth.baseUrl.replace(/\/$/, "")
24
  const url = new URL(`${base}/wiki/api/v2/blogposts`)
25
  for (const s of status ?? []) url.searchParams.append("status", s)
26
  if (title !== undefined && title !== "")
27
    url.searchParams.append("title", title)
28
  if (sort !== undefined) url.searchParams.append("sort", sort)
29
  if (body_format !== undefined)
30
    url.searchParams.append("body-format", body_format)
31
  if (cursor !== undefined && cursor !== "")
32
    url.searchParams.append("cursor", cursor)
33
  if (limit !== undefined) url.searchParams.append("limit", String(limit))
34

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

43
  if (!response.ok) {
44
    throw new Error(`${response.status} ${await response.text()}`)
45
  }
46

47
  return await response.json()
48
}
49