Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for "json" bindings #438

Merged
merged 2 commits into from
Feb 11, 2022
Merged

Conversation

Electroid
Copy link
Contributor

We've had support for "json" bindings for a long time, but never added them to wrangler1.. oops! This is nice because you can include complex values, and you don't need to use things like parseInteger(env.variable) in your code.

# wrangler.toml
[vars]
text = "plain ol' string"
count = 1
complex = { enabled = true, id = 123 }

/cc @mrbbot Since you probably didn't know this existed! 😅

@Electroid Electroid added this to the 2.0 milestone Feb 11, 2022
@changeset-bot
Copy link

changeset-bot bot commented Feb 11, 2022

🦋 Changeset detected

Latest commit: 2e87c36

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
wrangler Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions
Copy link
Contributor

github-actions bot commented Feb 11, 2022

wrangler prerelease is available for testing:

npm install --save-dev https://prerelease-registry.developers.workers.dev/runs/1828147535/wrangler

Copy link
Contributor

@petebacondarwin petebacondarwin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested this with wrangler dev and it does seem to work, despite my misgivings around the JSON encoding of the values.

I added an extra commit with a publish test, which picked up a missing change to the config.ts. (Testing FTW!)

Great quick win @Electroid!

[vars]
text = "plain ol' string"
count = 1
complex = { enabled = true, id = 123 }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strictly this object literal is not JSON (since it is in TOML).
Is the fact that it will be stringified to JSON in order to upload it is an implementation concern?
Perhaps we should just say that we now support more complex literals? Or perhaps more nuanced bindings that can be stringified as JSON?

Comment on lines +99 to +103
if (typeof value === "string") {
metadataBindings.push({ name: key, type: "plain_text", text: value });
} else {
metadataBindings.push({ name: key, type: "json", json: value });
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a long think about this, because it looked kinda dangerous to just rely upon the typeof here. But I came to the conclusion that it is reasonable.

I am slightly concerned about the upstream consumption of these "JSON" values. We simply JSON.stringify() the whole metadata object. So I think we would end up sending a string to the server that is like:

wrangler.toml

[vars]
text = "plain ol' string"
count = 1
complex = { enabled = true, id = 123 }

upload metadata string

'{"main_module":"index.js",' + 
'"bindings":[{"name":"text","type":"plain_text","text":"plain ol' string"},' +
`{"name":"count","type":"json","json":1},' +
`{"name":"complex","type":"json","json":{"enabled":true,"id":123}}],' +
'"compatibility_date":"2022-01-12"}'

Note that the actual text that gets uploaded contains:

{"name":"complex","type":"json","json":{"enabled":true,"id":123}}

I would have expected the value of the complex var to be double encoded in JSON. so it would look like:

{"name":"complex","type":"json","json":"{\\"enabled\\":true,\\"id\\":123}"}

But perhaps I am over thinking this??

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The real test is whether env.complex resolves correctly, and if you're saying you tested it with wrangler dev, then that would means it's all good.

@threepointone
Copy link
Contributor

This looks dope, let's ship it!

@threepointone threepointone merged commit 64d62be into main Feb 11, 2022
@threepointone threepointone deleted the feat/json-binding branch February 11, 2022 09:26
@github-actions github-actions bot mentioned this pull request Feb 11, 2022
@mrbbot
Copy link
Contributor

mrbbot commented Feb 11, 2022

Hey! Thanks for the mention @Electroid. Quick question on this, Wrangler 1 used to stringify all numeric/boolean [vars] (cloudflare/miniflare#50). Was this unintentional? Is this going to be a breaking change in Wrangler 2?

@threepointone
Copy link
Contributor

^ This was my experience with wrangler 1 too, which threw me for a loop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants