2 releases

0.1.1 Aug 20, 2021
0.1.0 Aug 20, 2021

#512 in WebAssembly

Download history 23/week @ 2024-03-11 22/week @ 2024-03-18 16/week @ 2024-03-25 60/week @ 2024-04-01 3/week @ 2024-04-08 14/week @ 2024-04-15 25/week @ 2024-04-22 10/week @ 2024-04-29 9/week @ 2024-05-06 14/week @ 2024-05-13 10/week @ 2024-05-20 20/week @ 2024-05-27 20/week @ 2024-06-03 21/week @ 2024-06-10 15/week @ 2024-06-17 22/week @ 2024-06-24

80 downloads per month

MIT license

14KB
188 lines

WASM Request

Create http request with rustwasm saving boilerplate

How to use

Include in Cargo.toml

wasm_request = '0.1'

Create http request

#[derive(Serialize, Deserialize, Debug, Clone)]
struct Eur {
    eur: f64,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
struct CurrentPrice {
    current_price: Eur,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
struct Price {
    market_data: CurrentPrice,
}

async fn get_token_price() -> Price {
    let options = get_options::<Price>(
        "https://api.coingecko.com/api/v3/coins/1million-token",
        Method::Get,
        None,
        None,
    );

    let price_1mt_token: Price = request(options).await.unwrap().into_serde().unwrap();

    price_1mt_token
}

Post with body

use wasm_request::{get_options, request, Method, DataType};

#[derive(Serialize, Deserialize, Debug)]
pub struct User {
    pub email: String,
    pub username: String,
    pub password: String,
}

async fn create_user() {
  let user = User {
    email: "test@example.com".to_string(),
    password: "test43214".to_string(),
  }

  let options = get_options::<User>(
    "https://api.sport-example.com/login",
    Method::Post,
    None,
    Some(DataType::Json(user)),
  );

  request(options).await.unwrap().into_serde().unwrap();  
}

Post with form data

use wasm_request::{get_options, request, Method, DataType, get_defualt_headers};
use web_sys::FormData;

async fn add_form() {
  let form = FormData::new().unwrap();
  form.append_with_str("name", "Lee").unwrap();
  form.append_with_str("email", "test.example@gmail.com").unwrap();

  let headers = get_defualt_headers();
  headers.delete("Content-Type").unwrap();

  let options = get_options::<()>(
    "https://api.sport-example.com/user",
    Method::Post,
    None,
    Some(DataType::Form(form)),
  );

  request(options).await.unwrap().into_serde().unwrap();
}

Manage Local Storage

set_storage("food", "ramen");
let food = get_storage("food").unwrap();
delete_storage("food");

Run tests

wasm-pack test --headless --chrome --firefox

Do you like Wasm Request

If you like Yew Styles, help me supporting the project:

Wasm Request is MIT licensed

Dependencies

~7–10MB
~187K SLoC