Upload Worker Module (Workers for Platforms)

Upload a worker module to a Workers for Platforms namespace.

Script cloudflare Verified

by hugo697 ยท 11/16/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 383 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * Upload Worker Module (Workers for Platforms)
8
 * Upload a worker module to a Workers for Platforms namespace.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  account_identifier: string,
13
  dispatch_namespace: string,
14
  script_name: string,
15
  body:
16
    | {
17
        "<any part name>"?: string[];
18
        metadata?: {
19
          bindings?: { [k: string]: unknown }[];
20
          body_part?: string;
21
          compatibility_date?: string;
22
          compatibility_flags?: string[];
23
          keep_bindings?: string[];
24
          logpush?: boolean;
25
          main_module?: string;
26
          migrations?:
27
            | ({ new_tag?: string; old_tag?: string; [k: string]: unknown } & {
28
                deleted_classes?: string[];
29
                new_classes?: string[];
30
                renamed_classes?: {
31
                  from?: string;
32
                  to?: string;
33
                  [k: string]: unknown;
34
                }[];
35
                transferred_classes?: {
36
                  from?: string;
37
                  from_script?: string;
38
                  to?: string;
39
                  [k: string]: unknown;
40
                }[];
41
                [k: string]: unknown;
42
              })
43
            | ({ new_tag?: string; old_tag?: string; [k: string]: unknown } & {
44
                steps?: {
45
                  deleted_classes?: string[];
46
                  new_classes?: string[];
47
                  renamed_classes?: {
48
                    from?: string;
49
                    to?: string;
50
                    [k: string]: unknown;
51
                  }[];
52
                  transferred_classes?: {
53
                    from?: string;
54
                    from_script?: string;
55
                    to?: string;
56
                    [k: string]: unknown;
57
                  }[];
58
                  [k: string]: unknown;
59
                }[];
60
                [k: string]: unknown;
61
              });
62
          placement?: { mode?: "smart"; [k: string]: unknown };
63
          tags?: string[];
64
          tail_consumers?: {
65
            environment?: string;
66
            namespace?: string;
67
            service: string;
68
            [k: string]: unknown;
69
          }[];
70
          usage_model?: "bundled" | "unbound";
71
          version_tags?: { [k: string]: unknown };
72
          [k: string]: unknown;
73
        };
74
        [k: string]: unknown;
75
      }
76
    | { message?: string; [k: string]: unknown }
77
) {
78
  const url = new URL(
79
    `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/workers/dispatch/namespaces/${dispatch_namespace}/scripts/${script_name}`
80
  );
81

82
  const formData = new FormData();
83
  for (const [k, v] of Object.entries(body)) {
84
    if (v !== undefined && v !== "") {
85
      formData.append(k, String(v));
86
    }
87
  }
88
  const response = await fetch(url, {
89
    method: "PUT",
90
    headers: {
91
      "X-AUTH-EMAIL": auth.email,
92
      "X-AUTH-KEY": auth.key,
93
      Authorization: "Bearer " + auth.token,
94
    },
95
    body: formData,
96
  });
97
  if (!response.ok) {
98
    const text = await response.text();
99
    throw new Error(`${response.status} ${text}`);
100
  }
101
  return await response.json();
102
}
103