Skip to content

Commit

Permalink
Adding host_metadata to Cloudflare Request Data (#348)
Browse files Browse the repository at this point in the history
* Adding host_metadata to Cloudflare Request Data

* Update incoming_request_cf_properties.rs

Make `host_metadata` method `catch`

* Update cf.rs

---------

Co-authored-by: Kevin Flansburg <kflansburg@cloudflare.com>
Co-authored-by: Kevin Flansburg <kevin.flansburg@gmail.com>
  • Loading branch information
3 people authored Oct 11, 2024
1 parent c81a753 commit b5de9b5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
3 changes: 3 additions & 0 deletions worker-sys/src/types/incoming_request_cf_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ extern "C" {
#[wasm_bindgen(method, catch, getter, js_name=regionCode)]
pub fn region_code(this: &IncomingRequestCfProperties) -> Result<Option<String>, JsValue>;

#[wasm_bindgen(method, catch, getter, js_name=hostMetadata)]
pub fn host_metadata(this: &IncomingRequestCfProperties) -> Result<JsValue, JsValue>;

#[wasm_bindgen(method, catch, getter)]
pub fn timezone(this: &IncomingRequestCfProperties) -> Result<String, JsValue>;

Expand Down
12 changes: 12 additions & 0 deletions worker/src/cf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,18 @@ impl Cf {
pub fn is_eu_country(&self) -> bool {
self.inner.is_eu_country().unwrap() == Some("1".to_string())
}

pub fn host_metadata<T: serde::de::DeserializeOwned>(&self) -> crate::Result<Option<T>> {
let host_metadata = self.inner.host_metadata()?;
if host_metadata.is_undefined() {
Ok(None)
} else {
serde_wasm_bindgen::from_value(host_metadata)
.map(Some)
.map_err(|e| wasm_bindgen::JsValue::from(e.to_string()))
}
.map_err(crate::Error::from)
}
}

/// Browser-requested prioritization information.
Expand Down

0 comments on commit b5de9b5

Please sign in to comment.