Edits history of script submission #23113 for ' List Worker Deployments (cloudflare)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    export type DynSelect_worker_name = string
    
    // Dropdown of the account's Worker scripts.
    export async function worker_name(auth: RT.Cloudflare) {
      const response = await fetch(
        `https://api.cloudflare.com/client/v4/accounts/${auth.account_id}/workers/scripts`,
        {
          headers: {
            Authorization: `Bearer ${auth.token}`,
            Accept: "application/json",
          },
        }
      )
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
      const { result } = (await response.json()) as { result: { id: string }[] }
      return result.map((s) => ({ value: s.id, label: s.id }))
    }
    
    /**
     * List Worker Deployments
     * List a Worker's deployments, newest first, with the versions and traffic split of each.
     */
    export async function main(
      auth: RT.Cloudflare,
      worker_name: DynSelect_worker_name
    ) {
      const response = await fetch(
        `https://api.cloudflare.com/client/v4/accounts/${auth.account_id}/workers/scripts/${worker_name}/deployments`,
        {
          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