Skip to content

Commit

Permalink
refactor: JsResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
amitksingh1490 committed May 4, 2024
1 parent 8642104 commit 8914c32
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions src/cli/javascript/js_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ impl JsRequest {
self.0.method().to_string()
}

pub fn headers(&self) -> BTreeMap<String, String> {
self.0
.headers()
.iter()
.map(|(k, v)| (k.as_str().to_string(), v.to_str().unwrap().to_string()))
.collect()
pub fn headers(&self) -> anyhow::Result<BTreeMap<String, String>> {
let headers = self.0.headers();
let mut map = BTreeMap::new();
for (k, v) in headers.iter() {
map.insert(k.to_string(), v.to_str()?.to_string());
}
Ok(map)
}

pub fn body(&self) -> Option<String> {
Expand Down Expand Up @@ -60,7 +61,14 @@ impl<'js> IntoJs<'js> for JsRequest {
let object = rquickjs::Object::new(ctx.clone())?;
object.set("uri", self.uri())?;
object.set("method", self.method())?;
object.set("headers", self.headers())?;
object.set(
"headers",
self.headers().map_err(|e| rquickjs::Error::FromJs {
from: "HeaderMap",
to: "BTreeMap<String, String>",
message: Some(e.to_string()),

Check warning on line 69 in src/cli/javascript/js_request.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/javascript/js_request.rs#L67-L69

Added lines #L67 - L69 were not covered by tests
})?,
)?;

Check warning on line 71 in src/cli/javascript/js_request.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/javascript/js_request.rs#L71

Added line #L71 was not covered by tests
object.set("body", self.body())?;
Ok(object.into_value())
}
Expand All @@ -78,14 +86,26 @@ impl<'js> FromJs<'js> for JsRequest {
let headers = object.get::<&str, BTreeMap<String, String>>("headers")?;
let body = object.get::<&str, Option<String>>("body")?;
let mut request = reqwest::Request::new(
reqwest::Method::from_bytes(method.as_bytes()).unwrap(),
uri.to_string().parse().unwrap(),
reqwest::Method::from_bytes(method.as_bytes()).map_err(|e| {
rquickjs::Error::FromJs {
from: "string",
to: "Method",
message: Some(e.to_string()),
}

Check warning on line 94 in src/cli/javascript/js_request.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/javascript/js_request.rs#L90-L94

Added lines #L90 - L94 were not covered by tests
})?,
uri.to_string()
.parse()
.map_err(|_| rquickjs::Error::FromJs {
from: "string",
to: "Url",
message: Some("unable to parse URL".to_string()),

Check warning on line 101 in src/cli/javascript/js_request.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/javascript/js_request.rs#L99-L101

Added lines #L99 - L101 were not covered by tests
})?,
);
for (k, v) in headers {
request.headers_mut().insert(
HeaderName::from_str(&k).map_err(|e| rquickjs::Error::FromJs {
from: "string",
to: "reqwest::header::HeaderName",
to: "HeaderName",
message: Some(e.to_string()),

Check warning on line 109 in src/cli/javascript/js_request.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/javascript/js_request.rs#L107-L109

Added lines #L107 - L109 were not covered by tests
})?,
HeaderValue::from_str(v.as_str()).map_err(|e| rquickjs::Error::FromJs {
Expand Down Expand Up @@ -318,7 +338,7 @@ mod tests {
assert_eq!(js_request.method(), "GET");
assert_eq!(js_request.body(), Some("Hello, World!".to_string()));
assert_eq!(
js_request.headers().get("content-type"),
js_request.headers().unwrap().get("content-type"),
Some(&"application/json".to_string())
);
});
Expand Down

0 comments on commit 8914c32

Please sign in to comment.