mirror of
https://github.com/yewstack/yew.git
synced 2025-12-08 21:26:25 +00:00
Re-enable services and agent features by default (#984)
* Re-enable services and agent features by default * Fix failing tests * Fix doc tests * cargo fmt
This commit is contained in:
parent
bb7c8280a6
commit
801b727754
@ -58,7 +58,6 @@ features = [
|
|||||||
"console",
|
"console",
|
||||||
"DedicatedWorkerGlobalScope",
|
"DedicatedWorkerGlobalScope",
|
||||||
"Document",
|
"Document",
|
||||||
"DomException",
|
|
||||||
"DomTokenList",
|
"DomTokenList",
|
||||||
"DragEvent",
|
"DragEvent",
|
||||||
"Element",
|
"Element",
|
||||||
@ -122,7 +121,7 @@ rmp-serde = "0.14.0"
|
|||||||
bincode = "~1.2.1"
|
bincode = "~1.2.1"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = []
|
default = ["services", "agent"]
|
||||||
std_web = ["stdweb"]
|
std_web = ["stdweb"]
|
||||||
web_sys = ["console_error_panic_hook", "futures", "gloo", "js-sys", "web-sys", "wasm-bindgen", "wasm-bindgen-futures"]
|
web_sys = ["console_error_panic_hook", "futures", "gloo", "js-sys", "web-sys", "wasm-bindgen", "wasm-bindgen-futures"]
|
||||||
doc_test = []
|
doc_test = []
|
||||||
|
|||||||
@ -25,10 +25,14 @@ pub enum Referrer {
|
|||||||
#[cfg(feature = "wasm_test")]
|
#[cfg(feature = "wasm_test")]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::callback::test_util::CallbackFuture;
|
use crate::callback::{test_util::CallbackFuture, Callback};
|
||||||
use crate::format::{Json, Nothing};
|
use crate::format::{Json, Nothing};
|
||||||
|
use crate::utils;
|
||||||
|
#[cfg(feature = "web_sys")]
|
||||||
|
use ::web_sys::ReferrerPolicy;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use ssri::Integrity;
|
use ssri::Integrity;
|
||||||
|
use std::collections::HashMap;
|
||||||
use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure};
|
use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure};
|
||||||
|
|
||||||
wasm_bindgen_test_configure!(run_in_browser);
|
wasm_bindgen_test_configure!(run_in_browser);
|
||||||
@ -283,7 +287,7 @@ mod tests {
|
|||||||
.headers
|
.headers
|
||||||
.get("Referer")
|
.get("Referer")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.starts_with(&stdweb::web::window().location().unwrap().origin().unwrap()));
|
.starts_with(&utils::origin().unwrap()));
|
||||||
} else {
|
} else {
|
||||||
assert!(false, "unexpected resp: {:#?}", resp);
|
assert!(false, "unexpected resp: {:#?}", resp);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -17,7 +17,7 @@ use wasm_bindgen::prelude::wasm_bindgen;
|
|||||||
use wasm_bindgen::{JsCast, JsValue};
|
use wasm_bindgen::{JsCast, JsValue};
|
||||||
use wasm_bindgen_futures::{spawn_local, JsFuture};
|
use wasm_bindgen_futures::{spawn_local, JsFuture};
|
||||||
use web_sys::{
|
use web_sys::{
|
||||||
AbortController, DomException, Headers, ReferrerPolicy, Request as WebRequest, RequestInit,
|
AbortController, Headers, ReferrerPolicy, Request as WebRequest, RequestInit,
|
||||||
Response as WebResponse,
|
Response as WebResponse,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -138,8 +138,6 @@ enum FetchError {
|
|||||||
InvalidResponse,
|
InvalidResponse,
|
||||||
#[error("unexpected error, please report")]
|
#[error("unexpected error, please report")]
|
||||||
InternalError,
|
InternalError,
|
||||||
#[error("network failure")]
|
|
||||||
NetworkFailure,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@ -195,6 +193,7 @@ impl FetchService {
|
|||||||
///# use yew::{Component, ComponentLink, Html, Renderable};
|
///# use yew::{Component, ComponentLink, Html, Renderable};
|
||||||
///# use yew::services::FetchService;
|
///# use yew::services::FetchService;
|
||||||
///# use yew::services::fetch::{Response, Request};
|
///# use yew::services::fetch::{Response, Request};
|
||||||
|
///# use anyhow::Error;
|
||||||
///# struct Comp;
|
///# struct Comp;
|
||||||
///# impl Component for Comp {
|
///# impl Component for Comp {
|
||||||
///# type Message = Msg;type Properties = ();
|
///# type Message = Msg;type Properties = ();
|
||||||
@ -234,6 +233,7 @@ impl FetchService {
|
|||||||
///# use yew::services::fetch::Response;
|
///# use yew::services::fetch::Response;
|
||||||
///# use yew::{Component, ComponentLink, Renderable, Html};
|
///# use yew::{Component, ComponentLink, Renderable, Html};
|
||||||
///# use serde_derive::Deserialize;
|
///# use serde_derive::Deserialize;
|
||||||
|
///# use anyhow::Error;
|
||||||
///# struct Comp;
|
///# struct Comp;
|
||||||
///# impl Component for Comp {
|
///# impl Component for Comp {
|
||||||
///# type Message = Msg;type Properties = ();
|
///# type Message = Msg;type Properties = ();
|
||||||
@ -286,6 +286,7 @@ impl FetchService {
|
|||||||
///# use yew::{Renderable, Html, Component, ComponentLink};
|
///# use yew::{Renderable, Html, Component, ComponentLink};
|
||||||
///# use yew::services::FetchService;
|
///# use yew::services::FetchService;
|
||||||
///# use http::Response;
|
///# use http::Response;
|
||||||
|
///# use anyhow::Error;
|
||||||
///# struct Comp;
|
///# struct Comp;
|
||||||
///# impl Component for Comp {
|
///# impl Component for Comp {
|
||||||
///# type Message = Msg;
|
///# type Message = Msg;
|
||||||
@ -433,7 +434,11 @@ where
|
|||||||
// Notice that the callback signature must match the call from the javascript
|
// Notice that the callback signature must match the call from the javascript
|
||||||
// side. There is no static check at this point.
|
// side. There is no static check at this point.
|
||||||
fn callback(&self, data: Result<DATA, Error>, status: u16, headers: Option<Headers>) {
|
fn callback(&self, data: Result<DATA, Error>, status: u16, headers: Option<Headers>) {
|
||||||
let mut response_builder = Response::builder().status(status);
|
let mut response_builder = Response::builder();
|
||||||
|
if let Ok(status) = StatusCode::from_u16(status) {
|
||||||
|
response_builder = response_builder.status(status);
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(headers) = headers {
|
if let Some(headers) = headers {
|
||||||
for (key, value) in header_iter(headers) {
|
for (key, value) in header_iter(headers) {
|
||||||
response_builder = response_builder.header(key.as_str(), value.as_str());
|
response_builder = response_builder.header(key.as_str(), value.as_str());
|
||||||
@ -449,15 +454,9 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn get_response(&self, fetch_promise: Promise) -> Result<WebResponse, FetchError> {
|
async fn get_response(&self, fetch_promise: Promise) -> Result<WebResponse, FetchError> {
|
||||||
let response = JsFuture::from(fetch_promise).await.map_err(|err| {
|
let response = JsFuture::from(fetch_promise)
|
||||||
let dom_exception = DomException::from(err);
|
.await
|
||||||
match dom_exception.code() {
|
.map_err(|_| FetchError::FetchFailed)?;
|
||||||
DomException::ABORT_ERR => FetchError::Canceled,
|
|
||||||
DomException::NETWORK_ERR => FetchError::NetworkFailure,
|
|
||||||
_ => FetchError::FetchFailed,
|
|
||||||
}
|
|
||||||
})?;
|
|
||||||
|
|
||||||
if *self.active.borrow() {
|
if *self.active.borrow() {
|
||||||
Ok(WebResponse::from(response))
|
Ok(WebResponse::from(response))
|
||||||
} else {
|
} else {
|
||||||
@ -573,43 +572,3 @@ impl WindowOrWorker {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
#[cfg(feature = "wasm_test")]
|
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
use crate::callback::test_util::CallbackFuture;
|
|
||||||
use crate::format::{Json, Nothing};
|
|
||||||
use serde::Deserialize;
|
|
||||||
use std::collections::HashMap;
|
|
||||||
use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure};
|
|
||||||
|
|
||||||
wasm_bindgen_test_configure!(run_in_browser);
|
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
|
||||||
struct HttpBin {
|
|
||||||
headers: HashMap<String, String>,
|
|
||||||
origin: String,
|
|
||||||
url: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
async fn fetch_redirect_default() {
|
|
||||||
let request = Request::get("https://httpbin.org/relative-redirect/1")
|
|
||||||
.body(Nothing)
|
|
||||||
.unwrap();
|
|
||||||
let options = FetchOptions::default();
|
|
||||||
let cb_future = CallbackFuture::<Response<Json<Result<HttpBin, Error>>>>::default();
|
|
||||||
let callback: Callback<_> = cb_future.clone().into();
|
|
||||||
let _task = FetchService::new()
|
|
||||||
.fetch_with_options(request, options, callback)
|
|
||||||
.expect("failed to fetch");
|
|
||||||
let resp = cb_future.await;
|
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
|
||||||
if let Json(Ok(http_bin)) = resp.body() {
|
|
||||||
assert_eq!(http_bin.url, String::from("https://httpbin.org/get"));
|
|
||||||
} else {
|
|
||||||
assert!(false, "unexpected resp: {:#?}", resp);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
22
src/utils.rs
22
src/utils.rs
@ -28,7 +28,7 @@ pub fn document() -> Document {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns `host` for the current document. Useful to connect to a server that server the app.
|
/// Returns `host` for the current document. Useful to connect to a server that serves the app.
|
||||||
pub fn host() -> Result<String, Error> {
|
pub fn host() -> Result<String, Error> {
|
||||||
let location = document()
|
let location = document()
|
||||||
.location()
|
.location()
|
||||||
@ -47,6 +47,26 @@ pub fn host() -> Result<String, Error> {
|
|||||||
Ok(host)
|
Ok(host)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns `origin` for the current window.
|
||||||
|
pub fn origin() -> Result<String, Error> {
|
||||||
|
let location = window().location();
|
||||||
|
|
||||||
|
#[cfg(feature = "std_web")]
|
||||||
|
let location = location.ok_or_else(|| anyhow!("can't get location"))?;
|
||||||
|
|
||||||
|
#[cfg(feature = "std_web")]
|
||||||
|
let origin = location.origin().map_err(Error::from)?;
|
||||||
|
|
||||||
|
#[cfg(feature = "web_sys")]
|
||||||
|
let origin = location.origin().map_err(|e| {
|
||||||
|
anyhow!(e
|
||||||
|
.as_string()
|
||||||
|
.unwrap_or_else(|| String::from("error not recoverable")),)
|
||||||
|
})?;
|
||||||
|
|
||||||
|
Ok(origin)
|
||||||
|
}
|
||||||
|
|
||||||
/// Specialty type necessary for helping flattening components returned from nested html macros.
|
/// Specialty type necessary for helping flattening components returned from nested html macros.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct NodeSeq<IN, OUT>(Vec<OUT>, PhantomData<IN>);
|
pub struct NodeSeq<IN, OUT>(Vec<OUT>, PhantomData<IN>);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user