npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-rest-dom

v0.1.1

Published

Library that allows you to consume Rest APIs through the React DOM.

Downloads

7

Readme

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

  • [x] Rest context.
  • [x] Rest client.
  • [x] Authentication.
  • [x] Error handler.
  • [x] Loading handler.
  • [x] 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.