No more rest π
Emanuele Pavanello
Posted on February 15, 2021
Hello guys!
Here the GitHub Repo and here the NPM package π¦
If you, like me, don't like to pass all day defining new rest API and calling them from the frontend in a verbose way, you can start to use my new NPM package no-more-rest that allows you to expose your API directly from your server to be called transparently from your client with the IntelliSense support.
If you like the idea star the project and collaborate with me π
A small example of the potential
// server/myApi.js
export function doLogin(username, password) {
return username == "admin" && password == "admin";
}
export function getLoggedUsers() {
return ["Elon Musk", "admin"];
}
// server/server.js
import express from "express";
import { expose } from "no-more-rest";
import * as myApi from "./myApi";
const app = express();
expose(app, myApi);
app.listen(8000);
- Add this npm script to your package to generate the proxy script for the client from the exposed module
"scripts": {
"sync-api": "no-more-rest --input myApi.js --output-dir ../your-client-path/ --watch"
}
- Import in the client your generated proxy and use it as if it were on your backend.
// client/index.js
import { doLogin, getLoggedUsers } from "./generatedProxy";
doLogin("admin", "admin")
.then((result) => {
if (result) {
alert("Login success");
getLoggedUsers().then((users) => {
alert("The logged users are: " + users.join(", "));
});
} else {
alert("Login failed");
}
})
.catch(() => {
alert("Network error");
});
π πͺ π
π©
Emanuele Pavanello
Posted on February 15, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.