rora_javascript_adapter/
lib.rs

1//! JavaScript Adapter
2//!
3//! HTTP messages are a good way to exchange data across the Internet, why not use them to exchange
4//! data between Rust/WASM and JavaScript.
5//!
6use std::collections::HashMap;
7use wasm_bindgen::prelude::*;
8
9
10#[wasm_bindgen]
11///
12/// Represents a request originating from JavaScript.
13/// ```javascript
14/// // JavaScript Example
15/// import {JsRequest} from "my-wasm-app"
16///
17/// const jsRequest = new JsRequest("https://www.rust-lang.org/", "GET");
18/// jsRequest.headers_append("Content-Type", "text/html");
19///
20/// // pass request to WASM app
21/// ```
22///
23/// Consume the request in Rust
24/// ```
25/// use javascript_adapter::JsRequest;
26///
27/// #[wasm_bindgen]
28/// pub fn app(js_request: JsRequest) {
29///     // do things with js_request
30/// }
31///
32pub struct JsRequest {
33    #[wasm_bindgen(skip)]
34    pub uri: String,
35    #[wasm_bindgen(skip)]
36    pub method: String,
37    #[wasm_bindgen(skip)]
38    pub body: Option<String>,
39
40    #[wasm_bindgen(skip)]
41    pub headers: HashMap<String, String>,
42}
43
44#[wasm_bindgen]
45impl JsRequest {
46    #[wasm_bindgen(constructor)]
47    pub fn new(uri: String, method: String) -> JsRequest {
48        JsRequest {
49            uri: uri.into(),
50            method: method.into(),
51            body: None,
52            headers: Default::default(),
53        }
54    }
55
56    #[wasm_bindgen(getter)]
57    pub fn uri(&self) -> String {
58        self.uri.to_string()
59    }
60
61    #[wasm_bindgen(getter)]
62    pub fn method(&self) -> String {
63        self.method.to_string()
64    }
65
66    #[wasm_bindgen(getter)]
67    pub fn body(&self) -> String {
68        self.body.clone().unwrap().to_string()
69    }
70
71    #[wasm_bindgen(setter)]
72    pub fn set_body(&mut self, body: String) {
73        self.body = Some(body);
74    }
75
76    #[wasm_bindgen(getter)]
77    pub fn headers(&self) -> JsValue {
78        JsValue::from_serde(&self.headers).unwrap()
79    }
80
81    pub fn headers_append(&mut self, key: String, value: String) {
82        self.headers.insert(key, value);
83    }
84}
85
86/// Represents a response originating from Rust.
87///
88/// ```
89/// use crate::javascript_adapter::JsResponse;
90///
91/// #[wasm_bindgen]
92/// pub fn app() -> JsResponse {
93///     let mut  response = JsResponse::new();
94///     response.body = Some(String::from("hello world"));
95///     response.headers.insert(String::from("Content-Type"), String::from("text/plain"));
96///     response.status_code = String::from("200");
97///
98///     response
99/// }
100/// ```
101#[wasm_bindgen]
102pub struct JsResponse {
103    #[wasm_bindgen(skip)]
104    pub status_code: String,
105    #[wasm_bindgen(skip)]
106    pub headers: HashMap<String, String>,
107    #[wasm_bindgen(skip)]
108    pub body: Option<String>,
109}
110
111
112#[wasm_bindgen]
113impl JsResponse {
114    #[wasm_bindgen(constructor)]
115    pub fn new() -> JsResponse {
116        JsResponse {
117            status_code: "".to_string(),
118            headers: Default::default(),
119            body: None,
120        }
121    }
122
123    #[wasm_bindgen(getter)]
124    pub fn status_code(&self) -> String {
125        self.status_code.to_string()
126    }
127
128    #[wasm_bindgen(getter)]
129    pub fn body(&self) -> Option<String> {
130        self.body.clone()
131    }
132
133    #[wasm_bindgen(getter)]
134    pub fn headers(&self) -> JsValue {
135        JsValue::from_serde(&self.headers).unwrap()
136    }
137}