1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// Most of code in this module is based on Yew's fetch service

pub use http::Request;
use wasm_bindgen::UnwrapThrowExt;

#[derive(thiserror::Error, Debug)]
pub enum FetchError {
    #[error("Invalid request header")]
    InvalidRequestHeader,
    #[error("Build headers failed")]
    BuildHeaderFailed,
    #[error("Build request failed")]
    BuildRequestFailed,
    #[error("Fetch failed")]
    FetchFailed,
    #[error("Invalid response")]
    InvalidResponse,
    #[error("Empty response")]
    EmptyResponse,
    #[error("Parse error: {0}")]
    ParseError(#[from] serde_json::error::Error),
}

#[derive(Debug)]
pub enum Referrer {
    /// `<same-origin URL>` value of referrer.
    SameOriginUrl(String),
    /// `about:client` value of referrer.
    AboutClient,
    /// `<empty string>` value of referrer.
    Empty,
}

/// Init options for `fetch()` function call.
/// https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch
#[derive(Default, Debug)]
pub struct FetchOptions {
    /// Cache of a fetch request.
    pub cache: Option<web_sys::RequestCache>,
    /// Credentials of a fetch request.
    pub credentials: Option<web_sys::RequestCredentials>,
    /// Redirect behaviour of a fetch request.
    pub redirect: Option<web_sys::RequestRedirect>,
    /// Request mode of a fetch request.
    pub mode: Option<web_sys::RequestMode>,
    /// Referrer of a fetch request.
    pub referrer: Option<Referrer>,
    /// Referrer policy of a fetch request.
    pub referrer_policy: Option<web_sys::ReferrerPolicy>,
    /// Integrity of a fetch request.
    pub integrity: Option<String>,
}

impl Into<web_sys::RequestInit> for FetchOptions {
    fn into(self) -> web_sys::RequestInit {
        let mut init = web_sys::RequestInit::new();

        if let Some(cache) = self.cache {
            init.cache(cache);
        }

        if let Some(credentials) = self.credentials {
            init.credentials(credentials);
        }

        if let Some(redirect) = self.redirect {
            init.redirect(redirect);
        }

        if let Some(mode) = self.mode {
            init.mode(mode);
        }

        if let Some(referrer) = self.referrer {
            match referrer {
                Referrer::SameOriginUrl(referrer) => init.referrer(&referrer),
                Referrer::AboutClient => init.referrer("about:client"),
                Referrer::Empty => init.referrer(""),
            };
        }

        if let Some(referrer_policy) = self.referrer_policy {
            init.referrer_policy(referrer_policy);
        }

        if let Some(integrity) = self.integrity {
            init.integrity(&integrity);
        }

        init
    }
}

pub struct FetchCommand<C, F, R> {
    args: Option<FetchCommandArgs<C, F>>,
    _phantom_data: std::marker::PhantomData<R>,
}

struct FetchCommandArgs<C, F> {
    request: http::Request<Option<String>>,
    options: FetchOptions,
    ok_handler: F,
    error_handler: fn(&mut C, FetchError),
}

impl<C, F, R> FetchCommand<C, F, R>
where
    C: crate::component::Component,
{
    pub(crate) fn new(
        request: http::Request<Option<String>>,
        options: Option<FetchOptions>,
        ok_handler: F,
        error_handler: fn(&mut C, FetchError),
    ) -> Self {
        Self {
            args: Some(FetchCommandArgs {
                request,
                options: options.unwrap_or_else(Default::default),
                ok_handler,
                error_handler,
            }),
            _phantom_data: std::marker::PhantomData,
        }
    }
}

impl<C, F, R, Cl> crate::component::Command<C> for FetchCommand<C, F, R>
where
    C: 'static + crate::component::Component,
    R: 'static + serde::de::DeserializeOwned,
    F: 'static + Fn(&mut C, R) -> Cl,
    Cl: 'static + Into<crate::component::Checklist<C>>,
{
    fn execute(&mut self, comp: &crate::component::Comp<C>, state: &mut C) {
        let FetchCommandArgs {
            request,
            options,
            ok_handler,
            error_handler,
        } = self
            .args
            .take()
            .expect_throw("Why FetchCommand is executed twice?");

        // Transform http::Request into web_sys::Request.
        let (parts, body) = request.into_parts();
        let body = body.and_then(|body| match serde_json::to_string(&body) {
            Ok(body) => Some(wasm_bindgen::JsValue::from(body)),
            Err(e) => {
                // The component instance is currently being borrowed,
                // we must send the error via the `state`, not the `comp`.
                error_handler(state, FetchError::from(e));
                None
            }
        });

        let ws_request = match build_request(parts, body.as_ref()) {
            Ok(request) => request,
            Err(e) => {
                // The component instance is currently being borrowed,
                // we must send the error via the `state`, not the `comp`.
                error_handler(state, e);
                return;
            }
        };

        // Transform FetchOptions into RequestInit.
        //
        // Not care about aborting yet
        // let abort_controller = AbortController::new().ok();
        let init = options.into(); //.map_or_else(web_sys::RequestInit::new, Into::into);
                                   // if let Some(abort_controller) = &abort_controller {
                                   //     init.signal(Some(&abort_controller.signal()));
                                   // }

        // Start fetch
        let promise = crate::utils::window().fetch_with_request_and_init(&ws_request, &init);

        // Spawn future to resolve fetch
        // let active = Rc::new(RefCell::new(true));

        let ok_handler = comp.callback_arg(ok_handler);
        let error_handler = comp.callback_arg(error_handler);

        let f = fetch_async(promise, ok_handler, error_handler);
        wasm_bindgen_futures::spawn_local(f);
    }
}

fn build_request(
    parts: http::request::Parts,
    body: Option<&wasm_bindgen::JsValue>,
) -> Result<web_sys::Request, FetchError> {
    use std::iter::FromIterator;

    // Map headers into a Js `Header` type.
    let header_list = parts
        .headers
        .iter()
        .map(|(k, v)| {
            Ok(js_sys::Array::from_iter(&[
                wasm_bindgen::JsValue::from_str(k.as_str()),
                wasm_bindgen::JsValue::from_str(
                    v.to_str().map_err(|_| FetchError::InvalidRequestHeader)?,
                ),
            ]))
        })
        .collect::<Result<js_sys::Array, FetchError>>()?;

    let header_map = web_sys::Headers::new_with_str_sequence_sequence(&header_list)
        .map_err(|_| FetchError::BuildHeaderFailed)?;

    // Formats URI.
    let uri = parts.uri.to_string();
    let method = parts.method.as_str();
    let mut init = web_sys::RequestInit::new();
    init.method(method).body(body).headers(&header_map);
    web_sys::Request::new_with_str_and_init(&uri, &init).map_err(|e| {
        log::error!("{:?}", e);
        FetchError::BuildRequestFailed
    })
}

async fn fetch_async<F1, F2, R>(promise: js_sys::Promise, ok_handler: F1, error_handler: F2)
where
    R: 'static + serde::de::DeserializeOwned,
    F1: FnOnce(R),
    F2: FnOnce(FetchError),
{
    match get_string(promise)
        .await
        .and_then(|data| serde_json::from_str::<R>(&data).map_err(From::from))
    {
        Ok(data) => ok_handler(data),
        Err(e) => error_handler(e),
    }
}

async fn get_string(promise: js_sys::Promise) -> Result<String, FetchError> {
    let response = wasm_bindgen_futures::JsFuture::from(promise)
        .await
        .map_err(|_| FetchError::FetchFailed)?;

    let promise = web_sys::Response::from(response)
        .text()
        .map_err(|_| FetchError::InvalidResponse)?;

    wasm_bindgen_futures::JsFuture::from(promise)
        .await
        .map(|js_text| js_text.as_string())
        .map_err(|_| FetchError::InvalidResponse)?
        .ok_or_else(|| FetchError::EmptyResponse)
}