redux-repository

A versatile set of pure functions to simplify the management of remote resources with Redux


Keywords
redux, repository
License
MIT
Install
npm install redux-repository@0.6.3

Documentation

redux-repository

NpmVersion Build Status Coverage Status dependencies Status devDependencies Status peerDependencies Status Greenkeeper badge

Quick start

Install

npm install --save redux-repository

Use

TODO: Update

Actions

import { createFetchResource } from 'redux-repository/lib/actions';

export const fetchMyResource = (id) => createFetchResource(
  'myResource',
  id,
  (state) => state.myRepository,
  (dispatchReceived, dispatchFailed) => {
    // Your custom logic to fetch the resource.
    fetch(`https://example.com/my-resources/${id}`)
      .then(data => dispatchReceived(data))
      .catch(error => dispatchFailed(error.toString()));
  },
  { ttl: 60 * 60 * 1000 },
);

fetchMyResource here is a simple action creator that you can use as usual.

Reducer

import { isResourceAction, repositoryReducer } from 'redux-repository/lib/reducer';
import { createInitialState } from 'redux-repository/lib/repository';

const initialState = {
  // ...
  myRepository: createInitialState(),
  // ...
};

export default (state = initialState, action) => {
  if (isResourceAction('myResource', action)) {
    return {
      ...state,
      myRepository: repositoryReducer(state.myRepository, action),
    };
  }

  switch (action.type) {
  // ...
    default:
      return state;
  }
};

Having different names for resources (myResource here) helps to support different resources.

Repository

import {
  getResourceById,
  getResourcesArrayByIds,
  pushResource,
  pushResourcesArray,
} from 'redux-repository/lib/repository';

Resource

import {
  createFailed,
  createReceived,
  createRequested,
  extractData,
  extractError,
  isExpired,
  isFailed,
  isReceived,
  isRequested,
} from 'redux-repository/lib/resource';

Flow types

import type {
  ActionType,
  FetchResourceOptionsType,
  ResourceIdType,
  ResourseFailedType,
  ResourseReceivedType,
  ResourceRequestedType,
  ResourceType,
  RepositoryType,
} from 'redux-repository/lib/flowTypes';

Contribution

Please use the dev branch and feel free to contribute!