From d1a41077d5b2e5d431d8ea33ab5508030ba8bc6b Mon Sep 17 00:00:00 2001 From: Adam Murray Date: Wed, 25 Oct 2023 11:11:46 -0500 Subject: [PATCH] [templates] cleanup (#4207) * templates: update worker js and ts templates. * remove stackblitz commands from templates. * remove deprecated --local from dev commands. * update compat date and workers types package. * Update templates/worker-typescript/wrangler.toml Co-authored-by: MrBBot --------- Co-authored-by: MrBBot --- templates/worker-aws/package.json | 2 +- templates/worker-durable-objects/package.json | 3 +- .../package.json | 3 +- templates/worker-example-wordle/package.json | 3 +- templates/worker-mysql/package.json | 2 +- templates/worker-postgres/package.json | 2 +- templates/worker-r2/package.json | 3 +- templates/worker-router/package.json | 3 +- templates/worker-sites-react/package.json | 2 +- templates/worker-sites/package.json | 2 +- templates/worker-speedtest/package.json | 2 +- templates/worker-turso-ts/package.json | 2 +- templates/worker-typescript/package.json | 7 +-- templates/worker-typescript/src/index.ts | 31 +++++++++++- templates/worker-typescript/wrangler.toml | 50 ++++++++++++++++++- .../package.json | 3 +- templates/worker-websocket/package.json | 3 +- templates/worker-worktop/package.json | 3 +- templates/worker/package.json | 2 +- templates/worker/wrangler.toml | 4 +- 20 files changed, 100 insertions(+), 32 deletions(-) diff --git a/templates/worker-aws/package.json b/templates/worker-aws/package.json index 62e8e897b53a..d74dbd9d2cbb 100644 --- a/templates/worker-aws/package.json +++ b/templates/worker-aws/package.json @@ -6,7 +6,7 @@ "scripts": { "build": "worktop build index.js", "deploy": "wrangler deploy index.js", - "dev": "wrangler dev index.js --local" + "dev": "wrangler dev index.js" }, "dependencies": { "@aws-sdk/client-dynamodb": "^3.82.0", diff --git a/templates/worker-durable-objects/package.json b/templates/worker-durable-objects/package.json index 3c6231e385de..37aa5b005c62 100644 --- a/templates/worker-durable-objects/package.json +++ b/templates/worker-durable-objects/package.json @@ -4,8 +4,7 @@ "private": true, "scripts": { "deploy": "wrangler deploy index.js", - "dev": "wrangler dev index.js --local", - "start-stackblitz": "WRANGLER_SEND_METRICS=false wrangler dev index.js --local" + "dev": "wrangler dev index.js" }, "devDependencies": { "wrangler": "^3.0.0" diff --git a/templates/worker-example-request-scheduler/package.json b/templates/worker-example-request-scheduler/package.json index 482252276623..2bfc769f363d 100644 --- a/templates/worker-example-request-scheduler/package.json +++ b/templates/worker-example-request-scheduler/package.json @@ -4,8 +4,7 @@ "private": true, "scripts": { "deploy": "wrangler deploy", - "start": "wrangler dev", - "start-stackblitz": "WRANGLER_SEND_METRICS=false wrangler dev --local" + "start": "wrangler dev" }, "devDependencies": { "@cloudflare/workers-types": "^4.20230518.0 ", diff --git a/templates/worker-example-wordle/package.json b/templates/worker-example-wordle/package.json index 9379aadc86b7..ad201eb4f27d 100644 --- a/templates/worker-example-wordle/package.json +++ b/templates/worker-example-wordle/package.json @@ -4,8 +4,7 @@ "private": true, "scripts": { "deploy": "wrangler deploy", - "dev": "wrangler dev --local", - "start-stackblitz": "WRANGLER_SEND_METRICS=false wrangler dev --local" + "dev": "wrangler dev" }, "devDependencies": { "@cloudflare/workers-types": "^4.20230518.0 ", diff --git a/templates/worker-mysql/package.json b/templates/worker-mysql/package.json index e98a468a5cf4..9981902d479f 100644 --- a/templates/worker-mysql/package.json +++ b/templates/worker-mysql/package.json @@ -5,7 +5,7 @@ "scripts": { "build": "esbuild --bundle --sourcemap --outfile=dist/index.mjs --minify --format=esm ./src/index.js --external:*.wasm --inject:./src/deno/workers-override.ts", "deploy": "wrangler deploy src/index.ts", - "dev": "wrangler dev src/index.ts --local" + "dev": "wrangler dev src/index.ts" }, "devDependencies": { "@cloudflare/workers-types": "^4.20230518.0 ", diff --git a/templates/worker-postgres/package.json b/templates/worker-postgres/package.json index 4f5d9fc48f9c..aeb8cc3314ec 100644 --- a/templates/worker-postgres/package.json +++ b/templates/worker-postgres/package.json @@ -5,7 +5,7 @@ "scripts": { "build": "esbuild --bundle --sourcemap --outfile=dist/index.mjs --minify --format=esm ./src/index.js --external:*.wasm --inject:./src/deno/workers-override.ts", "deploy": "wrangler deploy src/index.ts", - "dev": "wrangler dev src/index.ts --local" + "dev": "wrangler dev src/index.ts" }, "devDependencies": { "@cloudflare/workers-types": "^4.20230518.0 ", diff --git a/templates/worker-r2/package.json b/templates/worker-r2/package.json index a0b535cef8af..c563689463b9 100644 --- a/templates/worker-r2/package.json +++ b/templates/worker-r2/package.json @@ -4,8 +4,7 @@ "private": true, "scripts": { "deploy": "wrangler deploy src/index.ts", - "dev": "wrangler dev src/index.ts --local", - "start-stackblitz": "WRANGLER_SEND_METRICS=false wrangler dev src/index.ts --local" + "dev": "wrangler dev src/index.ts" }, "devDependencies": { "@cloudflare/workers-types": "^4.20230518.0 ", diff --git a/templates/worker-router/package.json b/templates/worker-router/package.json index d8fb54a636b1..e628c130b065 100644 --- a/templates/worker-router/package.json +++ b/templates/worker-router/package.json @@ -4,8 +4,7 @@ "private": true, "scripts": { "deploy": "wrangler deploy index.js", - "dev": "wrangler dev index.js --local", - "start-stackblitz": "WRANGLER_SEND_METRICS=false wrangler dev index.js --local", + "dev": "wrangler dev index.js", "test": "vitest" }, "dependencies": { diff --git a/templates/worker-sites-react/package.json b/templates/worker-sites-react/package.json index 6536b06f35d3..5cd640c82f1a 100644 --- a/templates/worker-sites-react/package.json +++ b/templates/worker-sites-react/package.json @@ -4,7 +4,7 @@ "private": true, "scripts": { "deploy": "wrangler deploy", - "dev": "wrangler dev --local", + "dev": "wrangler dev", "react:build": "react-scripts build", "react:eject": "react-scripts eject", "react:start": "react-scripts start", diff --git a/templates/worker-sites/package.json b/templates/worker-sites/package.json index 4906d3aa6c64..e74cfcc85ed4 100644 --- a/templates/worker-sites/package.json +++ b/templates/worker-sites/package.json @@ -4,7 +4,7 @@ "private": true, "scripts": { "deploy": "wrangler deploy index.js", - "dev": "wrangler dev index.js --local" + "dev": "wrangler dev index.js" }, "dependencies": { "@cloudflare/kv-asset-handler": "~0.1.2" diff --git a/templates/worker-speedtest/package.json b/templates/worker-speedtest/package.json index e7daafca9bc2..22f4da6b4fc3 100644 --- a/templates/worker-speedtest/package.json +++ b/templates/worker-speedtest/package.json @@ -4,7 +4,7 @@ "private": true, "scripts": { "deploy": "wrangler deploy src/index.js", - "dev": "wrangler dev src/index.js --local", + "dev": "wrangler dev src/index.js", "test": "jest" }, "devDependencies": { diff --git a/templates/worker-turso-ts/package.json b/templates/worker-turso-ts/package.json index e0107892877c..336b69365edc 100644 --- a/templates/worker-turso-ts/package.json +++ b/templates/worker-turso-ts/package.json @@ -4,7 +4,7 @@ "private": true, "scripts": { "deploy": "wrangler deploy", - "start": "wrangler dev --local" + "start": "wrangler dev" }, "dependencies": { "@libsql/client": "^0.1.1", diff --git a/templates/worker-typescript/package.json b/templates/worker-typescript/package.json index 451b00b83474..71dc3ed283b7 100644 --- a/templates/worker-typescript/package.json +++ b/templates/worker-typescript/package.json @@ -4,13 +4,14 @@ "private": true, "scripts": { "deploy": "wrangler deploy src/index.ts", - "dev": "wrangler dev src/index.ts --local", - "start-stackblitz": "WRANGLER_SEND_METRICS=false wrangler dev src/index.ts --local", + "dev": "wrangler dev", + "start": "wrangler dev", "test": "vitest run" }, "devDependencies": { - "@cloudflare/workers-types": "^4.20230518.0 ", + "@cloudflare/workers-types": "^4.20231016.0", "vitest": "^0.31.0", + "typescript": "^5.0.4", "wrangler": "^3.0.0" } } diff --git a/templates/worker-typescript/src/index.ts b/templates/worker-typescript/src/index.ts index ac78190d8680..8087b850d56c 100644 --- a/templates/worker-typescript/src/index.ts +++ b/templates/worker-typescript/src/index.ts @@ -1,5 +1,32 @@ +/** + * Welcome to Cloudflare Workers! This is your first worker. + * + * - Run `npm run dev` in your terminal to start a development server + * - Open a browser tab at http://localhost:8787/ to see your worker in action + * - Run `npm run deploy` to publish your worker + * + * Learn more at https://developers.cloudflare.com/workers/ + */ + +export interface Env { + // Example binding to KV. Learn more at https://developers.cloudflare.com/workers/runtime-apis/kv/ + // MY_KV_NAMESPACE: KVNamespace; + // + // Example binding to Durable Object. Learn more at https://developers.cloudflare.com/workers/runtime-apis/durable-objects/ + // MY_DURABLE_OBJECT: DurableObjectNamespace; + // + // Example binding to R2. Learn more at https://developers.cloudflare.com/workers/runtime-apis/r2/ + // MY_BUCKET: R2Bucket; + // + // Example binding to a Service. Learn more at https://developers.cloudflare.com/workers/runtime-apis/service-bindings/ + // MY_SERVICE: Fetcher; + // + // Example binding to a Queue. Learn more at https://developers.cloudflare.com/queues/javascript-apis/ + // MY_QUEUE: Queue; +} + export default { - async fetch(request: Request) { - return new Response(`request method: ${request.method}`); + async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise { + return new Response('Hello World!'); }, }; diff --git a/templates/worker-typescript/wrangler.toml b/templates/worker-typescript/wrangler.toml index 5ac8507062da..68270740adfe 100644 --- a/templates/worker-typescript/wrangler.toml +++ b/templates/worker-typescript/wrangler.toml @@ -1,3 +1,51 @@ name = "" # todo main = "./src/index.ts" -compatibility_date = "2022-05-03" +compatibility_date = "2022-10-10" + +# Variable bindings. These are arbitrary, plaintext strings (similar to environment variables) +# Note: Use secrets to store sensitive data. +# Docs: https://developers.cloudflare.com/workers/platform/environment-variables +# [vars] +# MY_VARIABLE = "production_value" + +# Bind a KV Namespace. Use KV as persistent storage for small key-value pairs. +# Docs: https://developers.cloudflare.com/workers/runtime-apis/kv +# [[kv_namespaces]] +# binding = "MY_KV_NAMESPACE" +# id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + +# Bind an R2 Bucket. Use R2 to store arbitrarily large blobs of data, such as files. +# Docs: https://developers.cloudflare.com/r2/api/workers/workers-api-usage/ +# [[r2_buckets]] +# binding = "MY_BUCKET" +# bucket_name = "my-bucket" + +# Bind a Queue producer. Use this binding to schedule an arbitrary task that may be processed later by a Queue consumer. +# Docs: https://developers.cloudflare.com/queues/get-started +# [[queues.producers]] +# binding = "MY_QUEUE" +# queue = "my-queue" + +# Bind a Queue consumer. Queue Consumers can retrieve tasks scheduled by Producers to act on them. +# Docs: https://developers.cloudflare.com/queues/get-started +# [[queues.consumers]] +# queue = "my-queue" + +# Bind another Worker service. Use this binding to call another Worker without network overhead. +# Docs: https://developers.cloudflare.com/workers/platform/services +# [[services]] +# binding = "MY_SERVICE" +# service = "my-service" + +# Bind a Durable Object. Durable objects are a scale-to-zero compute primitive based on the actor model. +# Durable Objects can live for as long as needed. Use these when you need a long-running "server", such as in realtime apps. +# Docs: https://developers.cloudflare.com/workers/runtime-apis/durable-objects +# [[durable_objects.bindings]] +# name = "MY_DURABLE_OBJECT" +# class_name = "MyDurableObject" + +# Durable Object migrations. +# Docs: https://developers.cloudflare.com/workers/learning/using-durable-objects#configure-durable-object-classes-with-migrations +# [[migrations]] +# tag = "v1" +# new_classes = ["MyDurableObject"] diff --git a/templates/worker-websocket-durable-objects/package.json b/templates/worker-websocket-durable-objects/package.json index afc3df711d4f..1bbd172b4c18 100644 --- a/templates/worker-websocket-durable-objects/package.json +++ b/templates/worker-websocket-durable-objects/package.json @@ -4,8 +4,7 @@ "private": true, "scripts": { "deploy": "wrangler deploy src/index.ts", - "start": "wrangler dev src/index.ts --local", - "start-stackblitz": "WRANGLER_SEND_METRICS=false wrangler dev src/index.ts --local" + "start": "wrangler dev src/index.ts" }, "devDependencies": { "@cloudflare/workers-types": "^4.20230518.0 ", diff --git a/templates/worker-websocket/package.json b/templates/worker-websocket/package.json index 7f919d88fd91..c65c173cb841 100644 --- a/templates/worker-websocket/package.json +++ b/templates/worker-websocket/package.json @@ -4,8 +4,7 @@ "private": true, "scripts": { "deploy": "wrangler deploy index.js", - "dev": "wrangler dev index.js --local", - "start-stackblitz": "WRANGLER_SEND_METRICS=false wrangler dev index.js --local" + "dev": "wrangler dev index.js" }, "devDependencies": { "@cloudflare/workers-types": "^4.20230518.0 ", diff --git a/templates/worker-worktop/package.json b/templates/worker-worktop/package.json index 58ac199d402e..6c19cc7e4891 100644 --- a/templates/worker-worktop/package.json +++ b/templates/worker-worktop/package.json @@ -4,8 +4,7 @@ "private": true, "scripts": { "deploy": "wrangler deploy src/index.ts", - "dev": "wrangler dev src/index.ts --local", - "start-stackblitz": "WRANGLER_SEND_METRICS=false wrangler dev src/index.ts --local" + "dev": "wrangler dev src/index.ts" }, "dependencies": { "worktop": "0.8.0-next.14" diff --git a/templates/worker/package.json b/templates/worker/package.json index 97b840fb9e37..247dfec9096c 100644 --- a/templates/worker/package.json +++ b/templates/worker/package.json @@ -4,7 +4,7 @@ "private": true, "scripts": { "deploy": "wrangler deploy index.js", - "dev": "wrangler dev index.js --local", + "dev": "wrangler dev index.js", "test": "vitest" }, "devDependencies": { diff --git a/templates/worker/wrangler.toml b/templates/worker/wrangler.toml index f35db7967a13..545b796231e7 100644 --- a/templates/worker/wrangler.toml +++ b/templates/worker/wrangler.toml @@ -1,3 +1,3 @@ name = "" # todo -main = "./index.js" -compatibility_date = "2022-05-03" +main = "./index.js" +compatibility_date = "2022-10-10"