UNPKG

@e-group/redux-modules

Version:

eGroup team react-redux modules that share across projects.

75 lines (59 loc) 2.03 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); var _actions = require("../apis/actions"); var _utils = require("../utils"); /** * Use to dispatch fetch actions automatically. */ function createHandleApisMiddleware() { return ({ dispatch }) => next => action => { const isObject = typeof action === 'object' && !Array.isArray(action); if (isObject && action.type && typeof action.type === 'string') { if (action.type.indexOf('FETCH') !== -1 || action.type.indexOf('fetch') !== -1) { const leafs = (0, _utils.camalize)(action.type).split('/'); const fetchIndex = (0, _utils.findFetchIndex)(leafs); const _getApiInfos = (0, _utils.getApiInfos)(leafs[fetchIndex]), _getApiInfos2 = (0, _slicedToArray2.default)(_getApiInfos, 2), apiType = _getApiInfos2[1]; const trimedLeafs = (0, _utils.trimLeafs)(leafs, fetchIndex); if (!apiType) { dispatch((0, _actions.egApiTake)({ leafs: trimedLeafs })); } if (apiType === 'request') { dispatch((0, _actions.egApiRequest)({ leafs: trimedLeafs })); } if (apiType === 'cancel') { dispatch((0, _actions.egApiCancel)({ leafs: trimedLeafs })); } if (apiType === 'success') { dispatch((0, _actions.egApiSuccess)({ leafs: trimedLeafs, response: action.payload })); } if (apiType === 'failure') { dispatch((0, _actions.egApiFailure)({ leafs: trimedLeafs, error: action.payload })); } } } return next(action); }; } var _default = createHandleApisMiddleware; exports.default = _default;