JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • 0
  • Score
    100M100P100Q39910F
  • License MIT

Writing asynchronous actions in synchronous style

Package Exports

  • redux-sync-promise

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (redux-sync-promise) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

redux-sync-promise

Build Status NPM version Downloads Coverage Status

Middleware for writing asynchronous actions in synchronous style

Installation

$ npm install --save redux-sync-promise

Usage

Add middleware

import { APISync } from 'redux-sync-promise';

const api = APISync({/* options */});
const createMiddleware = applyMiddleware(thunk, api);
const store = createMiddleware(createStore)(reducer);

Action Examples

Every action will dispatch type name from types plus postfix.

// This example will dispatch PEOPLE_PENDING, PEOPLE_SUCCESS or PEOPLE_FAILURE
/*
 * Full example
 * */
export function getPeopleList(contry, age) {
  return {
    types: 'PEOPLE',
    data: [contry, age], // this data will add in all functions like arguments
    name: getPeopleName,
    work: getPeopleWork
    food: getPeopleFood
  }
}

/*
 * Get people info
 * @param {object} state - store state
 * @param {function} dispatch - action dispatch function
 * @param {array} props - an array of props in the data key in the action
 * */
async function getPeopleName(state, dispatch, props) {
  let {people: {entriesOnPage}} = state;
  let _requestString = `investors/?rows=${entriesOnPage}`;

  const {data: {people, total}} = await fetch(_requestString);
  return {people, total};
}
// and so on ...

/*
 * Simple example
 * */
export function getUnicornList() {
  return {
    types: 'UNICORN',
    list: async (state, dispatch, props) => {
      let rainbow = await getRainbow();
      return rainbow.data.colors
    }
  }
}
// This example will dispatch UNICORN_PENDING, UNICORN_SUCCESS or UNICORN_FAILURE

API

APISync exposes single constructor function for creating middleware.

APISync( options: Object )

// Full example
APISync({
    postfix: {
        pending: 'IS_PENDING',
        success: 'IS_SUCCESS'
        failure: 'IS_FAILURE'
    },
    onPending: (dispatch, data) => {
        console.log('some action api panding');
    },
    onSuccess: (dispatch, result, data) => {
        console.log('some action api success');
    },
    onError: (dispatch, error, data) => {
        // example use on errors
        if(err.status === 401)
            dispatch({type: 'LOGOUT_SUCCESS'});

        console.log('some action api error');
    }
})

postfix

Add your custom action type postfix for API call.

Default: PENDING, SUCCESS, FAILURE

onPending

Callback when actions in progress

onSuccess

Callback on success

onError

Callback on error

License

Copyright © 2016 Alexander Dukhovnyak

Released under the MIT license. See license for details.