No more rest πŸš€

epavanello

Emanuele Pavanello

Posted on February 15, 2021

No more rest πŸš€

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"];
}

Enter fullscreen mode Exit fullscreen mode
// 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);
Enter fullscreen mode Exit fullscreen mode
  • 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"
}
Enter fullscreen mode Exit fullscreen mode
  • 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");
  });

Enter fullscreen mode Exit fullscreen mode
πŸ’– πŸ’ͺ πŸ™… 🚩
epavanello
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.

Related