Skip to content

Commit

Permalink
Add encodeBody and cf to ResponseInit. Adopt builder pattern.
Browse files Browse the repository at this point in the history
Closes #567
  • Loading branch information
kflansburg committed May 4, 2024
1 parent a2b50b7 commit ed96771
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 106 deletions.
33 changes: 24 additions & 9 deletions worker-sys/src/ext/response_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,32 @@ use wasm_bindgen::prelude::*;

pub trait ResponseInitExt {
/// Change the `webSocket` field of this object.
fn websocket(&mut self, val: &web_sys::WebSocket) -> &mut Self;
fn websocket(&mut self, val: &web_sys::WebSocket) -> Result<&mut Self, JsValue>;

/// Change the `encodeBody` field of this object.
fn encode_body(&mut self, val: String) -> Result<&mut Self, JsValue>;

/// Change the `cf` field of this object.
fn cf(&mut self, val: &JsValue) -> Result<&mut Self, JsValue>;
}

impl ResponseInitExt for web_sys::ResponseInit {
fn websocket(&mut self, val: &web_sys::WebSocket) -> &mut Self {
let r = js_sys::Reflect::set(self.as_ref(), &JsValue::from("webSocket"), val.as_ref());
debug_assert!(
r.is_ok(),
"setting properties should never fail on our dictionary objects"
);
let _ = r;
self
fn websocket(&mut self, val: &web_sys::WebSocket) -> Result<&mut Self, JsValue> {
js_sys::Reflect::set(self.as_ref(), &JsValue::from("webSocket"), val.as_ref())?;
Ok(self)
}

fn encode_body(&mut self, val: String) -> Result<&mut Self, JsValue> {
js_sys::Reflect::set(
self.as_ref(),
&JsValue::from("encodeBody"),
&JsValue::from(val),
)?;
Ok(self)
}

fn cf(&mut self, val: &JsValue) -> Result<&mut Self, JsValue> {
js_sys::Reflect::set(self.as_ref(), &JsValue::from("cf"), val)?;
Ok(self)
}
}

0 comments on commit ed96771

Please sign in to comment.