Giter VIP home page Giter VIP logo

wrpc's Introduction

wRPC (WASM RPC)

Make your server side axum APIs consumable from WASM. This crate introduces a simple attribute macro that transforms your API handler's signature into a fully typed client side request function that calls the API endpoint. Inspired by tRPC.

CI

Quick Usage

#[rpc(get("/api/user/:id"))]
pub async fn get_user(Path(id): Path<u32>) -> Json<User> {
    // Do things here
    Json(User)
}

This will gate the handler to only exist on non-WASM targets and create a WASM side function somewhat like this:

pub async fn get_user(id: u32) -> Result<User, reqwasm::Error> {
    reqwasm::http::Request::get(&format!("/api/user/{id}"))
        .send()
        .await?
        .json()
        .await
}

Configuration

  • get(path) - Specifiy this handler's path relative to the root of your API. Extracted path segments are prefixed with :, i.e. :id.
  • returns(Type) - Specify an overriding return type for your client side function. This must be either String or a deserializable type. It's mostly useful for handlers that return status codes or have an otherwise more complex return type.

Requirements

  • Path inputs with multiple segments must be destructured. This is because the macro separates these parameters into separate arguments to the client side function and needs their names.
  • Text body inputs must be Strings
  • All request-derived inputs must be Json, Query, Path or String. Any other arguments are assumed to be state derived and skipped.
  • The return type must be Json or String/&str. &str will be turned into String on the client side.
  • The full path to the API handler must be specified. wrpc currently can't have access to your Router, so paths are unknown to the macro.

Kitchen Sink Example

use wrpc::rpc;

struct UpdateQuery {
   force: bool
}

#[rpc(post("/api/user/:id/update"), returns(User))]
pub async fn update_user(
    Path(id): Path<u32>,
    Query(settings): Query<UpdateQuery>,
    Json(new_user): Json<User>
) -> impl IntoResponse {
    // Do actual things
    (StatusCode::OK, Json(User))
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.