Edits history of script submission #23115 for ' List Zones (cloudflare)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * List Zones
     * List the zones (domains) the API token can access, optionally filtered by name or status.
     */
    export async function main(
      auth: RT.Cloudflare,
      name: string | undefined,
      status: "initializing" | "pending" | "active" | "moved" | undefined,
      page: number | undefined,
      per_page: number | undefined
    ) {
      const url = new URL("https://api.cloudflare.com/client/v4/zones")
      for (const [k, v] of Object.entries({ name, status, page, per_page })) {
        if (v != null && v !== "") url.searchParams.append(k, String(v))
      }
    
      const response = await fetch(url, {
        headers: {
          Authorization: `Bearer ${auth.token}`,
          Accept: "application/json",
        },
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 4 hours ago