redux-easy-async
Version:
Redux Easy Async makes handling asynchronous actions, such as API requests, simple, reliable, and powerful
57 lines (48 loc) • 2.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getOrCreateAsyncConstants = exports.isValidAsyncConstant = exports.createAsyncConstants = undefined;
var _every2 = require('lodash/every');
var _every3 = _interopRequireDefault(_every2);
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Creates an object with constant keys `NAME`, `START_TYPE`, `SUCCESS_TYPE`, `FAIL_TYPE` in the
* format that {@link createAsyncAction}, {@link createAsyncReducer}, and
* {@link createAsyncReducer} accept. **Note:** this is an extra optional step for those that
* prefer to separate action creator definitions from constants. If you don't know why or care then
* skip this and use only {@link createSingleAsyncAction} to create your actions.
* @kind function
* @param {string} type - the base name for this constant, e.g. `"GET_USER"`
* @return {object} returns an object with keys: `NAME`, `START_TYPE`, `SUCCESS_TYPE`, and
* `FAIL_TYPE`
* @example
* const GET_USER = createAsyncConstants('GET_USER');
* // {
* // NAME: 'GET_USER', // general name for action
* // START_TYPE: 'START_GET_USER', // start type of the this async action
* // SUCCESS_TYPE: 'SUCCESS_GET_USER', // success type of the this async action
* // FAIL_TYPE: 'FAIL_GET_USER' // fail type of the this async action
* // }
*/
var createAsyncConstants = exports.createAsyncConstants = function createAsyncConstants(type) {
return {
NAME: type,
START_TYPE: 'START_' + type,
SUCCESS_TYPE: 'SUCCESS_' + type,
FAIL_TYPE: 'FAIL_' + type
};
};
var requiredKeys = ['NAME', 'START_TYPE', 'FAIL_TYPE', 'SUCCESS_TYPE'];
var isValidAsyncConstant = exports.isValidAsyncConstant = function isValidAsyncConstant(obj) {
if (['object', 'function'].indexOf(typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) === -1) return false;
return (0, _every3.default)(requiredKeys, function (requiredKey) {
var objKey = obj[requiredKey];
return typeof objKey === 'string' && objKey.length > 0;
});
};
var getOrCreateAsyncConstants = exports.getOrCreateAsyncConstants = function getOrCreateAsyncConstants(type) {
if (typeof type === 'string' && type.length) return createAsyncConstants(type);else if (isValidAsyncConstant(type)) return type;
return null;
};