Edits history of script submission #22830 for ' List Spaces (confluence)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * List Spaces
     * Retrieve a paginated list of spaces on the site, with optional filtering by key, type, and status.
     */
    export async function main(
      auth: RT.Confluence,
      keys: string[] | undefined,
      type: "global" | "collaboration" | "knowledge_base" | "personal" | undefined,
      status: "current" | "archived" | undefined,
      sort: "id" | "-id" | "key" | "-key" | "name" | "-name" | undefined,
      cursor: string | undefined,
      limit: number | undefined
    ) {
      const base = auth.baseUrl.replace(/\/$/, "")
      const url = new URL(`${base}/wiki/api/v2/spaces`)
      if (keys !== undefined && keys.length > 0)
        url.searchParams.append("keys", keys.join(","))
      if (type !== undefined) url.searchParams.append("type", type)
      if (status !== undefined) url.searchParams.append("status", status)
      if (sort !== undefined) url.searchParams.append("sort", sort)
      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