Home

Awesome

⛳ @brigad/redux-rest-easy

Redux/React/React Native framework handling network requests, state management, selectors, caching and much more

CircleCI Codecov version downloads MIT License All Contributors PRs Welcome Code of Conduct code style: prettier semantic-release Star on GitHub

Release article

Installation

yarn add @brigad/redux-rest-easy

Or, if you are using npm:

npm install --save @brigad/redux-rest-easy

Problem

At Brigad, we have been extensively using redux and redux-thunk to perform network requests, and store/access the resulting data, and we always felt some pain points, or at least like there were things we could do better:

Solution

To solve the problems listed above, redux-rest-easy generates actions, reducers, and selectors, and also manages the state's data and metadata for your network requests. It is easy to use, and to observe via the Redux Devtools.

It also provides sensible defaults, allowing you to use it with almost no configuration, but also to customize anything you would like.

And the cherry on the top: it works seamlessly with redux-offline and redux-persist!

Scroll down for a small example, or browse the documentation to get started! To learn more about the problem and solution, you can also read the release article.

API

import {
  createResource,
  reducer,
  connect,
  reset,
  initializeNetworkHelpers,
  getPersistableState,
} from '@brigad/redux-rest-easy';

Internals

Core principles

  1. Preflight checks
  2. Actions
  3. Reducers
  4. Selectors

Minimal Example

// users.js

import { createResource } from '@brigad/redux-rest-easy';

const users = createResource('users')({
  retrieve: {
    method: 'GET',
    url: 'https://my-api.com/users',
    afterHook: () => console.log('Users retrieved successfully'),
  },
});

const {
  actions: { retrieve: retrieveUsers },
  selectors: {
    resource: { getResource: getUsers },
    retrieve: {
      request: { isPerforming: isRetrievingUsers },
    },
  },
} = users;

export { retrieveUsers, getUsers, isRetrievingUsers };
// reducers.js

import { reducer } from '@brigad/redux-rest-easy';

const reducers = combineReducers({
  restEasy: reducer,
});
// UsersList.js

import React, { Component } from 'react';
import { connect } from '@brigad/redux-rest-easy';
import {
  retrieveUsers,
  getUsers,
  isRetrievingUsers,
} from './redux-rest-easy/users';

class UsersList extends Component {
  state = {
    error: false,
  };

  componentDidMount() {
    this.props.retrieveUsers(this.onSuccess, this.onError);
  }

  onSuccess = () => {
    this.setState({ error: false });
  };

  onError = () => {
    this.setState({ error: true });
  };

  render() {
    if (this.props.isRetrievingUsers) {
      return <div>{'Loading...'}</div>;
    }

    if (this.state.error) {
      return (
        <div>{'There seems to be a problem... A network error occured.'}</div>
      );
    }

    return <Users items={this.props.users} />;
  }
}

const mapStateToProps = state => ({
  users: getUsers(state),
  isRetrievingUsers: isRetrievingUsers(state),
});

const mapDispatchToProps = dispatch => ({
  retrieveUsers: (onSuccess, onError) =>
    dispatch(retrieveUsers({ onSuccess, onError })),
});

export default connect(
  mapStateToProps,
  mapDispatchToProps,
)(ConnectedComponent);

Peer dependencies

Redux-rest-easy assumes you are using react (or react-native) and react-redux.

Redux-rest-easy also uses redux-thunk under the hood, to handle async actions, and therefore requires you to use redux-thunk's middleware in your store. If you are already using redux-thunk, then you have nothing more to do. Else, follow redux-thunk's docs for a quick setup.

Examples

Simple Example

Displays a list of users and allows to create new ones.

Pagination (TODO, coming soon)

Displays a paginated list, with seamless query-based selectors and cache.

Data invalidation (TODO, coming soon)

Invalidates store data after a successful POST request

Multiple requests (e.g. S3 signed upload) (TODO, coming soon)

Performs multiple requests in beforeHook before the final one, to upload a signed file to S3.

Cache hints (TODO, coming soon)

Makes use of cache hints to customize the built-in cache.

Store persistence (TODO, coming soon)

Introduces store persistence in the "Pagination" example, so that data persists after the page is refreshed.

Contributors

Thanks goes to these people (emoji key):

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --> <!-- prettier-ignore -->
<img src="https://avatars1.githubusercontent.com/u/15089053?v=4" width="100px;"/><br /><sub><b>Adrien HARNAY</b></sub><br />📝 💻 📖 🤔 🚇 👀 ⚠️<img src="https://avatars1.githubusercontent.com/u/6181446?v=4" width="100px;"/><br /><sub><b>Thibault Malbranche</b></sub><br />🐛 💻 🤔 👀<img src="https://avatars3.githubusercontent.com/u/11462388?v=4" width="100px;"/><br /><sub><b>Grisha Ghukasyan</b></sub><br />🤔<img src="https://avatars1.githubusercontent.com/u/569243?v=4" width="100px;"/><br /><sub><b>Aymeric Beaumet</b></sub><br />🤔<img src="https://avatars3.githubusercontent.com/u/3433722?v=4" width="100px;"/><br /><sub><b>Jess</b></sub><br />🐛 📖<img src="https://avatars2.githubusercontent.com/u/296106?v=4" width="100px;"/><br /><sub><b>Matt Labrum</b></sub><br />💻
<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the all-contributors specification. Contributions of any kind welcome!