Skip to content

Commit 752e5b6

Browse files
committed
Merge remote-tracking branch 'hamza1311/reqwasm/gloo-reqwasm' into gloo-reqwasm
This merge is done to move reqwasm crate to gloo. This puts reqwasm under the gloo umbrella. See #188 (comment) for need of this merge.
2 parents 2aa1f6f + 9cc8722 commit 752e5b6

File tree

10 files changed

+1117
-0
lines changed

10 files changed

+1117
-0
lines changed

.github/workflows/ci.yml

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
8+
9+
env:
10+
CARGO_TERM_COLOR: always
11+
12+
jobs:
13+
fmt:
14+
name: Format
15+
runs-on: ubuntu-latest
16+
steps:
17+
- uses: actions/checkout@v2
18+
19+
- uses: actions-rs/toolchain@v1
20+
with:
21+
toolchain: stable
22+
profile: minimal
23+
components: clippy
24+
target: wasm32-unknown-unknown
25+
26+
- uses: actions/cache@v2
27+
with:
28+
path: |
29+
~/.cargo/registry
30+
~/.cargo/git
31+
target
32+
key: cargo-${{ runner.os }}-fmt-${{ hashFiles('**/Cargo.toml') }}
33+
restore-keys: |
34+
cargo-${{ runner.os }}-fmt-
35+
cargo-${{ runner.os }}-
36+
37+
- name: Run cargo fmt
38+
uses: actions-rs/cargo@v1
39+
with:
40+
command: fmt
41+
args: -- --check
42+
43+
clippy:
44+
name: Clippy
45+
runs-on: ubuntu-latest
46+
steps:
47+
- uses: actions/checkout@v2
48+
- uses: actions-rs/toolchain@v1
49+
with:
50+
toolchain: stable
51+
profile: minimal
52+
components: clippy
53+
target: wasm32-unknown-unknown
54+
55+
- uses: actions/cache@v2
56+
with:
57+
path: |
58+
~/.cargo/registry
59+
~/.cargo/git
60+
target
61+
key: cargo-${{ runner.os }}-clippy-${{ hashFiles('**/Cargo.toml') }}
62+
restore-keys: |
63+
cargo-${{ runner.os }}-clippy-
64+
cargo-${{ runner.os }}-
65+
66+
- name: Run clippy
67+
uses: actions-rs/cargo@v1
68+
with:
69+
command: clippy
70+
args: --target wasm32-unknown-unknown -- -D warnings
71+
72+
- name: Run clippy (no default features)
73+
uses: actions-rs/cargo@v1
74+
with:
75+
command: clippy
76+
args: --no-default-features --target wasm32-unknown-unknown -- -D warnings
77+
78+
- name: Run clippy (with json feature)
79+
uses: actions-rs/cargo@v1
80+
with:
81+
command: clippy
82+
args: --no-default-features --features json --target wasm32-unknown-unknown -- -D warnings
83+
84+
- name: Run clippy (with websocket feature)
85+
uses: actions-rs/cargo@v1
86+
with:
87+
command: clippy
88+
args: --no-default-features --features websocket --target wasm32-unknown-unknown -- -D warnings
89+
90+
- name: Run clippy (with http feature)
91+
uses: actions-rs/cargo@v1
92+
with:
93+
command: clippy
94+
args: --no-default-features --features http --target wasm32-unknown-unknown -- -D warnings
95+
96+
test:
97+
name: Test
98+
runs-on: ubuntu-latest
99+
services:
100+
httpbin:
101+
image: kennethreitz/httpbin@sha256:599fe5e5073102dbb0ee3dbb65f049dab44fa9fc251f6835c9990f8fb196a72b
102+
ports:
103+
- 8080:80
104+
echo_server:
105+
image: jmalloc/echo-server@sha256:c461e7e54d947a8777413aaf9c624b4ad1f1bac5d8272475da859ae82c1abd7d
106+
ports:
107+
- 8081:8080
108+
109+
steps:
110+
- uses: actions/checkout@v2
111+
- uses: actions-rs/toolchain@v1
112+
with:
113+
toolchain: stable
114+
profile: minimal
115+
components: clippy
116+
target: wasm32-unknown-unknown
117+
118+
119+
- name: Install wasm-pack
120+
run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
121+
122+
- uses: actions/cache@v2
123+
with:
124+
path: |
125+
~/.cargo/registry
126+
~/.cargo/git
127+
target
128+
key: cargo-${{ runner.os }}-test-${{ hashFiles('**/Cargo.toml') }}
129+
restore-keys: |
130+
cargo-${{ runner.os }}-test-
131+
cargo-${{ runner.os }}-
132+
133+
- name: Run browser tests
134+
env:
135+
HTTPBIN_URL: "http://localhost:8080"
136+
ECHO_SERVER_URL: "ws://localhost:8081"
137+
run: wasm-pack test --chrome --firefox --headless
138+
139+
- name: Run browser tests
140+
env:
141+
HTTPBIN_URL: "http://localhost:8080"
142+
ECHO_SERVER_URL: "ws://localhost:8081"
143+
uses: actions-rs/cargo@v1
144+
with:
145+
command: test

crates/net/Cargo.toml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
[package]
2+
name = "reqwasm"
3+
version = "0.4.1"
4+
authors = ["Hamza <[email protected]>"]
5+
edition = "2018"
6+
license = "MIT OR Apache-2.0"
7+
repository = "https://github.com/hamza1311/reqwasm"
8+
description = "HTTP requests library for WASM Apps"
9+
readme = "README.md"
10+
keywords = ["requests", "http", "wasm", "websockets"]
11+
categories = ["wasm", "web-programming::http-client", "api-bindings"]
12+
exclude = [
13+
".idea",
14+
]
15+
16+
[package.metadata.docs.rs]
17+
all-features = true
18+
19+
[dependencies]
20+
wasm-bindgen = "0.2"
21+
web-sys = "0.3"
22+
js-sys = "0.3"
23+
gloo-utils = "0.1.0"
24+
25+
wasm-bindgen-futures = "0.4"
26+
futures-core = { version = "0.3", optional = true }
27+
futures-sink = { version = "0.3", optional = true }
28+
29+
thiserror = "1.0"
30+
31+
serde = { version = "1.0", features = ["derive"], optional = true }
32+
serde_json = { version = "1.0", optional = true }
33+
34+
futures-channel = { version = "0.3", optional = true }
35+
pin-project = { version = "1.0", optional = true }
36+
37+
[dev-dependencies]
38+
wasm-bindgen-test = "0.3"
39+
futures = "0.3"
40+
41+
[features]
42+
default = ["json", "websocket", "http"]
43+
44+
# Enables `.json()` on `Response`
45+
json = ["wasm-bindgen/serde-serialize", "serde", "serde_json"]
46+
# Enables the WebSocket API
47+
websocket = [
48+
'web-sys/WebSocket',
49+
'web-sys/ErrorEvent',
50+
'web-sys/FileReader',
51+
'web-sys/MessageEvent',
52+
'web-sys/ProgressEvent',
53+
'web-sys/CloseEvent',
54+
'web-sys/BinaryType',
55+
'web-sys/Blob',
56+
"futures-channel",
57+
"pin-project",
58+
"futures-core",
59+
"futures-sink",
60+
]
61+
# Enables the HTTP API
62+
http = [
63+
'web-sys/Headers',
64+
'web-sys/Request',
65+
'web-sys/RequestInit',
66+
'web-sys/RequestMode',
67+
'web-sys/Response',
68+
'web-sys/Window',
69+
'web-sys/RequestCache',
70+
'web-sys/RequestCredentials',
71+
'web-sys/ObserverCallback',
72+
'web-sys/RequestRedirect',
73+
'web-sys/ReferrerPolicy',
74+
'web-sys/AbortSignal',
75+
'web-sys/ReadableStream',
76+
'web-sys/Blob',
77+
'web-sys/FormData',
78+
]

crates/net/README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Reqwasm
2+
3+
[![crates.io](https://img.shields.io/crates/v/reqwasm.svg?style=flat)](https://crates.io/crates/reqwasm)
4+
[![docs.rs](https://img.shields.io/docsrs/reqwasm)](https://docs.rs/reqwasm/)
5+
![licence](https://img.shields.io/crates/l/reqwasm)
6+
7+
HTTP requests library for WASM Apps. It provides idiomatic Rust bindings for the `web_sys` `fetch` and `WebSocket` API
8+
9+
## Examples
10+
11+
### HTTP
12+
13+
```rust
14+
let resp = Request::get("/path")
15+
.send()
16+
.await
17+
.unwrap();
18+
assert_eq!(resp.status(), 200);
19+
```
20+
21+
### WebSocket
22+
23+
```rust
24+
use reqwasm::websocket::{Message, futures::WebSocket};
25+
use wasm_bindgen_futures::spawn_local;
26+
use futures::{SinkExt, StreamExt};
27+
28+
let mut ws = WebSocket::open("wss://echo.websocket.org").unwrap();
29+
let (mut write, mut read) = ws.split();
30+
31+
spawn_local(async move {
32+
write.send(Message::Text(String::from("test"))).await.unwrap();
33+
write.send(Message::Text(String::from("test 2"))).await.unwrap();
34+
});
35+
36+
spawn_local(async move {
37+
while let Some(msg) = read.next().await {
38+
console_log!(format!("1. {:?}", msg))
39+
}
40+
console_log!("WebSocket Closed")
41+
})
42+
```
43+
## Contributions
44+
45+
Your PRs and Issues are welcome. Note that all the contribution submitted by you, shall be licensed as MIT or APACHE 2.0 at your choice, without any additional terms or conditions.

crates/net/src/error.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use gloo_utils::errors::JsError;
2+
use thiserror::Error as ThisError;
3+
4+
/// All the errors returned by this crate.
5+
#[derive(Debug, ThisError)]
6+
pub enum Error {
7+
/// Error returned by JavaScript.
8+
#[error("{0}")]
9+
JsError(JsError),
10+
/// Error returned by `serde` during deserialization.
11+
#[cfg(feature = "json")]
12+
#[cfg_attr(docsrs, doc(cfg(feature = "json")))]
13+
#[error("{0}")]
14+
SerdeError(
15+
#[source]
16+
#[from]
17+
serde_json::Error,
18+
),
19+
}
20+
21+
#[cfg(any(feature = "http", feature = "websocket"))]
22+
pub(crate) use conversion::*;
23+
#[cfg(any(feature = "http", feature = "websocket"))]
24+
mod conversion {
25+
use gloo_utils::errors::JsError;
26+
use std::convert::TryFrom;
27+
use wasm_bindgen::JsValue;
28+
29+
#[cfg(feature = "http")]
30+
pub(crate) fn js_to_error(js_value: JsValue) -> super::Error {
31+
super::Error::JsError(js_to_js_error(js_value))
32+
}
33+
34+
pub(crate) fn js_to_js_error(js_value: JsValue) -> JsError {
35+
match JsError::try_from(js_value) {
36+
Ok(error) => error,
37+
Err(_) => unreachable!("JsValue passed is not an Error type -- this is a bug"),
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)