Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/sammwyy/react-rest-dom

Library that allows you to consume Rest APIs through the React DOM.
https://github.com/sammwyy/react-rest-dom

api api-client api-rest client react react-api react-dom react-rest reactjs rest rest-api rest-client restful restful-api

Last synced: 2 months ago
JSON representation

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

Awesome Lists containing this project

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.

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

ReactDOM.render(




,
document.getElementById("root")
);
```

### 2. Authentication

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

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

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

ReactDOM.render(




,
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)

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

function App() {
return (

data.map((item, index) => {
return (


ID: {item.id}

Title: {item.title}

Completed: {item.completed}



);
})
}
/>
);
}
```

### 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.

```javascript
;

```

### 5. Custom Headers

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

```javascript
;
```

### 6. Loading Handling

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

```javascript
(

Loading...


)}
onData={...}
/>;
```

### 7. Error Handling

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

```javascript
(

Error: { e.toString() }


)}
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](https://paypal.me/sammwy).