feat: wit-bindgen-sample

This commit is contained in:
2022-07-23 10:48:31 +08:00
parent a116b1c2e6
commit 7d769d3a0f
5 changed files with 31 additions and 22 deletions

View File

@@ -14,9 +14,9 @@ impl FnResult {
}
}
pub fn success(result: String) -> Self {
pub fn success(result: impl Into<String>) -> Self {
FnResult {
result: Some(result),
result: Some(result.into()),
error: None,
}
}

View File

@@ -96,16 +96,16 @@ pub fn do_fetch(params: &str) -> FnResult {
let mut result_map = Map::new();
result_map.insert("status".to_string(), Value::Number(Number::from(status)));
let mut header_map = Map::new();
let mut headers_map = Map::new();
for (k, v) in headers {
let header_key = k.map(|n| n.as_str().to_string()).unwrap_or_else(|| "".to_string());
let header_value = match v.to_str() {
Err(_) => continue,
Ok(v) => v.to_string(),
};
header_map.insert(header_key, Value::String(header_value));
headers_map.insert(header_key, Value::String(header_value));
}
result_map.insert("headers".to_string(), Value::Object(header_map));
result_map.insert("headers".to_string(), Value::Object(headers_map));
result_map.insert("body".to_string(), Value::String(String::from_utf8_lossy(&body_buff).to_string()));
FnResult::success(format!("{}", Value::Object(result_map)))

View File

@@ -30,15 +30,15 @@ fn main() -> Result<()> {
)?;
let a = exports.eval_javascript(&mut store, r##"
function hi(name) { return "hi: " + name; }
let a = [];
a.push(fetch('https://hatter.ink/util/print_request.action'));
for (let i = 0; i < 3; i++) { a.push(i); }
a.push({
id: 1, name: 'hatter'
});
a.push(hi('001'));
a.push(hi('002'));
// let a = fetch('https://hatter.ink/util/print_request.action');
// let a = [];
// a.push(fetch('https://hatter.ink/util/print_request.action'));
// for (let i = 0; i < 3; i++) { a.push(i); }
// a.push({
// id: 1, name: 'hatter'
// });
// a.push(hi('001'));
// a.push(hi('002'));
let a = fetch('https://hatter.ink/util/print_request.action');
JSON.stringify(a)
// a
"##);