Giter VIP home page Giter VIP logo

react-rest-dom's Introduction

React Rest DOM

This library allows to make requests to Rest APIs and render them easily in the DOM. It also allows rendering components in case there is an error and another while the request is loading.

ToDo

  • Rest context.
  • Rest client.
  • Authentication.
  • Error handler.
  • Loading handler.
  • Data handler.
  • Error with Data handler.
  • Cache.

How to use

1. Context

In order to use the library you must first create an execution context which will specify the URL of the API.

// index.js
import { RestContext } from "react-rest-dom";

ReactDOM.render(
  <React.StrictMode>
    <RestContext.Provider url="https://jsonplaceholder.typicode.com/" >
      <App />
    </RestContext.Provider>
  </React.StrictMode>,
  document.getElementById("root")
);

2. Authentication

In case of requiring authentication in the requests, it can be specified in the context.

// index.js
import { RestContext } from "react-rest-dom";

const authFactory = () => {
  const token = localStorage.getItem("token");
  return token ? "Bearer " + token : null;
};

ReactDOM.render(
  <React.StrictMode>
    <RestContext.Provider 
      url="https://jsonplaceholder.typicode.com/" 
      auth={authFactory}
     >
      <App />
    </RestContext.Provider>
  </React.StrictMode>,
  document.getElementById("root")
);

3. GET Request

Here is an example of how to make a GET request to an endpoint, in this case to "/todos" (from the API which url we have specified in the context)

// App.js or any component
import { RequestRenderer } from "react-rest-dom";

function App() {
  return (
    <RequestRenderer
      path="/todos"  // Slash (/) at the start is optional.
      onData={(data, statusCode /* Example: 200, 201, 400, 404, 500... */) =>
        data.map((item, index) => {
          return (
            <div id={index}>
              ID: {item.id} <br />
              Title: {item.title} <br />
              Completed: {item.completed}
              <hr />
            </div>
          );
        })
      }
    />
  );
}

4. POST Request

We can make a POST request declaring the method as a property of the component, we can do the same with the Body.

<RequestRenderer
  path="/todos"
  method="POST" // Can be any valid method, like PUT, PATCH, DELETE...
  body={{ yourBody: "content" }}
  onData={...}
/>;

5. Custom Headers

You can declare custom headers to send on request by passing them in the "headers" property in the component.

<RequestRenderer
  path="/todos"
  headers={{ myHeader: "Your Value" }}
  onData={...}
/>;

6. Loading Handling

You can render a component while the request is loading, for example a text, an animation or any type of component.

<RequestRenderer
  path="/todos"
  headers={{ myHeader: "Your Value" }}
  onLoading={() => (
    <h1>Loading...</h1>
  )}
  onData={...}
/>;

7. Error Handling

You can also render a component in case there is some kind of error when sending the request.

<RequestRenderer
  path="/todos"
  headers={{ myHeader: "Your Value" }}
  onError={(e) => (
    <h1>Error: { e.toString() } </h1>
  )}
  onData={...}
/>;

Contribute

I made this library to manage my projects so it is not well developed but it does the job. If you think it can improve, I invite you to create a pull request so we all benefit.

If you want to donate to the project you can do it through PayPal.

react-rest-dom's People

Contributors

ivyhjk avatar sammwyy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

ivyhjk

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.