Edits history of script submission #22829 for ' List Pages (confluence)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * List Pages
     * Retrieve a paginated list of pages on the site, with optional filtering and sorting.
     */
    export async function main(
      auth: RT.Confluence,
      status: ("current" | "archived" | "deleted" | "trashed")[] | undefined,
      title: string | undefined,
      sort:
        | "id"
        | "-id"
        | "created-date"
        | "-created-date"
        | "modified-date"
        | "-modified-date"
        | "title"
        | "-title"
        | undefined,
      body_format: "storage" | "atlas_doc_format" | undefined,
      cursor: string | undefined,
      limit: number | undefined
    ) {
      const base = auth.baseUrl.replace(/\/$/, "")
      const url = new URL(`${base}/wiki/api/v2/pages`)
      for (const s of status ?? []) url.searchParams.append("status", s)
      if (title !== undefined && title !== "")
        url.searchParams.append("title", title)
      if (sort !== undefined) url.searchParams.append("sort", sort)
      if (body_format !== undefined)
        url.searchParams.append("body-format", body_format)
      if (cursor !== undefined && cursor !== "")
        url.searchParams.append("cursor", cursor)
      if (limit !== undefined) url.searchParams.append("limit", String(limit))
    
      const response = await fetch(url, {
        method: "GET",
        headers: {
          Authorization: "Basic " + btoa(`${auth.email}:${auth.apiToken}`),
          Accept: "application/json",
        },
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 4 hours ago