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
//! JavaScript Adapter
//!
//! HTTP messages are good way to exchange data across the Internet, why not use them to exchange
//! data between Rust/WASM and JavaScript.
//!
use std::collections::HashMap;
use wasm_bindgen::prelude::*;


#[wasm_bindgen]
///
/// Represents a request originating from JavaScript.
/// ```javascript
/// // JavaScript Example
/// import {JsRequest} from "my-wasm-app"
///
/// const jsRequest = new JsRequest("https://www.rust-lang.org/", "GET");
/// jsRequest.headers_append("Content-Type", "text/html");
///
/// // pass request to WASM app
/// ```
///
/// Consume the request in Rust
/// ```
/// use javascript_adapter::JsRequest;
///
/// #[wasm_bindgen]
/// pub fn app(js_request: JsRequest) {
///     // do things with js_request
/// }
///
pub struct JsRequest {
    #[wasm_bindgen(skip)]
    pub uri: String,
    #[wasm_bindgen(skip)]
    pub method: String,
    #[wasm_bindgen(skip)]
    pub body: Option<String>,

    #[wasm_bindgen(skip)]
    pub headers: HashMap<String, String>,
}

#[wasm_bindgen]
impl JsRequest {
    #[wasm_bindgen(constructor)]
    pub fn new(uri: String, method: String) -> JsRequest {
        JsRequest {
            uri: uri.into(),
            method: method.into(),
            body: None,
            headers: Default::default(),
        }
    }

    #[wasm_bindgen(getter)]
    pub fn uri(&self) -> String {
        self.uri.to_string()
    }

    #[wasm_bindgen(getter)]
    pub fn method(&self) -> String {
        self.method.to_string()
    }

    #[wasm_bindgen(getter)]
    pub fn body(&self) -> String {
        self.body.clone().unwrap().to_string()
    }

    #[wasm_bindgen(getter)]
    pub fn headers(&self) -> JsValue {
        JsValue::from_serde(&self.headers).unwrap()
    }

    pub fn headers_append(&mut self, key: String, value: String) {
        self.headers.insert(key, value);
    }
}

/// Represents a response originating from Rust.
///
/// ```
/// use crate::javascript_adapter::JsResponse;
///
/// #[wasm_bindgen]
/// pub fn app() -> JsResponse {
///     let mut  response = JsResponse::new();
///     response.body = Some(String::from("hello world"));
///     response.headers.insert(String::from("Content-Type"), String::from("text/plain"));
///     response.status_code = String::from("200");
///
///     response
/// }
/// ```
#[wasm_bindgen]
pub struct JsResponse {
    #[wasm_bindgen(skip)]
    pub status_code: String,
    #[wasm_bindgen(skip)]
    pub headers: HashMap<String, String>,
    #[wasm_bindgen(skip)]
    pub body: Option<String>,
}


#[wasm_bindgen]
impl JsResponse {
    #[wasm_bindgen(constructor)]
    pub fn new() -> JsResponse {
        JsResponse {
            status_code: "".to_string(),
            headers: Default::default(),
            body: None,
        }
    }

    #[wasm_bindgen(getter)]
    pub fn status_code(&self) -> String {
        self.status_code.to_string()
    }

    #[wasm_bindgen(getter)]
    pub fn body(&self) -> Option<String> {
        self.body.clone()
    }

    #[wasm_bindgen(getter)]
    pub fn headers(&self) -> JsValue {
        JsValue::from_serde(&self.headers).unwrap()
    }
}