0

List Worker Deployments

by
Published today

List a Worker's deployments, newest first, with the versions and traffic split of each.

Script cloudflare Verified

The script

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

3
export type DynSelect_worker_name = string
4

5
// Dropdown of the account's Worker scripts.
6
export async function worker_name(auth: RT.Cloudflare) {
7
  const response = await fetch(
8
    `https://api.cloudflare.com/client/v4/accounts/${auth.account_id}/workers/scripts`,
9
    {
10
      headers: {
11
        Authorization: `Bearer ${auth.token}`,
12
        Accept: "application/json",
13
      },
14
    }
15
  )
16
  if (!response.ok) {
17
    throw new Error(`${response.status} ${await response.text()}`)
18
  }
19
  const { result } = (await response.json()) as { result: { id: string }[] }
20
  return result.map((s) => ({ value: s.id, label: s.id }))
21
}
22

23
/**
24
 * List Worker Deployments
25
 * List a Worker's deployments, newest first, with the versions and traffic split of each.
26
 */
27
export async function main(
28
  auth: RT.Cloudflare,
29
  worker_name: DynSelect_worker_name
30
) {
31
  const response = await fetch(
32
    `https://api.cloudflare.com/client/v4/accounts/${auth.account_id}/workers/scripts/${worker_name}/deployments`,
33
    {
34
      headers: {
35
        Authorization: `Bearer ${auth.token}`,
36
        Accept: "application/json",
37
      },
38
    }
39
  )
40

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

45
  return await response.json()
46
}
47