Skip to content

Spiking new hypotheses before doing it in complex code bases

Notifications You must be signed in to change notification settings

pragmaxim-com/openapi-wasm-client

Repository files navigation

Rest-api with wasm client library

Actix-web rest-api with 2 generated client libraries from OpenAPI specification :

and one hand-written client using shared model with the server.

Building OpenAPI

First we build our openapi.json specification which will later be served at http://127.0.0.1:8082/swagger :

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.0/install.sh | bash
nvm install 20
npm install --global yarn
npm install --global @redocly/cli@latest

yarn bundle

Hand made reqwest-client

reqwest-client is a hand-written client with wasm-pack generated javascript.

# install web-pack
curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh

cd reqwest-client # native target is built in root
wasm-pack build --target web

Generating web client with Progenitor

cd progenitor
wasm-pack build --target web

Generating web client with OpenAPI generator

Advantage of OpenAPI generator is that it can generate even tests for the endpoints.

cd openapi-gen

java -jar openapi-generator-cli.jar generate \
    -i ../openapi.json \
    -g rust \
    -o ./rust \
    --additional-properties=packageName=openapi-gen,library=reqwest

cd rust
wasm-pack build --target web

Running Actix-web http server

Server handles requests from all 3 types of clients, handwritten, progenitor and openapi-gen :

.service(fs::Files::new("/reqwest-client", "./reqwest-client").index_file("index.html"))
.service(fs::Files::new("/progenitor", "./progenitor").index_file("index.html"))
.service(fs::Files::new("/openapi-gen", "./openapi-gen/rust").index_file("index.html"))
cargo run
curl -X POST http://127.0.0.1:8082/blocks -H "Content-Type: application/json" -d '{"block_id":"frog", "height":1}'
curl -X POST http://127.0.0.1:8082/addresses -H "Content-Type: application/json" -d '{"address":"croco", "balance":2}'
curl http://127.0.0.1:8082/blocks
curl http://127.0.0.1:8082/addresses

Now you can access :

About

Spiking new hypotheses before doing it in complex code bases

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published