1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
use crate::{DoProxy, Proxy};
/// The [`EnvExt`] trait makes it easy to create proxies from a [`worker::Env`].
///
/// Defines multiple methods on the [`workers::Env`] type to make it easier to
/// create proxies.
///
/// # Example
///
/// ```ignore
/// #[worker::event(fetch, respond_with_errors)]
/// pub async fn main(mut req: Request, env: Env, _ctx: worker::Context) -> Result<Response> {
/// let url = req.url()?;
/// let do_name = url.path();
///
/// let command = req.json().await?;
///
/// // Easily access an `Inserter` object with the given name.
/// let inserter = env.obj::<Inserter>(do_name)?;
/// let resp = inserter.send(command).await??;
///
/// Response::from_json(&resp)
/// }
/// ```
pub trait EnvExt {
/// Get a proxy to a durable object with the given name.
///
/// ```ignore
/// env.obj::<Inserter>("inserter_for_fisher")?;
/// ```
fn obj<Obj>(&self, name: &str) -> Result<Proxy<Obj>, worker::Error>
where
Obj: DoProxy;
/// Get a proxy to a durable object with the given hex ID.
///
/// ```ignore
/// env.obj::<Inserter>("<long_hex_string>")?;
/// ```
fn obj_from_id<Obj>(&self, id: &str) -> Result<Proxy<Obj>, worker::Error>
where
Obj: DoProxy;
/// Get a unique proxy to a durable object.
fn unique_obj<Obj>(&self) -> Result<Proxy<Obj>, worker::Error>
where
Obj: DoProxy;
}
impl EnvExt for worker::Env {
fn obj<Obj>(&self, name: &str) -> Result<Proxy<Obj>, worker::Error>
where
Obj: DoProxy,
{
let binding = self.durable_object(Obj::BINDING)?;
let obj = binding.id_from_name(name)?;
let stub = obj.get_stub()?;
Ok(Proxy::new(stub))
}
fn obj_from_id<Obj>(&self, id: &str) -> Result<Proxy<Obj>, worker::Error>
where
Obj: DoProxy,
{
let binding = self.durable_object(Obj::BINDING)?;
let obj = binding.id_from_string(id)?;
let stub = obj.get_stub()?;
Ok(Proxy::new(stub))
}
fn unique_obj<Obj>(&self) -> Result<Proxy<Obj>, worker::Error>
where
Obj: DoProxy,
{
let binding = self.durable_object(Obj::BINDING)?;
let obj = binding.unique_id()?;
let stub = obj.get_stub()?;
Ok(Proxy::new(stub))
}
}