feat: update fetch

This commit is contained in:
2022-07-22 23:54:20 +08:00
parent ca32d8701c
commit d90b1e69ea
2 changed files with 82 additions and 48 deletions

View File

@@ -1,6 +1,6 @@
use boa_engine::{Context, JsResult, JsString, JsValue};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use serde_json::{Map, Value};
wit_bindgen_rust::export!("../exports.wit");
wit_bindgen_rust::import!("../container.wit");
@@ -28,15 +28,14 @@ struct FetchResult {
}
fn fetch_fn(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult<JsValue> {
let mut options = String::new();
options.push('[');
args.iter().enumerate().for_each(|(i, arg)| {
if i > 0 { options.push(','); }
options.push_str(arg.to_json(ctx).expect("to json error").as_str().expect("as str error"))
});
options.push(']');
// let r = serde_json::to_string(args).unwrap();
let fetch_result_string = container::fetch(&options);
let mut fetch_params_map = Map::new();
fetch_params_map.insert("url".to_string(), args[0].to_json(ctx).expect("error"));
if args.len() > 1 {
fetch_params_map.insert("params".to_string(), args[1].to_json(ctx).expect("error"));
}
let fetch_params = format!("{}", Value::Object(fetch_params_map));
let fetch_result_string = container::fetch(&fetch_params);
let fetch_result: FetchResult = serde_json::from_str(&fetch_result_string).expect("from str error");
if let Some(result) = fetch_result.result {
let r: Value = serde_json::from_str(&result).expect("result from str error");