diff --git a/.changeset/green-carrots-scream.md b/.changeset/green-carrots-scream.md new file mode 100644 index 000000000000..7a4881e92743 --- /dev/null +++ b/.changeset/green-carrots-scream.md @@ -0,0 +1,12 @@ +--- +"wrangler": patch +--- + +feat: Add support for "json" bindings + +Did you know? We have support for "json" bindings! Here are a few examples: + +[vars] +text = "plain ol' string" +count = 1 +complex = { enabled = true, id = 123 } diff --git a/.vscode/settings.json b/.vscode/settings.json index 5810fc03be87..a552bde8d041 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -27,7 +27,7 @@ "websockets", "xxhash" ], - "cSpell.ignoreWords": ["yxxx"], + "cSpell.ignoreWords": ["TESTWASMNAME", "yxxx"], "eslint.runtime": "node", "files.trimTrailingWhitespace": false } diff --git a/packages/wrangler/src/__tests__/publish.test.ts b/packages/wrangler/src/__tests__/publish.test.ts index ce1f2f8caa0f..d8bf14e09d68 100644 --- a/packages/wrangler/src/__tests__/publish.test.ts +++ b/packages/wrangler/src/__tests__/publish.test.ts @@ -1215,6 +1215,45 @@ export default{ }); }); + describe("vars bindings", () => { + it("should support json bindings", async () => { + writeWranglerToml({ + vars: { + text: "plain ol' string", + count: 1, + complex: { enabled: true, id: 123 }, + }, + }); + writeWorkerSource(); + mockSubDomainRequest(); + mockUploadWorkerRequest({ + expectedBindings: [ + { name: "text", type: "plain_text", text: "plain ol' string" }, + { name: "count", type: "json", json: 1 }, + { + name: "complex", + type: "json", + json: { enabled: true, id: 123 }, + }, + ], + }); + + await runWrangler("publish index.js"); + expect(std.out).toMatchInlineSnapshot(` + "Uploaded + test-name + (TIMINGS) + Deployed + test-name + (TIMINGS) + + test-name.test-sub-domain.workers.dev" + `); + expect(std.err).toMatchInlineSnapshot(`""`); + expect(std.warn).toMatchInlineSnapshot(`""`); + }); + }); + describe("unsafe bindings", () => { it("should warn if using unsafe bindings", async () => { writeWranglerToml({ @@ -1310,7 +1349,7 @@ function writeWranglerToml(config: Omit = {}) { TOML.stringify({ compatibility_date: "2022-01-12", name: "test-name", - ...config, + ...(config as TOML.JsonMap), }), "utf-8" diff --git a/packages/wrangler/src/api/form_data.ts b/packages/wrangler/src/api/form_data.ts index b78de7c2910f..05591d2a7255 100644 --- a/packages/wrangler/src/api/form_data.ts +++ b/packages/wrangler/src/api/form_data.ts @@ -42,6 +42,7 @@ export interface WorkerMetadata { bindings: ( | { type: "kv_namespace"; name: string; namespace_id: string } | { type: "plain_text"; name: string; text: string } + | { type: "json"; name: string; json: unknown } | { type: "wasm_module"; name: string; part: string } | { type: "durable_object_namespace"; @@ -95,7 +96,11 @@ export function toFormData(worker: CfWorkerInit): FormData { ); Object.entries(bindings.vars || {})?.forEach(([key, value]) => { - metadataBindings.push({ name: key, type: "plain_text", text: value }); + if (typeof value === "string") { + metadataBindings.push({ name: key, type: "plain_text", text: value }); + } else { + metadataBindings.push({ name: key, type: "json", json: value }); + } }); for (const [name, filePath] of Object.entries(bindings.wasm_modules || {})) { diff --git a/packages/wrangler/src/api/worker.ts b/packages/wrangler/src/api/worker.ts index f86207e46129..6dded065a740 100644 --- a/packages/wrangler/src/api/worker.ts +++ b/packages/wrangler/src/api/worker.ts @@ -71,7 +71,7 @@ export interface CfModule { * A map of variable names to values. */ interface CfVars { - [key: string]: string; + [key: string]: unknown; } /** diff --git a/packages/wrangler/src/config.ts b/packages/wrangler/src/config.ts index 58c3a09921c5..fa1a1851496b 100644 --- a/packages/wrangler/src/config.ts +++ b/packages/wrangler/src/config.ts @@ -154,15 +154,14 @@ export type Config = { /** * A map of environment variables to set when deploying your worker. - * Of note, they can only be strings. Which is unfortunate, really. - * (TODO: verify that they can only be strings?) + * * NB: these are not inherited, and HAVE to be duplicated across all environments. * * @default `{}` * @optional * @inherited false */ - vars?: { [key: string]: string }; + vars?: { [key: string]: unknown }; /** * A list of durable objects that your worker should be bound to. diff --git a/packages/wrangler/src/index.tsx b/packages/wrangler/src/index.tsx index 2007c0dbc504..1a8c0063dc2b 100644 --- a/packages/wrangler/src/index.tsx +++ b/packages/wrangler/src/index.tsx @@ -52,6 +52,7 @@ const fgGreenColor = "\x1b[32m"; // a set of binding types that are known to be supported by wrangler const knownBindings = [ "plain_text", + "json", "kv_namespace", "durable_object_namespace", ];