Skip to content

Commit dfb6a13

Browse files
committed
docs: update Workers script upload example with new interface
1 parent 833792a commit dfb6a13

File tree

1 file changed

+53
-35
lines changed

1 file changed

+53
-35
lines changed

examples/workers/script-upload.ts

Lines changed: 53 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,39 @@
99
* Set these environment variables:
1010
* - CLOUDFLARE_API_TOKEN
1111
* - CLOUDFLARE_ACCOUNT_ID
12+
*
13+
* ### Workers for Platforms ###
14+
*
15+
* For uploading a User Worker to a dispatch namespace:
16+
* https://developers.cloudflare.com/cloudflare-for-platforms/workers-for-platforms/
17+
*
18+
* Define a "dispatchNamespaceName" variable and change the entire "const script = " line to the following:
19+
* "const script = await client.workersForPlatforms.dispatch.namespaces.scripts.update(dispatchNamespaceName, scriptName, {"
1220
*/
1321

1422
import Cloudflare from 'cloudflare';
23+
import { toFile } from 'cloudflare/index';
24+
25+
const apiToken = process.env['CLOUDFLARE_API_TOKEN'] ?? '';
26+
if (!apiToken) {
27+
throw new Error('Please set envar CLOUDFLARE_ACCOUNT_ID');
28+
}
29+
30+
const accountID = process.env['CLOUDFLARE_ACCOUNT_ID'] ?? '';
31+
if (!accountID) {
32+
throw new Error('Please set envar CLOUDFLARE_API_TOKEN');
33+
}
1534

1635
const client = new Cloudflare({
17-
apiToken: process.env['CLOUDFLARE_API_TOKEN'] ?? '',
36+
apiToken: apiToken,
1837
});
19-
const accountID = process.env['CLOUDFLARE_ACCOUNT_ID'] ?? '';
2038

2139
async function main() {
2240
const scriptName = 'my-hello-world-script';
2341
const scriptFileName = `${scriptName}.mjs`;
42+
43+
// Workers Scripts prefer Module Syntax
44+
// https://blog.cloudflare.com/workers-javascript-modules/
2445
const scriptContent = `
2546
export default {
2647
async fetch(request, env, ctx) {
@@ -29,41 +50,38 @@ async function main() {
2950
};
3051
`;
3152

32-
const script: Cloudflare.Workers.Scripts.ScriptUpdateResponse = await client.workers.scripts.update(
33-
scriptName,
34-
{
53+
try {
54+
// https://developers.cloudflare.com/api/resources/workers/subresources/scripts/methods/update/
55+
const script = await client.workers.scripts.update(scriptName, {
3556
account_id: accountID,
36-
/*
37-
* Add script content keyed by the filename
38-
*/
39-
// @ts-ignore
40-
[scriptFileName]: new File([scriptContent], scriptFileName, {
41-
type: 'application/javascript+module',
42-
}),
43-
// @ts-ignore
44-
metadata: new File(
45-
[
46-
JSON.stringify({
47-
// https://developers.cloudflare.com/workers/configuration/multipart-upload-metadata/
48-
bindings: [
49-
{
50-
type: 'plain_text',
51-
name: 'MESSAGE',
52-
text: 'Hello World!',
53-
},
54-
],
55-
main_module: scriptFileName,
56-
}),
57+
// https://developers.cloudflare.com/workers/configuration/multipart-upload-metadata/
58+
metadata: {
59+
main_module: scriptFileName,
60+
bindings: [
61+
{
62+
type: 'plain_text',
63+
name: 'MESSAGE',
64+
text: 'Hello World!',
65+
},
5766
],
58-
'metadata.json',
59-
{
60-
type: 'application/json',
61-
},
62-
),
63-
},
64-
);
65-
66-
console.log(script.id);
67+
},
68+
files: {
69+
// Add main_module file
70+
[scriptFileName]: await toFile(Buffer.from(scriptContent), scriptFileName, {
71+
type: 'application/javascript+module',
72+
}),
73+
// Can add other files, such as more modules or source maps
74+
// [sourceMapFileName]: await toFile(Buffer.from(sourceMapContent), sourceMapFileName, {
75+
// type: 'application/source-map',
76+
// }),
77+
},
78+
});
79+
console.log('Script Upload success!');
80+
console.log(JSON.stringify(script, null, 2));
81+
} catch (error) {
82+
console.error('Script Upload failure!');
83+
console.error(error);
84+
}
6785
}
6886

6987
main();

0 commit comments

Comments
 (0)