UNPKG

@e-group/utils

Version:

eGroup team utils that share across projects.

68 lines (55 loc) 3.36 kB
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties"; function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } import { ofType } from 'redux-observable'; import { throwError, of, concat } from 'rxjs'; import { switchMap, catchError, debounceTime, tap } from 'rxjs/operators'; import findDeepValue from '../findDeepValue'; import createObservableApi from '../createObservableApi'; export default function makeBasicFetchEpic({ actionType, apiName, fetchRequest, handleSuccess, handleFailure, debounceTime: time, observableMap: customized$Map, handleTakeUntil, handleBeforeFetch, handleAfterFetch }) { const observableMap = customized$Map || switchMap; const basicFetchEpic = (action$, state$, dependencies = {}) => action$.pipe(ofType(actionType), debounceTime(time !== null && time !== void 0 ? time : 0), observableMap(action => { const beforeFetch = handleBeforeFetch ? handleBeforeFetch(action, dependencies) : of(); const afterFetch = handleAfterFetch ? handleAfterFetch(action, dependencies) : of(); const cancelRequest = handleTakeUntil ? handleTakeUntil(action$) : tap(() => {}); // we can concat observable actions // see this comment https://github.com/redux-observable/redux-observable/issues/62#issuecomment-266337873 const apis = dependencies.apis; if (!apis) { return throwError(new Error('makeBasicFetchEpic need setup apis dependency.')); } const api = findDeepValue(apis, apiName); if (!api) { return throwError(new Error("".concat(apiName, " is missing in apis dependency."))); } let apiPayload = action.payload; if (action.payload && typeof action.payload === 'object' && !Array.isArray(action.payload)) { // Destructuring `callback` automatically it's used to execute method after api finish. const _action$payload = action.payload, callback = _action$payload.callback, payload = _objectWithoutProperties(_action$payload, ["callback"]); apiPayload = payload; } return concat(beforeFetch, of(fetchRequest()), createObservableApi(api(apiPayload)).pipe(switchMap(response => handleSuccess(response, _objectSpread({ action$, state$, action }, dependencies))), cancelRequest, catchError(error => handleFailure(error, _objectSpread({ action$, state$, action }, dependencies)))), afterFetch); })); return basicFetchEpic; }