@3plus/redux-api 1.0.5 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,5 @@
1
1
  "use strict";
2
2
 
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports["default"] = _default;
7
3
  function processData(data) {
8
4
  try {
9
5
  return JSON.parse(data);
@@ -20,27 +16,23 @@ function toJSON(resp) {
20
16
  return Promise.resolve(resp).then(processData);
21
17
  }
22
18
  }
23
- function _default(fetch) {
24
- return function (url, opts) {
25
- return fetch(url, opts).then(function (resp) {
26
- // Normalize IE9's response to HTTP 204 when Win error 1223.
27
- var status = resp.status === 1223 ? 204 : resp.status;
28
- var statusText = resp.status === 1223 ? "No Content" : resp.statusText;
29
- if (status >= 400) {
30
- return Promise.reject({
31
- status: status,
32
- statusText: statusText
33
- });
34
- } else {
35
- return toJSON(resp).then(function (data) {
36
- if (status >= 200 && status < 300) {
37
- return data;
38
- } else {
39
- return Promise.reject(data);
40
- }
41
- });
42
- }
43
- });
44
- };
45
- }
46
- module.exports = exports.default;
19
+ export default function (fetch) {
20
+ return (url, opts) => fetch(url, opts).then(resp => {
21
+ const status = resp.status === 1223 ? 204 : resp.status;
22
+ const statusText = resp.status === 1223 ? "No Content" : resp.statusText;
23
+ if (status >= 400) {
24
+ return Promise.reject({
25
+ status,
26
+ statusText
27
+ });
28
+ } else {
29
+ return toJSON(resp).then(data => {
30
+ if (status >= 200 && status < 300) {
31
+ return data;
32
+ } else {
33
+ return Promise.reject(data);
34
+ }
35
+ });
36
+ }
37
+ });
38
+ }
package/lib/async.js CHANGED
@@ -1,39 +1,17 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports["default"] = async;
7
- /**
8
- *
9
- * @param {[type]} dispatch [description]
10
- * @param {...[type]} args [description]
11
- * @return {[type]} [description]
12
- * @example
13
- * async(dispatch,
14
- * cb=> actions.test(1, cb),
15
- * actions.test2
16
- * ).then(()=> async(dispatch, actions.test3))
17
- */
18
- function async(dispatch) {
19
- var currentFunction = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
20
- for (var _len = arguments.length, restFunctions = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
21
- restFunctions[_key - 2] = arguments[_key];
22
- }
23
- return new Promise(function (resolve, reject) {
1
+ export default function async(dispatch, currentFunction = null, ...restFunctions) {
2
+ return new Promise((resolve, reject) => {
24
3
  if (!currentFunction) {
25
4
  reject("no chain function");
26
5
  } else {
27
- dispatch(currentFunction(function (err, data) {
6
+ dispatch(currentFunction((err, data) => {
28
7
  err ? reject(err) : resolve(data);
29
8
  }) || {});
30
9
  }
31
- }).then(function (data) {
10
+ }).then(data => {
32
11
  if (restFunctions.length) {
33
- return async.apply(void 0, [dispatch].concat(restFunctions));
12
+ return async(dispatch, ...restFunctions);
34
13
  } else {
35
14
  return data;
36
15
  }
37
16
  });
38
- }
39
- module.exports = exports.default;
17
+ }
@@ -1,14 +1,10 @@
1
1
  "use strict";
2
2
 
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports["default"] = _default;
7
- function _default() {
8
- var data;
9
- var hasData = false;
3
+ export default function () {
4
+ let data;
5
+ let hasData = false;
10
6
  return {
11
- set: function set(val) {
7
+ set(val) {
12
8
  if (!hasData) {
13
9
  data = val;
14
10
  hasData = true;
@@ -16,17 +12,16 @@ function _default() {
16
12
  }
17
13
  return false;
18
14
  },
19
- empty: function empty() {
15
+ empty() {
20
16
  return !hasData;
21
17
  },
22
- pop: function pop() {
18
+ pop() {
23
19
  if (hasData) {
24
20
  hasData = false;
25
- var result = data;
21
+ const result = data;
26
22
  data = null;
27
23
  return result;
28
24
  }
29
25
  }
30
26
  };
31
- }
32
- module.exports = exports.default;
27
+ }
@@ -1,20 +1,10 @@
1
1
  "use strict";
2
2
 
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports["default"] = fetchResolver;
7
3
  function none() {}
8
- function fetchResolver() {
9
- var index = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
10
- var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
11
- var cb = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : none;
4
+ export default function fetchResolver(index = 0, opts = {}, cb = none) {
12
5
  if (!opts.prefetch || index >= opts.prefetch.length) {
13
6
  cb();
14
7
  } else {
15
- opts.prefetch[index](opts, function (err) {
16
- return err ? cb(err) : fetchResolver(index + 1, opts, cb);
17
- });
8
+ opts.prefetch[index](opts, err => err ? cb(err) : fetchResolver(index + 1, opts, cb));
18
9
  }
19
- }
20
- module.exports = exports.default;
10
+ }
package/lib/helpers.js CHANGED
@@ -1,30 +1,8 @@
1
- "use strict";
2
-
3
- function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
4
- Object.defineProperty(exports, "__esModule", {
5
- value: true
6
- });
7
- exports.CRUD = void 0;
8
- exports.defaultMiddlewareArgsParser = defaultMiddlewareArgsParser;
9
- exports.extractArgs = extractArgs;
10
- exports.helperCrudFunction = helperCrudFunction;
11
- exports.none = none;
12
- function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
13
- function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
14
- function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
15
- function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
16
- function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
17
- function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
18
- function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
19
- function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
20
- function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
21
- function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
22
- function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
23
- function none() {}
24
- function extractArgs(args) {
25
- var pathvars;
26
- var params = {};
27
- var callback;
1
+ export function none() {}
2
+ export function extractArgs(args) {
3
+ let pathvars;
4
+ let params = {};
5
+ let callback;
28
6
  if (args[0] instanceof Function) {
29
7
  callback = args[0];
30
8
  } else if (args[1] instanceof Function) {
@@ -37,28 +15,22 @@ function extractArgs(args) {
37
15
  }
38
16
  return [pathvars, params, callback];
39
17
  }
40
- function helperCrudFunction(name) {
41
- return function () {
42
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
43
- args[_key] = arguments[_key];
44
- }
45
- var _extractArgs = extractArgs(args),
46
- _extractArgs2 = _slicedToArray(_extractArgs, 3),
47
- pathvars = _extractArgs2[0],
48
- params = _extractArgs2[1],
49
- cb = _extractArgs2[2];
50
- return [pathvars, _objectSpread(_objectSpread({}, params), {}, {
18
+ export function helperCrudFunction(name) {
19
+ return (...args) => {
20
+ const [pathvars, params, cb] = extractArgs(args);
21
+ return [pathvars, {
22
+ ...params,
51
23
  method: name.toUpperCase()
52
- }), cb];
24
+ }, cb];
53
25
  };
54
26
  }
55
- function defaultMiddlewareArgsParser(dispatch, getState) {
27
+ export function defaultMiddlewareArgsParser(dispatch, getState) {
56
28
  return {
57
- dispatch: dispatch,
58
- getState: getState
29
+ dispatch,
30
+ getState
59
31
  };
60
32
  }
61
- var CRUD = exports.CRUD = ["get", "post", "put", "delete", "patch"].reduce(function (memo, name) {
33
+ export const CRUD = ["get", "post", "put", "delete", "patch"].reduce((memo, name) => {
62
34
  memo[name] = helperCrudFunction(name);
63
35
  return memo;
64
36
  }, {});
package/lib/index.js CHANGED
@@ -1,72 +1,16 @@
1
1
  "use strict";
2
-
3
- /* eslint no-void: 0 */
4
- Object.defineProperty(exports, "__esModule", {
5
- value: true
6
- });
7
- exports["default"] = reduxApi;
8
- var _reducerFn = _interopRequireDefault(require("./reducerFn"));
9
- var _actionFn = _interopRequireDefault(require("./actionFn"));
10
- var _transformers = _interopRequireDefault(require("./transformers"));
11
- var _async = _interopRequireDefault(require("./async"));
12
- var _cache = _interopRequireDefault(require("./utils/cache"));
13
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
14
- function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
15
- function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
16
- function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
17
- function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
18
- function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
19
- function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
20
- // export { transformers, async };
21
-
22
- /**
23
- * Default configuration for each endpoint
24
- * @type {Object}
25
- */
26
- var defaultEndpointConfig = {
27
- transformer: _transformers["default"].object
2
+ import reducerFn from "./reducerFn";
3
+ import actionFn from "./actionFn";
4
+ import transformers from "./transformers";
5
+ import cacheManager from "./utils/cache";
6
+ export { transformers };
7
+ const defaultEndpointConfig = {
8
+ transformer: transformers.object
28
9
  };
29
- var PREFIX = "@@redux-api";
30
- /**
31
- * Entry api point
32
- * @param {Object} config Rest api configuration
33
- * @param {Object} baseConfig baseConfig settings for Rest api
34
- * @param {Function} fetch Adapter for rest requests
35
- * @param {Boolean} isServer false by default (fif you want to use it for isomorphic apps)
36
- * @return {actions, reducers} { actions, reducers}
37
- * @example ```js
38
- * const api = reduxApi({
39
- * test: "/plain/url",
40
- * testItem: "/plain/url/:id",
41
- * testModify: {
42
- * url: "/plain/url/:endpoint",
43
-
44
- * transformer: (data)=> !data ?
45
- * { title: "", message: "" } :
46
- * { title: data.title, message: data.message },
47
- * options: {
48
- * method: "post"
49
- * headers: {
50
- * "Accept": "application/json",
51
- * "Content-Type": "application/json"
52
- * }
53
- * }
54
- * }
55
- * });
56
- * // register reducers
57
- *
58
- * // call actions
59
- * dispatch(api.actions.test());
60
- * dispatch(api.actions.testItem({id: 1}));
61
- * dispatch(api.actions.testModify({endpoint: "upload-1"}, {
62
- * body: JSON.stringify({title: "Hello", message: "World"})
63
- * }));
64
- * ```
65
- */
66
-
67
- function reduxApi(config, baseConfig) {
10
+ const PREFIX = "@@redux-api";
11
+ export default function reduxApi(config, baseConfig) {
68
12
  config || (config = {});
69
- var fetchHolder = {
13
+ const fetchHolder = {
70
14
  fetch: null,
71
15
  server: false,
72
16
  rootUrl: null,
@@ -74,15 +18,12 @@ function reduxApi(config, baseConfig) {
74
18
  options: {},
75
19
  responseHandler: null
76
20
  };
77
- var cfg = {
78
- use: function use(key, value) {
21
+ const cfg = {
22
+ use(key, value) {
79
23
  fetchHolder[key] = value;
80
24
  return this;
81
25
  },
82
- init: function init(fetch) {
83
- var isServer = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
84
- var rootUrl = arguments.length > 2 ? arguments[2] : undefined;
85
- /* eslint no-console: 0 */
26
+ init(fetch, isServer = false, rootUrl) {
86
27
  console.warn("Deprecated method, use `use` method");
87
28
  this.use("fetch", fetch);
88
29
  this.use("server", isServer);
@@ -94,89 +35,85 @@ function reduxApi(config, baseConfig) {
94
35
  events: {}
95
36
  };
96
37
  function fnConfigCallback(memo, value, key) {
97
- var opts = _typeof(value) === "object" ? _objectSpread(_objectSpread({}, defaultEndpointConfig), {}, {
98
- reducerName: key
99
- }, value) : _objectSpread(_objectSpread({}, defaultEndpointConfig), {}, {
38
+ const opts = typeof value === "object" ? {
39
+ ...defaultEndpointConfig,
40
+ reducerName: key,
41
+ ...value
42
+ } : {
43
+ ...defaultEndpointConfig,
100
44
  reducerName: key,
101
45
  url: value
102
- });
46
+ };
103
47
  if (opts.broadcast !== void 0) {
104
- /* eslint no-console: 0 */
105
48
  console.warn("Deprecated `broadcast` option. you shoud use `events`" + "to catch redux-api events (see https://github.com/3plus/redux-api/blob/master/DOCS.md#Events)");
106
49
  }
107
- var url = opts.url,
108
- urlOptions = opts.urlOptions,
109
- options = opts.options,
110
- transformer = opts.transformer,
111
- broadcast = opts.broadcast,
112
- crud = opts.crud,
113
- reducerName = opts.reducerName,
114
- prefetch = opts.prefetch,
115
- postfetch = opts.postfetch,
116
- validation = opts.validation,
117
- helpers = opts.helpers;
118
- var prefix = baseConfig && baseConfig.prefix || "";
119
- var ACTIONS = {
120
- actionFetch: "".concat(PREFIX, "@").concat(prefix).concat(reducerName),
121
- actionSuccess: "".concat(PREFIX, "@").concat(prefix).concat(reducerName, "_success"),
122
- actionFail: "".concat(PREFIX, "@").concat(prefix).concat(reducerName, "_fail"),
123
- actionReset: "".concat(PREFIX, "@").concat(prefix).concat(reducerName, "_delete"),
124
- actionCache: "".concat(PREFIX, "@").concat(prefix).concat(reducerName, "_cache"),
125
- actionAbort: "".concat(PREFIX, "@").concat(prefix).concat(reducerName, "_abort")
50
+ const {
51
+ url,
52
+ urlOptions,
53
+ options,
54
+ transformer,
55
+ broadcast,
56
+ crud,
57
+ reducerName,
58
+ prefetch,
59
+ postfetch,
60
+ validation,
61
+ helpers
62
+ } = opts;
63
+ const prefix = baseConfig && baseConfig.prefix || "";
64
+ const ACTIONS = {
65
+ actionFetch: `${PREFIX}@${prefix}${reducerName}`,
66
+ actionSuccess: `${PREFIX}@${prefix}${reducerName}_success`,
67
+ actionFail: `${PREFIX}@${prefix}${reducerName}_fail`,
68
+ actionReset: `${PREFIX}@${prefix}${reducerName}_delete`,
69
+ actionCache: `${PREFIX}@${prefix}${reducerName}_cache`,
70
+ actionAbort: `${PREFIX}@${prefix}${reducerName}_abort`
126
71
  };
127
- var fetch = opts.fetch ? opts.fetch : function () {
128
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
129
- args[_key] = arguments[_key];
130
- }
72
+ const fetch = opts.fetch ? opts.fetch : function (...args) {
131
73
  return fetchHolder.fetch.apply(this, args);
132
74
  };
133
- var meta = {
75
+ const meta = {
134
76
  holder: fetchHolder,
135
77
  virtual: !!opts.virtual,
136
78
  actions: memo.actions,
137
- cache: (0, _cache["default"])(opts.cache),
138
- urlOptions: urlOptions,
139
- fetch: fetch,
140
- broadcast: broadcast,
141
- reducerName: reducerName,
142
- prefetch: prefetch,
143
- postfetch: postfetch,
144
- validation: validation,
145
- helpers: helpers,
146
- transformer: transformer,
147
- prefix: prefix,
148
- crud: crud
79
+ cache: cacheManager(opts.cache),
80
+ urlOptions,
81
+ fetch,
82
+ broadcast,
83
+ reducerName,
84
+ prefetch,
85
+ postfetch,
86
+ validation,
87
+ helpers,
88
+ transformer,
89
+ prefix,
90
+ crud
149
91
  };
150
- memo.actions[key] = (0, _actionFn["default"])(url, key, options, ACTIONS, meta);
92
+ memo.actions[key] = actionFn(url, key, options, ACTIONS, meta);
151
93
  if (!meta.virtual && !memo.reducers[reducerName]) {
152
- var data = transformer();
153
- var sync = false;
154
- var syncing = false;
155
- var loading = false;
156
- var initialState = opts.cache ? {
157
- sync: sync,
158
- syncing: syncing,
159
- loading: loading,
160
- data: data,
94
+ const data = transformer();
95
+ const sync = false;
96
+ const syncing = false;
97
+ const loading = false;
98
+ const initialState = opts.cache ? {
99
+ sync,
100
+ syncing,
101
+ loading,
102
+ data,
161
103
  cache: {},
162
104
  request: null
163
105
  } : {
164
- sync: sync,
165
- syncing: syncing,
166
- loading: loading,
167
- data: data,
106
+ sync,
107
+ syncing,
108
+ loading,
109
+ data,
168
110
  request: null
169
111
  };
170
- var reducer = opts.reducer ? opts.reducer.bind(memo) : null;
171
- memo.reducers[reducerName] = (0, _reducerFn["default"])(initialState, ACTIONS, reducer);
112
+ const reducer = opts.reducer ? opts.reducer.bind(memo) : null;
113
+ memo.reducers[reducerName] = reducerFn(initialState, ACTIONS, reducer);
172
114
  }
173
115
  memo.events[reducerName] = ACTIONS;
174
116
  return memo;
175
117
  }
176
- return Object.keys(config).reduce(function (memo, key) {
177
- return fnConfigCallback(memo, config[key], key, config);
178
- }, cfg);
179
- }
180
- reduxApi.transformers = _transformers["default"];
181
- reduxApi.async = _async["default"];
182
- module.exports = exports.default;
118
+ return Object.keys(config).reduce((memo, key) => fnConfigCallback(memo, config[key], key, config), cfg);
119
+ }
package/lib/reducerFn.js CHANGED
@@ -1,88 +1,81 @@
1
1
  "use strict";
2
-
3
- /* eslint no-case-declarations: 0 */
4
- Object.defineProperty(exports, "__esModule", {
5
- value: true
6
- });
7
- exports["default"] = reducerFn;
8
- var _cache = require("./utils/cache");
9
- function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
10
- function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
11
- function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
12
- function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
13
- function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
14
- function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
15
- /**
16
- * Reducer contructor
17
- * @param {Object} initialState default initial state
18
- * @param {Object} actions actions map
19
- * @param {Function} reducer custom reducer function
20
- * @return {Function} reducer function
21
- */
22
- function reducerFn(initialState) {
23
- var actions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
24
- var reducer = arguments.length > 2 ? arguments[2] : undefined;
25
- var actionFetch = actions.actionFetch,
26
- actionSuccess = actions.actionSuccess,
27
- actionFail = actions.actionFail,
28
- actionReset = actions.actionReset,
29
- actionCache = actions.actionCache,
30
- actionAbort = actions.actionAbort;
31
- return function () {
32
- var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : initialState;
33
- var action = arguments.length > 1 ? arguments[1] : undefined;
34
- var request = action.request || {};
2
+ import { setExpire } from "./utils/cache";
3
+ export default function reducerFn(initialState, actions = {}, reducer) {
4
+ const {
5
+ actionFetch,
6
+ actionSuccess,
7
+ actionFail,
8
+ actionReset,
9
+ actionCache,
10
+ actionAbort
11
+ } = actions;
12
+ return (state = initialState, action) => {
13
+ const request = action.request || {};
35
14
  switch (action.type) {
36
15
  case actionFetch:
37
- return _objectSpread(_objectSpread({}, state), {}, {
38
- request: request,
16
+ return {
17
+ ...state,
18
+ request,
39
19
  loading: true,
40
20
  error: null,
41
21
  syncing: !!action.syncing
42
- });
22
+ };
43
23
  case actionSuccess:
44
- return _objectSpread(_objectSpread({}, state), {}, {
24
+ return {
25
+ ...state,
45
26
  loading: false,
46
27
  sync: true,
47
28
  syncing: false,
48
29
  error: null,
49
30
  data: action.data
50
- });
31
+ };
51
32
  case actionFail:
52
- return _objectSpread(_objectSpread({}, state), {}, {
33
+ return {
34
+ ...state,
53
35
  loading: false,
54
36
  error: action.error,
55
37
  syncing: false
56
- });
38
+ };
57
39
  case actionReset:
58
- var mutation = action.mutation;
59
- return mutation === "sync" ? _objectSpread(_objectSpread({}, state), {}, {
40
+ const {
41
+ mutation
42
+ } = action;
43
+ return mutation === "sync" ? {
44
+ ...state,
60
45
  request: null,
61
46
  sync: false
62
- }) : _objectSpread({}, initialState);
47
+ } : {
48
+ ...initialState
49
+ };
63
50
  case actionAbort:
64
- return _objectSpread(_objectSpread({}, state), {}, {
51
+ return {
52
+ ...state,
65
53
  request: null,
66
54
  loading: false,
67
55
  syncing: false,
68
56
  error: action.error
69
- });
57
+ };
70
58
  case actionCache:
71
- var id = action.id,
72
- data = action.data,
73
- persisted = action.persisted;
74
- var cacheExpire = state.cache[id] ? state.cache[id].expire : null;
75
- var expire = (0, _cache.setExpire)(action.expire, cacheExpire);
76
- return _objectSpread(_objectSpread({}, state), {}, {
77
- cache: _objectSpread(_objectSpread({}, state.cache), {}, _defineProperty({}, id, {
78
- expire: expire,
79
- data: data,
80
- persisted: persisted
81
- }))
82
- });
59
+ const {
60
+ id,
61
+ data,
62
+ persisted
63
+ } = action;
64
+ const cacheExpire = state.cache[id] ? state.cache[id].expire : null;
65
+ const expire = setExpire(action.expire, cacheExpire);
66
+ return {
67
+ ...state,
68
+ cache: {
69
+ ...state.cache,
70
+ [id]: {
71
+ expire,
72
+ data,
73
+ persisted
74
+ }
75
+ }
76
+ };
83
77
  default:
84
78
  return reducer ? reducer(state, action) : state;
85
79
  }
86
80
  };
87
- }
88
- module.exports = exports.default;
81
+ }