Skip to content

Commit b5de9b5

Browse files
mettkekflansburg
andauthored
Adding host_metadata to Cloudflare Request Data (#348)
* 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>
1 parent c81a753 commit b5de9b5

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

worker-sys/src/types/incoming_request_cf_properties.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ extern "C" {
6161
#[wasm_bindgen(method, catch, getter, js_name=regionCode)]
6262
pub fn region_code(this: &IncomingRequestCfProperties) -> Result<Option<String>, JsValue>;
6363

64+
#[wasm_bindgen(method, catch, getter, js_name=hostMetadata)]
65+
pub fn host_metadata(this: &IncomingRequestCfProperties) -> Result<JsValue, JsValue>;
66+
6467
#[wasm_bindgen(method, catch, getter)]
6568
pub fn timezone(this: &IncomingRequestCfProperties) -> Result<String, JsValue>;
6669

worker/src/cf.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,18 @@ impl Cf {
172172
pub fn is_eu_country(&self) -> bool {
173173
self.inner.is_eu_country().unwrap() == Some("1".to_string())
174174
}
175+
176+
pub fn host_metadata<T: serde::de::DeserializeOwned>(&self) -> crate::Result<Option<T>> {
177+
let host_metadata = self.inner.host_metadata()?;
178+
if host_metadata.is_undefined() {
179+
Ok(None)
180+
} else {
181+
serde_wasm_bindgen::from_value(host_metadata)
182+
.map(Some)
183+
.map_err(|e| wasm_bindgen::JsValue::from(e.to_string()))
184+
}
185+
.map_err(crate::Error::from)
186+
}
175187
}
176188

177189
/// Browser-requested prioritization information.

0 commit comments

Comments
 (0)