Add error to JsValue conversion and example (#691)

This commit is contained in:
John Gallagher
2019-10-30 16:30:32 -04:00
committed by Sean McArthur
parent 57300edbc4
commit 43f2ff083c
9 changed files with 6224 additions and 0 deletions

5
examples/wasm_github_fetch/.gitignore vendored Normal file
View File

@@ -0,0 +1,5 @@
node_modules
pkg
target
Cargo.lock
*.swp

View File

@@ -0,0 +1,18 @@
[package]
name = "wasm"
version = "0.1.0"
authors = ["John Gallagher <john.willis.gallagher@gmail.com>"]
edition = "2018"
# Config mostly pulled from: https://github.com/rustwasm/wasm-bindgen/blob/master/examples/fetch/Cargo.toml
[lib]
crate-type = ["cdylib"]
[dependencies]
reqwest = {path = "../../"}
serde = { version = "1.0.101", features = ["derive"] }
serde_derive = "^1.0.59"
wasm-bindgen-futures = "0.4.1"
serde_json = "1.0.41"
wasm-bindgen = { version = "0.2.51", features = ["serde-serialize"] }

View File

@@ -0,0 +1,11 @@
## Example usage of Reqwest from WASM
You can build the example locally with:
npm run serve
and then visiting http://localhost:8080 in a browser should run the example!
This example is loosely based off of [this example](https://github.com/rustwasm/wasm-bindgen/blob/master/examples/fetch/src/lib.rs), an example usage of `fetch` from `wasm-bindgen`.

View File

@@ -0,0 +1,12 @@
const rust = import('./pkg');
rust
.then(m => {
return m.run().then((data) => {
console.log(data);
console.log("The latest commit to the wasm-bindgen %s branch is:", data.name);
console.log("%s, authored by %s <%s>", data.commit.sha, data.commit.commit.author.name, data.commit.commit.author.email);
})
})
.catch(console.error);

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,14 @@
{
"scripts": {
"build": "webpack",
"serve": "webpack-dev-server"
},
"devDependencies": {
"@wasm-tool/wasm-pack-plugin": "1.0.1",
"text-encoding": "^0.7.0",
"html-webpack-plugin": "^3.2.0",
"webpack": "^4.29.4",
"webpack-cli": "^3.1.1",
"webpack-dev-server": "^3.1.0"
}
}

View File

@@ -0,0 +1,46 @@
use serde::{Deserialize, Serialize};
use wasm_bindgen::prelude::*;
// NOTE: This test is a clone of https://github.com/rustwasm/wasm-bindgen/blob/master/examples/fetch/src/lib.rs
// but uses Reqwest instead of the web_sys fetch api directly
/// A struct to hold some data from the github Branch API.
///
/// Note how we don't have to define every member -- serde will ignore extra
/// data when deserializing
#[derive(Debug, Serialize, Deserialize)]
pub struct Branch {
pub name: String,
pub commit: Commit,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Commit {
pub sha: String,
pub commit: CommitDetails,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct CommitDetails {
pub author: Signature,
pub committer: Signature,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Signature {
pub name: String,
pub email: String,
}
#[wasm_bindgen]
pub async fn run() -> Result<JsValue, JsValue> {
let res = reqwest::Client::new()
.get("https://api.github.com/repos/rustwasm/wasm-bindgen/branches/master")
.send()
.await?;
let text = res.text().await?;
let branch_info: Branch = serde_json::from_str(&text).unwrap();
Ok(JsValue::from_serde(&branch_info).unwrap())
}

View File

@@ -0,0 +1,25 @@
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const webpack = require('webpack');
const WasmPackPlugin = require("@wasm-tool/wasm-pack-plugin");
module.exports = {
entry: './index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'index.js',
},
plugins: [
new HtmlWebpackPlugin(),
new WasmPackPlugin({
crateDirectory: path.resolve(__dirname, ".")
}),
// Have this example work in Edge which doesn't ship `TextEncoder` or
// `TextDecoder` at this time.
new webpack.ProvidePlugin({
TextDecoder: ['text-encoding', 'TextDecoder'],
TextEncoder: ['text-encoding', 'TextEncoder']
})
],
mode: 'development'
};