Skip to content

Commit

Permalink
feat: Add support for "json" bindings (#438)
Browse files Browse the repository at this point in the history
* feat: Add support for "json" bindings

* fixup! feat: Add support for "json" bindings

Co-authored-by: Pete Bacon Darwin <pete@bacondarwin.com>
  • Loading branch information
Electroid and petebacondarwin committed Feb 11, 2022
1 parent a52f0e0 commit 64d62be
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 7 deletions.
12 changes: 12 additions & 0 deletions .changeset/green-carrots-scream.md
Original file line number Diff line number Diff line change
@@ -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 }
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"websockets",
"xxhash"
],
"cSpell.ignoreWords": ["yxxx"],
"cSpell.ignoreWords": ["TESTWASMNAME", "yxxx"],
"eslint.runtime": "node",
"files.trimTrailingWhitespace": false
}
41 changes: 40 additions & 1 deletion packages/wrangler/src/__tests__/publish.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -1310,7 +1349,7 @@ function writeWranglerToml(config: Omit<Config, "env"> = {}) {
TOML.stringify({
compatibility_date: "2022-01-12",
name: "test-name",
...config,
...(config as TOML.JsonMap),
}),

"utf-8"
Expand Down
7 changes: 6 additions & 1 deletion packages/wrangler/src/api/form_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 || {})) {
Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/src/api/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export interface CfModule {
* A map of variable names to values.
*/
interface CfVars {
[key: string]: string;
[key: string]: unknown;
}

/**
Expand Down
5 changes: 2 additions & 3 deletions packages/wrangler/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions packages/wrangler/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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",
];
Expand Down

0 comments on commit 64d62be

Please sign in to comment.