@3plus/redux-api 1.1.0 → 1.1.3
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.
- package/lib/PubSub.js +16 -39
- package/lib/actionFn.js +156 -210
- package/lib/adapters/fetch.js +20 -27
- package/lib/async.js +6 -17
- package/lib/createHolder.js +8 -13
- package/lib/fetchResolver.js +3 -13
- package/lib/helpers.js +15 -43
- package/lib/index.js +72 -90
- package/lib/reducerFn.js +52 -50
- package/lib/transformers.js +7 -12
- package/lib/urlTransform.js +43 -50
- package/lib/utils/cache.js +29 -35
- package/lib/utils/get.js +3 -19
- package/lib/utils/merge.js +8 -18
- package/lib/utils/omit.js +6 -17
- package/package.json +6 -7
- package/module/PubSub.js +0 -39
- package/module/actionFn.js +0 -373
- package/module/adapters/fetch.js +0 -40
- package/module/async.js +0 -21
- package/module/createHolder.js +0 -27
- package/module/fetchResolver.js +0 -15
- package/module/helpers.js +0 -54
- package/module/index.js +0 -127
- package/module/reducerFn.js +0 -74
- package/module/transformers.js +0 -17
- package/module/urlTransform.js +0 -65
- package/module/utils/cache.js +0 -84
- package/module/utils/get.js +0 -19
- package/module/utils/merge.js +0 -32
- package/module/utils/omit.js +0 -17
package/lib/async.js
CHANGED
|
@@ -1,28 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports["default"] = async;
|
|
7
|
-
function async(dispatch) {
|
|
8
|
-
var currentFunction = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
|
9
|
-
for (var _len = arguments.length, restFunctions = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
|
|
10
|
-
restFunctions[_key - 2] = arguments[_key];
|
|
11
|
-
}
|
|
12
|
-
return new Promise(function (resolve, reject) {
|
|
1
|
+
export default function async(dispatch, currentFunction = null, ...restFunctions) {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
13
3
|
if (!currentFunction) {
|
|
14
4
|
reject("no chain function");
|
|
15
5
|
} else {
|
|
16
|
-
dispatch(currentFunction(
|
|
6
|
+
dispatch(currentFunction((err, data) => {
|
|
17
7
|
err ? reject(err) : resolve(data);
|
|
18
8
|
}) || {});
|
|
19
9
|
}
|
|
20
|
-
}).then(
|
|
10
|
+
}).then(data => {
|
|
21
11
|
if (restFunctions.length) {
|
|
22
|
-
return async
|
|
12
|
+
return async(dispatch, ...restFunctions);
|
|
23
13
|
} else {
|
|
24
14
|
return data;
|
|
25
15
|
}
|
|
26
16
|
});
|
|
27
|
-
}
|
|
28
|
-
module.exports = exports.default;
|
|
17
|
+
}
|
package/lib/createHolder.js
CHANGED
|
@@ -1,14 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
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
|
|
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
|
|
15
|
+
empty() {
|
|
20
16
|
return !hasData;
|
|
21
17
|
},
|
|
22
|
-
pop
|
|
18
|
+
pop() {
|
|
23
19
|
if (hasData) {
|
|
24
20
|
hasData = false;
|
|
25
|
-
|
|
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
|
+
}
|
package/lib/fetchResolver.js
CHANGED
|
@@ -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,
|
|
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
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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
|
|
42
|
-
|
|
43
|
-
|
|
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
|
-
}
|
|
24
|
+
}, cb];
|
|
53
25
|
};
|
|
54
26
|
}
|
|
55
|
-
function defaultMiddlewareArgsParser(dispatch, getState) {
|
|
27
|
+
export function defaultMiddlewareArgsParser(dispatch, getState) {
|
|
56
28
|
return {
|
|
57
|
-
dispatch
|
|
58
|
-
getState
|
|
29
|
+
dispatch,
|
|
30
|
+
getState
|
|
59
31
|
};
|
|
60
32
|
}
|
|
61
|
-
|
|
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,32 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
return _transformers["default"];
|
|
10
|
-
}
|
|
11
|
-
});
|
|
12
|
-
var _reducerFn = _interopRequireDefault(require("./reducerFn"));
|
|
13
|
-
var _actionFn = _interopRequireDefault(require("./actionFn"));
|
|
14
|
-
var _transformers = _interopRequireDefault(require("./transformers"));
|
|
15
|
-
var _cache = _interopRequireDefault(require("./utils/cache"));
|
|
16
|
-
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
|
|
17
|
-
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; }
|
|
18
|
-
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; }
|
|
19
|
-
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; }
|
|
20
|
-
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
21
|
-
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); }
|
|
22
|
-
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); }
|
|
23
|
-
var defaultEndpointConfig = {
|
|
24
|
-
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
|
|
25
9
|
};
|
|
26
|
-
|
|
27
|
-
function reduxApi(config, baseConfig) {
|
|
10
|
+
const PREFIX = "@@redux-api";
|
|
11
|
+
export default function reduxApi(config, baseConfig) {
|
|
28
12
|
config || (config = {});
|
|
29
|
-
|
|
13
|
+
const fetchHolder = {
|
|
30
14
|
fetch: null,
|
|
31
15
|
server: false,
|
|
32
16
|
rootUrl: null,
|
|
@@ -34,14 +18,12 @@ function reduxApi(config, baseConfig) {
|
|
|
34
18
|
options: {},
|
|
35
19
|
responseHandler: null
|
|
36
20
|
};
|
|
37
|
-
|
|
38
|
-
use
|
|
21
|
+
const cfg = {
|
|
22
|
+
use(key, value) {
|
|
39
23
|
fetchHolder[key] = value;
|
|
40
24
|
return this;
|
|
41
25
|
},
|
|
42
|
-
init
|
|
43
|
-
var isServer = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
|
44
|
-
var rootUrl = arguments.length > 2 ? arguments[2] : undefined;
|
|
26
|
+
init(fetch, isServer = false, rootUrl) {
|
|
45
27
|
console.warn("Deprecated method, use `use` method");
|
|
46
28
|
this.use("fetch", fetch);
|
|
47
29
|
this.use("server", isServer);
|
|
@@ -53,85 +35,85 @@ function reduxApi(config, baseConfig) {
|
|
|
53
35
|
events: {}
|
|
54
36
|
};
|
|
55
37
|
function fnConfigCallback(memo, value, key) {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
38
|
+
const opts = typeof value === "object" ? {
|
|
39
|
+
...defaultEndpointConfig,
|
|
40
|
+
reducerName: key,
|
|
41
|
+
...value
|
|
42
|
+
} : {
|
|
43
|
+
...defaultEndpointConfig,
|
|
59
44
|
reducerName: key,
|
|
60
45
|
url: value
|
|
61
|
-
}
|
|
46
|
+
};
|
|
62
47
|
if (opts.broadcast !== void 0) {
|
|
63
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)");
|
|
64
49
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
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`
|
|
84
71
|
};
|
|
85
|
-
|
|
86
|
-
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
87
|
-
args[_key] = arguments[_key];
|
|
88
|
-
}
|
|
72
|
+
const fetch = opts.fetch ? opts.fetch : function (...args) {
|
|
89
73
|
return fetchHolder.fetch.apply(this, args);
|
|
90
74
|
};
|
|
91
|
-
|
|
75
|
+
const meta = {
|
|
92
76
|
holder: fetchHolder,
|
|
93
77
|
virtual: !!opts.virtual,
|
|
94
78
|
actions: memo.actions,
|
|
95
|
-
cache: (
|
|
96
|
-
urlOptions
|
|
97
|
-
fetch
|
|
98
|
-
broadcast
|
|
99
|
-
reducerName
|
|
100
|
-
prefetch
|
|
101
|
-
postfetch
|
|
102
|
-
validation
|
|
103
|
-
helpers
|
|
104
|
-
transformer
|
|
105
|
-
prefix
|
|
106
|
-
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
|
|
107
91
|
};
|
|
108
|
-
memo.actions[key] = (
|
|
92
|
+
memo.actions[key] = actionFn(url, key, options, ACTIONS, meta);
|
|
109
93
|
if (!meta.virtual && !memo.reducers[reducerName]) {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
sync
|
|
116
|
-
syncing
|
|
117
|
-
loading
|
|
118
|
-
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,
|
|
119
103
|
cache: {},
|
|
120
104
|
request: null
|
|
121
105
|
} : {
|
|
122
|
-
sync
|
|
123
|
-
syncing
|
|
124
|
-
loading
|
|
125
|
-
data
|
|
106
|
+
sync,
|
|
107
|
+
syncing,
|
|
108
|
+
loading,
|
|
109
|
+
data,
|
|
126
110
|
request: null
|
|
127
111
|
};
|
|
128
|
-
|
|
129
|
-
memo.reducers[reducerName] = (
|
|
112
|
+
const reducer = opts.reducer ? opts.reducer.bind(memo) : null;
|
|
113
|
+
memo.reducers[reducerName] = reducerFn(initialState, ACTIONS, reducer);
|
|
130
114
|
}
|
|
131
115
|
memo.events[reducerName] = ACTIONS;
|
|
132
116
|
return memo;
|
|
133
117
|
}
|
|
134
|
-
return Object.keys(config).reduce(
|
|
135
|
-
return fnConfigCallback(memo, config[key], key, config);
|
|
136
|
-
}, cfg);
|
|
118
|
+
return Object.keys(config).reduce((memo, key) => fnConfigCallback(memo, config[key], key, config), cfg);
|
|
137
119
|
}
|
package/lib/reducerFn.js
CHANGED
|
@@ -1,79 +1,81 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
var actions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
15
|
-
var reducer = arguments.length > 2 ? arguments[2] : undefined;
|
|
16
|
-
var actionFetch = actions.actionFetch,
|
|
17
|
-
actionSuccess = actions.actionSuccess,
|
|
18
|
-
actionFail = actions.actionFail,
|
|
19
|
-
actionReset = actions.actionReset,
|
|
20
|
-
actionCache = actions.actionCache,
|
|
21
|
-
actionAbort = actions.actionAbort;
|
|
22
|
-
return function () {
|
|
23
|
-
var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : initialState;
|
|
24
|
-
var action = arguments.length > 1 ? arguments[1] : undefined;
|
|
25
|
-
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 || {};
|
|
26
14
|
switch (action.type) {
|
|
27
15
|
case actionFetch:
|
|
28
|
-
return
|
|
29
|
-
|
|
16
|
+
return {
|
|
17
|
+
...state,
|
|
18
|
+
request,
|
|
30
19
|
loading: true,
|
|
31
20
|
error: null,
|
|
32
21
|
syncing: !!action.syncing
|
|
33
|
-
}
|
|
22
|
+
};
|
|
34
23
|
case actionSuccess:
|
|
35
|
-
return
|
|
24
|
+
return {
|
|
25
|
+
...state,
|
|
36
26
|
loading: false,
|
|
37
27
|
sync: true,
|
|
38
28
|
syncing: false,
|
|
39
29
|
error: null,
|
|
40
30
|
data: action.data
|
|
41
|
-
}
|
|
31
|
+
};
|
|
42
32
|
case actionFail:
|
|
43
|
-
return
|
|
33
|
+
return {
|
|
34
|
+
...state,
|
|
44
35
|
loading: false,
|
|
45
36
|
error: action.error,
|
|
46
37
|
syncing: false
|
|
47
|
-
}
|
|
38
|
+
};
|
|
48
39
|
case actionReset:
|
|
49
|
-
|
|
50
|
-
|
|
40
|
+
const {
|
|
41
|
+
mutation
|
|
42
|
+
} = action;
|
|
43
|
+
return mutation === "sync" ? {
|
|
44
|
+
...state,
|
|
51
45
|
request: null,
|
|
52
46
|
sync: false
|
|
53
|
-
}
|
|
47
|
+
} : {
|
|
48
|
+
...initialState
|
|
49
|
+
};
|
|
54
50
|
case actionAbort:
|
|
55
|
-
return
|
|
51
|
+
return {
|
|
52
|
+
...state,
|
|
56
53
|
request: null,
|
|
57
54
|
loading: false,
|
|
58
55
|
syncing: false,
|
|
59
56
|
error: action.error
|
|
60
|
-
}
|
|
57
|
+
};
|
|
61
58
|
case actionCache:
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
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
|
+
};
|
|
74
77
|
default:
|
|
75
78
|
return reducer ? reducer(state, action) : state;
|
|
76
79
|
}
|
|
77
80
|
};
|
|
78
|
-
}
|
|
79
|
-
module.exports = exports.default;
|
|
81
|
+
}
|
package/lib/transformers.js
CHANGED
|
@@ -1,22 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
Object.
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
var toString = Object.prototype.toString;
|
|
8
|
-
var OBJECT = "[object Object]";
|
|
9
|
-
var _default = exports["default"] = {
|
|
10
|
-
array: function array(data) {
|
|
3
|
+
const toString = Object.prototype.toString;
|
|
4
|
+
const OBJECT = "[object Object]";
|
|
5
|
+
export default {
|
|
6
|
+
array(data) {
|
|
11
7
|
return !data ? [] : Array.isArray(data) ? data : [data];
|
|
12
8
|
},
|
|
13
|
-
object
|
|
9
|
+
object(data) {
|
|
14
10
|
if (!data) {
|
|
15
11
|
return {};
|
|
16
12
|
}
|
|
17
13
|
return toString.call(data) === OBJECT ? data : {
|
|
18
|
-
data
|
|
14
|
+
data
|
|
19
15
|
};
|
|
20
16
|
}
|
|
21
|
-
};
|
|
22
|
-
module.exports = exports.default;
|
|
17
|
+
};
|