3 stable releases

2.1.0 Nov 8, 2024
2.0.0 Jun 28, 2024
1.0.0 Jun 25, 2024

#355 in WebAssembly

Download history 13/week @ 2024-09-16 9/week @ 2024-09-23 12/week @ 2024-09-30 87/week @ 2024-11-04 14/week @ 2024-11-11

101 downloads per month

MIT license

19KB
266 lines

worker-bindings

Automatically detect bindings in wrangler.toml and bind them to a Rust struct

License CI status crates.io

Example

wrangler.toml

[vars]
MY_VAR = "my-variable"

[[kv_namespaces]]
binding = "MY_KV"
id      = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

lib.rs

use worker::*;
use worker_bindings::bindings;

/* This knows all your bindings in wrangler.toml */
#[bindings]
struct Bindings;

#[event(fetch)]
pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result<Response> {
    /* load bindings from env */
    let b = Bindings::from(&env);

    let var: &'static str = b.MY_VAR;

    let data = b.MY_KV.get("data").text().await?;

    //...
}

Note

  • #[bindings] works in a cargo workspace but has a limitation that it can't resolve wrangler.toml if more than one members have wrangler.tomls.
  • This crate is originally developed in Ohkami web framework and later extracted as an independent edition.

License

worker-bindings is licensed under the MIT License (LICENSE or https://opensource.org/licenses/MIT).

Dependencies

~2.3–3MB
~62K SLoC