Skip to content

C3: Improve bindings DX in hono template#5675

Merged
jculvey merged 3 commits intomainfrom
c3-hono-upgrades
Apr 22, 2024
Merged

C3: Improve bindings DX in hono template#5675
jculvey merged 3 commits intomainfrom
c3-hono-upgrades

Conversation

@jculvey
Copy link
Copy Markdown
Contributor

@jculvey jculvey commented Apr 21, 2024

What this PR solves / how to test

The hono template has been updated as follows:

  • Bumps create-hono to 0.7.0
  • Automatically installs dependencies and specifies the detected package manager to avoid interactive prompts
  • Adds a wrangler.toml file with commented out examples of all available bindings to match other templates.
  • Adds a cf-typegen package script to automatically regenerate types for Bindings from wrangler.toml

Example of resulting project with an added kv example: https://github.com/jculvey/cf-hono-example.

Author has addressed the following

@jculvey jculvey requested a review from yusukebe April 21, 2024 18:45
@jculvey jculvey requested a review from a team as a code owner April 21, 2024 18:45
@jculvey jculvey requested a review from a team April 21, 2024 18:45
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Apr 21, 2024

🦋 Changeset detected

Latest commit: 9188267

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

This PR includes changesets to release 1 package
Name Type
create-cloudflare Minor

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
Copy Markdown
Contributor

github-actions bot commented Apr 21, 2024

A wrangler prerelease is available for testing. You can install this latest build in your project with:

npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/8787108403/npm-package-wrangler-5675

You can reference the automatically updated head of this PR with:

npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/prs/5675/npm-package-wrangler-5675

Or you can use npx with this latest build directly:

npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/8787108403/npm-package-wrangler-5675 dev path/to/script.js
Additional artifacts:
npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/8787108403/npm-package-create-cloudflare-5675 --no-auto-update
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/8787108403/npm-package-cloudflare-kv-asset-handler-5675
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/8787108403/npm-package-miniflare-5675
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/8787108403/npm-package-cloudflare-pages-shared-5675
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/8787108403/npm-package-cloudflare-vitest-pool-workers-5675

Note that these links will no longer work once the GitHub Actions artifact expires.


wrangler@3.51.2 includes the following runtime dependencies:

Package Constraint Resolved
miniflare workspace:* 3.20240405.2
workerd 1.20240419.0 1.20240419.0
workerd --version 1.20240419.0 2024-04-19

Please ensure constraints are pinned, and miniflare/workerd minor versions match.

Copy link
Copy Markdown
Member

@dario-piotrowicz dario-piotrowicz left a comment

Choose a reason for hiding this comment

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

Looks good to me (with a few minor suggestions) 🙂

Also if you're adding the toml file support, I'm thinking that it might be appropriate to add an e2e for the bindings access as you did for the other frameworks?

verifyDev: {
route: "/test",
expectedText: "C3_TEST",
},

Copy link
Copy Markdown
Contributor

@yusukebe yusukebe left a comment

Choose a reason for hiding this comment

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

Looks good to me! Thanks!

@jculvey jculvey merged commit 235c439 into main Apr 22, 2024
@jculvey jculvey deleted the c3-hono-upgrades branch April 22, 2024 16:25
@Cherry
Copy link
Copy Markdown
Contributor

Cherry commented Apr 22, 2024

@jculvey Looking at your generated repo at https://github.com/jculvey/cf-hono-example, everything on env is now unknown, so for example:

c.env.ddd

in your repo doesn't throw any errors, it just reports as unknown. Vs with a more traditional bindings definition, you end up with a useful error that tells you it doesn't exist on env:

Property 'ddd' does not exist on type 'CloudflareBindings'.ts(2339)

Is this the intent here? I feel like the DX is much worsened when everything on env is unknown.


Was this added as a result of using an interface to define bindings, instead of a type? And then to make that pass the Record<string, unknown> type Hono requires for Bindings, Record<string, unknown> & CloudflareBindings had to be added? If CloudflareBindings is just a type instead of an interface, everything works as I would expect and provides significantly better DX, imo, reporting when I try and access something on env that doesn't exist, actually as an error.

@jculvey
Copy link
Copy Markdown
Contributor Author

jculvey commented Apr 23, 2024

@Cherry thanks for catching this!

In short, no, the intent here wasn't just to regress the type safety of env 😅 .

In short we're trying to simplify the process of consuming the type definition generated by wrangler types for hono to bring it in line with the rest of the C3 templates.

Unfortunately If you try to assign the result of that command directly to Bindings you end up with this error:

image

As you point out, if the generated CloudflareBindings was a type and not an interface, there wouldn't be an issue.

What I was trying to accomplish was to augment the existing type with the generated one:
image

Agreed this is worse DX. I'll make a PR to patch this up. Thanks again for the report!

@Cherry
Copy link
Copy Markdown
Contributor

Cherry commented Apr 23, 2024

Thanks @jculvey! 🧡

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants