Skip to main content

dioxus_cloudflare/
bindings.rs

1//! Typed binding shorthands for common Cloudflare Worker resources.
2//!
3//! These combine [`crate::context::env()`] + binding lookup + error conversion
4//! into a single call with contextual error messages.
5
6use crate::context::env;
7use crate::error::CfError;
8
9/// Access a D1 database binding by name.
10///
11/// Shorthand for `cf::env().d1(name).cf()?`.
12///
13/// # Errors
14///
15/// Returns [`CfError`] if the binding is missing or not a D1 database.
16///
17/// # Example
18///
19/// ```rust,ignore
20/// use dioxus_cloudflare::prelude::*;
21///
22/// #[server]
23/// pub async fn get_user(id: String) -> Result<User, ServerFnError> {
24///     let db = cf::d1("DB")?;
25///     // ...
26/// }
27/// ```
28pub fn d1(name: &str) -> Result<worker::D1Database, CfError> {
29    env()
30        .d1(name)
31        .map_err(|e| CfError(worker::Error::RustError(format!("D1 binding `{name}`: {e}"))))
32}
33
34/// Access a Workers KV namespace binding by name.
35///
36/// Shorthand for `cf::env().kv(name).cf()?`.
37///
38/// # Errors
39///
40/// Returns [`CfError`] if the binding is missing or not a KV namespace.
41///
42/// # Example
43///
44/// ```rust,ignore
45/// use dioxus_cloudflare::prelude::*;
46///
47/// #[server]
48/// pub async fn get_value(key: String) -> Result<String, ServerFnError> {
49///     let kv = cf::kv("KV")?;
50///     let val = kv.get(&key).text().await.cf()?.unwrap_or_default();
51///     Ok(val)
52/// }
53/// ```
54pub fn kv(name: &str) -> Result<worker::kv::KvStore, CfError> {
55    env()
56        .kv(name)
57        .map_err(|e| CfError(worker::Error::RustError(format!("KV binding `{name}`: {e}"))))
58}
59
60/// Access an R2 bucket binding by name.
61///
62/// Shorthand for `cf::env().bucket(name).cf()?`.
63///
64/// # Errors
65///
66/// Returns [`CfError`] if the binding is missing or not an R2 bucket.
67///
68/// # Example
69///
70/// ```rust,ignore
71/// use dioxus_cloudflare::prelude::*;
72///
73/// #[server]
74/// pub async fn upload(key: String, data: Vec<u8>) -> Result<(), ServerFnError> {
75///     let bucket = cf::r2("BUCKET")?;
76///     bucket.put(&key, data).execute().await.cf()?;
77///     Ok(())
78/// }
79/// ```
80pub fn r2(name: &str) -> Result<worker::Bucket, CfError> {
81    env()
82        .bucket(name)
83        .map_err(|e| CfError(worker::Error::RustError(format!("R2 binding `{name}`: {e}"))))
84}
85
86/// Access a Durable Object namespace binding by name.
87///
88/// Shorthand for `cf::env().durable_object(name).cf()?`.
89///
90/// # Errors
91///
92/// Returns [`CfError`] if the binding is missing or not a Durable Object namespace.
93///
94/// # Example
95///
96/// ```rust,ignore
97/// use dioxus_cloudflare::prelude::*;
98///
99/// #[server]
100/// pub async fn get_counter(name: String) -> Result<String, ServerFnError> {
101///     let ns = cf::durable_object("COUNTER")?;
102///     let stub = ns.id_from_name(&name)?.get_stub()?;
103///     // ...
104/// }
105/// ```
106pub fn durable_object(name: &str) -> Result<worker::durable::ObjectNamespace, CfError> {
107    env()
108        .durable_object(name)
109        .map_err(|e| CfError(worker::Error::RustError(format!("Durable Object binding `{name}`: {e}"))))
110}
111
112/// Access a Queue producer binding by name.
113///
114/// Shorthand for `cf::env().queue(name).cf()?`.
115///
116/// # Errors
117///
118/// Returns [`CfError`] if the binding is missing or not a Queue.
119///
120/// # Example
121///
122/// ```rust,ignore
123/// use dioxus_cloudflare::prelude::*;
124///
125/// #[server]
126/// pub async fn enqueue_job(payload: String) -> Result<(), ServerFnError> {
127///     let queue = cf::queue("JOBS")?;
128///     queue.send(payload).await.cf()?;
129///     Ok(())
130/// }
131/// ```
132#[cfg(feature = "queue")]
133pub fn queue(name: &str) -> Result<worker::Queue, CfError> {
134    env()
135        .queue(name)
136        .map_err(|e| CfError(worker::Error::RustError(format!("Queue binding `{name}`: {e}"))))
137}