Example #1
0
export default store => next => action => {
    const apiCall = action[API_CALL];

    if (typeof apiCall === 'undefined') {
        return next(action)
    }

    let { endpoint, method, types } = apiCall;

    if (typeof endpoint !== 'string') {
        throw new Error('Specify a string endpoint URL.');
    }

    if (!Array.isArray(types) || types.length !== 3) {
        throw new Error('Expected an array of three action types.');
    }
    if (!types.every(type => typeof type === 'string')) {
        throw new Error('Expected action types to be strings.');
    }

    const [ requestType, successType, failureType ] = types;

    next(createAction(requestType)());

    return callApi(endpoint, method, action.payload).then(
        response => {
            return next(createAction(successType)(response))
        },
        error => {
            return next(createAction(failureType)(error.message || 'Something bad happened'))
        }
    );
}
Example #2
0
import actionTypes from '../consts/article'
import Model from '../models/article'
import createAction from 'redux-actions/lib/createAction'

export const postArticle = createAction(
  actionTypes.POST_ARTICLE,
  (options) => {
    return new Model()
      .addPaths(['{category}', 'news'])
      .replace({
        category: 123
      })
      .POST({
        data: options.data
      })
  }
)
Example #3
0
 error => {
     return next(createAction(failureType)(error.message || 'Something bad happened'))
 }
Example #4
0
 response => {
     return next(createAction(successType)(response))
 },