UNPKG

redux-easy-async

Version:

Redux Easy Async makes handling asynchronous actions, such as API requests, simple, reliable, and powerful

76 lines (65 loc) 2.42 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.createAction = undefined; var _isError2 = require('lodash/isError'); var _isError3 = _interopRequireDefault(_isError2); var _get2 = require('lodash/get'); var _get3 = _interopRequireDefault(_get2); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * Action creator function that creates Flux Standard Actions (FSA) {@link https://github.com/acdlite/flux-standard-action} * @typedef {Object} actionCreator * @private * @param {Object} options options * @param {Object} [options.payload={}] payload for the action * @param {Object} [options.meta={}] meta for the action * @param {Object} [options.error=null] */ /** * Creates an action creator function * @param {string} type a string type for the action, e.g. `ADD_TODO` * @param {function} [payloadReducer] function which returns the payload from the arguments action * @param {function} [metaReducer] function which returns the meta from the arguments action * is called with. (Default: `args => args.meta`). * @return {actionCreator} an action creator function using the given type * @private * @example * // simple example * const addTodo = createAction('ADD_TODO'); * addTodo({ * payload: { text: 'Do something' }, * meta: { time: Date.now() }, * }) * // { * // type: 'ADD_TODO', * // payload: { text: 'Do something' }, * // meta: { time: 1490472864040 } * // } */ var createAction = exports.createAction = function createAction(type) { var payloadReducer = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function (args) { return (0, _get3.default)(args, 'payload'); }; var metaReducer = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : function (args) { return (0, _get3.default)(args, 'meta'); }; var actionCreator = function actionCreator() { var action = { type: type, payload: payloadReducer.apply(undefined, arguments), meta: metaReducer.apply(undefined, arguments) }; if ((0, _isError3.default)(action.payload)) action.error = true; return action; }; // TODO: this is just for backwards compatibility - will remove soon actionCreator.getType = function () { return type; }; actionCreator.toString = function () { return type; }; return actionCreator; };