-
Notifications
You must be signed in to change notification settings - Fork 150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Seanaye/feat/add misc methods #334
Open
seanaye
wants to merge
12
commits into
rustwasm:master
Choose a base branch
from
seanaye:seanaye/feat/add-misc-methods
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+110
−36
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
093a61f
add getter methods and impl clone responsebuilder
seanaye 4278e9a
expose responsebuilder
seanaye d450642
add try clone method
seanaye 9233fa4
cargo fmt
seanaye ca68183
take ownership of request when reading body
seanaye 3af657d
take ownership of response when reading body
seanaye d6ea45a
fix tests
seanaye 63b98b7
derive clone headers
seanaye 52f1a5d
improve safety and clone headers
seanaye 6a89f16
Merge branch 'master' into seanaye/feat/add-misc-methods
seanaye cf92a0b
run cargo fmt
seanaye 1c3e67a
Merge branch 'seanaye/feat/add-misc-methods' of github.com:seanaye/gl…
seanaye File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,7 +64,7 @@ impl RequestBuilder { | |
} | ||
|
||
/// Sets a header. | ||
pub fn header(self, key: &str, value: &str) -> Self { | ||
pub fn header(mut self, key: &str, value: &str) -> Self { | ||
self.headers.set(key, value); | ||
self | ||
} | ||
|
@@ -265,12 +265,12 @@ impl Request { | |
} | ||
|
||
/// Gets the body. | ||
pub fn body(&self) -> Option<ReadableStream> { | ||
pub fn body(self) -> Option<ReadableStream> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function can be called multiple times and still be valid. |
||
self.0.body() | ||
} | ||
|
||
/// Reads the request to completion, returning it as `FormData`. | ||
pub async fn form_data(&self) -> Result<FormData, Error> { | ||
pub async fn form_data(self) -> Result<FormData, Error> { | ||
let promise = self.0.form_data().map_err(js_to_error)?; | ||
let val = JsFuture::from(promise).await.map_err(js_to_error)?; | ||
Ok(FormData::from(val)) | ||
|
@@ -279,12 +279,12 @@ impl Request { | |
/// Reads the request to completion, parsing it as JSON. | ||
#[cfg(feature = "json")] | ||
#[cfg_attr(docsrs, doc(cfg(feature = "json")))] | ||
pub async fn json<T: DeserializeOwned>(&self) -> Result<T, Error> { | ||
pub async fn json<T: DeserializeOwned>(self) -> Result<T, Error> { | ||
serde_json::from_str::<T>(&self.text().await?).map_err(Error::from) | ||
} | ||
|
||
/// Reads the reqeust as a String. | ||
pub async fn text(&self) -> Result<String, Error> { | ||
pub async fn text(self) -> Result<String, Error> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
let promise = self.0.text().unwrap(); | ||
let val = JsFuture::from(promise).await.map_err(js_to_error)?; | ||
let string = js_sys::JsString::from(val); | ||
|
@@ -295,7 +295,7 @@ impl Request { | |
/// | ||
/// This works by obtaining the response as an `ArrayBuffer`, creating a `Uint8Array` from it | ||
/// and then converting it to `Vec<u8>` | ||
pub async fn binary(&self) -> Result<Vec<u8>, Error> { | ||
pub async fn binary(self) -> Result<Vec<u8>, Error> { | ||
let promise = self.0.array_buffer().map_err(js_to_error)?; | ||
let array_buffer: ArrayBuffer = JsFuture::from(promise) | ||
.await | ||
|
@@ -343,6 +343,11 @@ impl Request { | |
.map_err(|e| panic!("fetch returned {:?}, not `Response` - this is a bug", e)) | ||
.map(Response::from) | ||
} | ||
/// attempts to clone the request via the Request.clone api | ||
pub fn try_clone(&self) -> Result<Self, Error> { | ||
let clone = self.0.clone().map_err(js_to_error)?; | ||
Ok(Self(clone)) | ||
} | ||
} | ||
|
||
impl From<web_sys::Request> for Request { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a fan of making this
&mut
. This makes the method unnecessarily restrictive.CC: @futursolo, do you have any thoughts on this?