Skip to content

Commit d9af604

Browse files
committed
mention global bindings pattern in #[worker] doc
1 parent 38a4058 commit d9af604

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ npm create cloudflare <project dir> -- --template https://github.com/ohkami-
9494

9595
then `<project dir>` will have `wrangler.jsonc`, `package.json` and a Rust library crate.
9696

97-
A `#[ohkami::worker]` (async/sync) fn returning `Ohkami` is the Worker definition.
97+
`#[ohkami::worker] async? fn({bindings}?) -> Ohkami` is the Worker definition.
9898

9999
Local dev by `npm run dev` and deploy by `npm run deploy` !
100100

ohkami_macros/src/lib.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,11 @@ pub fn operation(args: proc_macro::TokenStream, handler: proc_macro::TokenStream
121121
/// Create an worker Ohkami, running on Cloudflare Workers !
122122
///
123123
/// - This only handle `fetch` event.
124-
/// - Expected signature: `() -> Ohkami` ( both sync/async are available )
124+
/// - Expected signature: `() -> Ohkami` or `(bindings) -> Ohkami` ( both sync/async are available )
125+
///
126+
/// `(bindings) -> Ohkami` pattern is called **global bindings**.
125127
///
126128
/// ---
127-
/// *lib.rs*
128129
/// ```ignore
129130
/// use ohkami::prelude::*;
130131
///
@@ -136,6 +137,34 @@ pub fn operation(args: proc_macro::TokenStream, handler: proc_macro::TokenStream
136137
/// }
137138
/// ```
138139
/// ---
140+
/// ```ignore
141+
/// use ohkami::{prelude::*, worker, bindings};
142+
///
143+
/// #[bindings]
144+
/// struct Bindings {
145+
/// MY_KV: bindings::KV,
146+
/// }
147+
///
148+
/// async fn get_from_kv(
149+
/// Path(key): Path<String>,
150+
/// Context(kv): Context<'_, bindings::KV>,
151+
/// ) -> Result<String, worker::Error> {
152+
/// kv.get(&key).text().await?.ok_or_else(|| worker::Error::RustError(
153+
/// format!("Key '{}' not found in KV", key)
154+
/// ))
155+
/// }
156+
///
157+
/// #[worker]
158+
/// // global bindings
159+
/// fn my_ohkami(b: Bindings) -> Ohkami {
160+
/// Ohkami::new((
161+
/// Context::new(b.MY_KV),
162+
/// "/".GET(|| async {"Hello, world!"}),
163+
/// "/kv/:key".GET(get_from_kv),
164+
/// ))
165+
/// }
166+
/// ```
167+
/// ---
139168
///
140169
/// `#[worker]` accepts an argument in following format for *document purpose*:
141170
///

0 commit comments

Comments
 (0)