@3plus/redux-api 1.1.0 → 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.
- 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/PubSub.js
CHANGED
|
@@ -1,43 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
});
|
|
6
|
-
exports["default"] = void 0;
|
|
7
|
-
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); }
|
|
8
|
-
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
|
|
9
|
-
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
|
|
10
|
-
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
|
|
11
|
-
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
12
|
-
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); }
|
|
13
|
-
var PubSub = exports["default"] = function () {
|
|
14
|
-
function PubSub() {
|
|
15
|
-
_classCallCheck(this, PubSub);
|
|
3
|
+
export default class PubSub {
|
|
4
|
+
constructor() {
|
|
16
5
|
this.container = [];
|
|
17
6
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}, {
|
|
33
|
-
key: "reject",
|
|
34
|
-
value: function reject(err) {
|
|
35
|
-
var container = this.container;
|
|
36
|
-
this.container = [];
|
|
37
|
-
container.forEach(function (cb) {
|
|
38
|
-
return cb(err);
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
}]);
|
|
42
|
-
}();
|
|
43
|
-
module.exports = exports.default;
|
|
7
|
+
push(cb) {
|
|
8
|
+
cb instanceof Function && this.container.push(cb);
|
|
9
|
+
}
|
|
10
|
+
resolve(data) {
|
|
11
|
+
const container = this.container;
|
|
12
|
+
this.container = [];
|
|
13
|
+
container.forEach(cb => cb(null, data));
|
|
14
|
+
}
|
|
15
|
+
reject(err) {
|
|
16
|
+
const container = this.container;
|
|
17
|
+
this.container = [];
|
|
18
|
+
container.forEach(cb => cb(err));
|
|
19
|
+
}
|
|
20
|
+
}
|
package/lib/actionFn.js
CHANGED
|
@@ -1,57 +1,36 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
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; }
|
|
23
|
-
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; }
|
|
24
|
-
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
25
|
-
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); }
|
|
26
|
-
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
|
|
27
|
-
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."); }
|
|
28
|
-
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; } }
|
|
29
|
-
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; }
|
|
30
|
-
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; } }
|
|
31
|
-
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
|
|
32
|
-
function actionFn(url, name, options) {
|
|
33
|
-
var ACTIONS = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
|
|
34
|
-
var meta = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
|
|
35
|
-
var actionFetch = ACTIONS.actionFetch,
|
|
36
|
-
actionSuccess = ACTIONS.actionSuccess,
|
|
37
|
-
actionFail = ACTIONS.actionFail,
|
|
38
|
-
actionReset = ACTIONS.actionReset,
|
|
39
|
-
actionCache = ACTIONS.actionCache,
|
|
40
|
-
actionAbort = ACTIONS.actionAbort;
|
|
41
|
-
var pubsub = new _PubSub["default"]();
|
|
42
|
-
var requestHolder = (0, _createHolder["default"])();
|
|
3
|
+
import urlTransform from "./urlTransform";
|
|
4
|
+
import merge from "./utils/merge";
|
|
5
|
+
import get from "./utils/get";
|
|
6
|
+
import fetchResolver from "./fetchResolver";
|
|
7
|
+
import PubSub from "./PubSub";
|
|
8
|
+
import createHolder from "./createHolder";
|
|
9
|
+
import { none, extractArgs, defaultMiddlewareArgsParser, CRUD } from "./helpers";
|
|
10
|
+
import { getCacheManager } from "./utils/cache";
|
|
11
|
+
export default function actionFn(url, name, options, ACTIONS = {}, meta = {}) {
|
|
12
|
+
const {
|
|
13
|
+
actionFetch,
|
|
14
|
+
actionSuccess,
|
|
15
|
+
actionFail,
|
|
16
|
+
actionReset,
|
|
17
|
+
actionCache,
|
|
18
|
+
actionAbort
|
|
19
|
+
} = ACTIONS;
|
|
20
|
+
const pubsub = new PubSub();
|
|
21
|
+
const requestHolder = createHolder();
|
|
43
22
|
function getOptions(urlT, params, getState) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
return (
|
|
23
|
+
const globalOptions = !meta.holder ? {} : meta.holder.options instanceof Function ? meta.holder.options(urlT, params, getState) : meta.holder.options;
|
|
24
|
+
const baseOptions = !(options instanceof Function) ? options : options(urlT, params, getState);
|
|
25
|
+
return merge({}, globalOptions, baseOptions, params);
|
|
47
26
|
}
|
|
48
27
|
function getUrl(pathvars, params, getState) {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
28
|
+
const resultUrlT = urlTransform(url, pathvars, meta.urlOptions);
|
|
29
|
+
let urlT = resultUrlT;
|
|
30
|
+
let rootUrl = get(meta, "holder", "rootUrl");
|
|
52
31
|
rootUrl = !(rootUrl instanceof Function) ? rootUrl : rootUrl(urlT, params, getState);
|
|
53
32
|
if (rootUrl) {
|
|
54
|
-
|
|
33
|
+
let urlObject = {};
|
|
55
34
|
try {
|
|
56
35
|
urlObject = new URL(urlT);
|
|
57
36
|
} catch (error) {
|
|
@@ -59,27 +38,24 @@ function actionFn(url, name, options) {
|
|
|
59
38
|
pathname: urlT
|
|
60
39
|
};
|
|
61
40
|
}
|
|
62
|
-
|
|
41
|
+
const rootUrlObject = new URL(rootUrl);
|
|
63
42
|
if (!urlObject.host) {
|
|
64
|
-
|
|
65
|
-
urlT =
|
|
43
|
+
const urlPath = (rootUrlObject.pathname ? rootUrlObject.pathname.replace(/\/$/, "") : "") + "/" + (urlObject.pathname ? urlObject.pathname.replace(/^\//, "") : "");
|
|
44
|
+
urlT = `${rootUrlObject.protocol}//${rootUrlObject.host}${urlPath}`;
|
|
66
45
|
}
|
|
67
46
|
}
|
|
68
47
|
return urlT;
|
|
69
48
|
}
|
|
70
|
-
function fetch(pathvars, params) {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
if (cacheManager && getState !== _helpers.none) {
|
|
79
|
-
var state = getState();
|
|
80
|
-
var cache = (0, _get["default"])(state, meta.prefix, meta.reducerName, "cache");
|
|
49
|
+
function fetch(pathvars, params, options = {}, getState = none, dispatch = none) {
|
|
50
|
+
const urlT = getUrl(pathvars, params, getState);
|
|
51
|
+
const opts = getOptions(urlT, params, getState);
|
|
52
|
+
let id = meta.reducerName || "";
|
|
53
|
+
const cacheManager = getCacheManager(options.expire, meta.cache);
|
|
54
|
+
if (cacheManager && getState !== none) {
|
|
55
|
+
const state = getState();
|
|
56
|
+
const cache = get(state, meta.prefix, meta.reducerName, "cache");
|
|
81
57
|
id += "_" + cacheManager.id(pathvars, params);
|
|
82
|
-
|
|
58
|
+
const data = cacheManager.getData(cache && id && cache[id] !== undefined && cache[id]);
|
|
83
59
|
if (data !== undefined) {
|
|
84
60
|
if (cache[id].persisted) {
|
|
85
61
|
Object.defineProperty(data, "persisted", {
|
|
@@ -90,13 +66,13 @@ function actionFn(url, name, options) {
|
|
|
90
66
|
return Promise.resolve(data);
|
|
91
67
|
}
|
|
92
68
|
}
|
|
93
|
-
|
|
94
|
-
if (cacheManager && dispatch !==
|
|
95
|
-
response.then(
|
|
69
|
+
const response = meta.fetch(urlT, opts, options);
|
|
70
|
+
if (cacheManager && dispatch !== none && id) {
|
|
71
|
+
response.then(data => {
|
|
96
72
|
dispatch({
|
|
97
73
|
type: actionCache,
|
|
98
|
-
id
|
|
99
|
-
data
|
|
74
|
+
id,
|
|
75
|
+
data,
|
|
100
76
|
expire: cacheManager.expire
|
|
101
77
|
});
|
|
102
78
|
});
|
|
@@ -104,84 +80,70 @@ function actionFn(url, name, options) {
|
|
|
104
80
|
return response;
|
|
105
81
|
}
|
|
106
82
|
function abort() {
|
|
107
|
-
|
|
108
|
-
|
|
83
|
+
const defer = requestHolder.pop();
|
|
84
|
+
const err = new Error("Application abort request");
|
|
109
85
|
defer && defer.reject(err);
|
|
110
86
|
return err;
|
|
111
87
|
}
|
|
112
|
-
function request(pathvars, params, options) {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
return err ? reject(err) : resolve(data);
|
|
120
|
-
});
|
|
121
|
-
});
|
|
122
|
-
});
|
|
123
|
-
var ret = result;
|
|
124
|
-
var responseHandler = (0, _get["default"])(meta, "holder", "responseHandler");
|
|
88
|
+
function request(pathvars, params, options, getState = none, dispatch = none) {
|
|
89
|
+
const response = fetch(pathvars, params, options, getState, dispatch);
|
|
90
|
+
const result = !meta.validation ? response : response.then(data => new Promise((resolve, reject) => {
|
|
91
|
+
meta.validation(data, err => err ? reject(err) : resolve(data));
|
|
92
|
+
}));
|
|
93
|
+
let ret = result;
|
|
94
|
+
const responseHandler = get(meta, "holder", "responseHandler");
|
|
125
95
|
if (responseHandler) {
|
|
126
96
|
if (result && result.then) {
|
|
127
|
-
ret = result.then(
|
|
128
|
-
|
|
97
|
+
ret = result.then(data => {
|
|
98
|
+
const res = responseHandler(null, data);
|
|
129
99
|
if (res === undefined) {
|
|
130
100
|
return data;
|
|
131
101
|
} else {
|
|
132
102
|
return res;
|
|
133
103
|
}
|
|
134
|
-
},
|
|
135
|
-
return responseHandler(err);
|
|
136
|
-
});
|
|
104
|
+
}, err => responseHandler(err));
|
|
137
105
|
} else {
|
|
138
106
|
ret = responseHandler(result);
|
|
139
107
|
}
|
|
140
108
|
}
|
|
141
|
-
ret && ret
|
|
109
|
+
ret && ret.catch && ret.catch(none);
|
|
142
110
|
return ret;
|
|
143
111
|
}
|
|
144
|
-
function fn() {
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
}
|
|
148
|
-
var _extractArgs = (0, _helpers.extractArgs)(args),
|
|
149
|
-
_extractArgs2 = _slicedToArray(_extractArgs, 3),
|
|
150
|
-
pathvars = _extractArgs2[0],
|
|
151
|
-
params = _extractArgs2[1],
|
|
152
|
-
callback = _extractArgs2[2];
|
|
153
|
-
var syncing = params ? !!params.syncing : false;
|
|
112
|
+
function fn(...args) {
|
|
113
|
+
const [pathvars, params, callback] = extractArgs(args);
|
|
114
|
+
const syncing = params ? !!params.syncing : false;
|
|
154
115
|
params && delete params.syncing;
|
|
155
116
|
pubsub.push(callback);
|
|
156
|
-
return
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
dispatch
|
|
160
|
-
getState
|
|
161
|
-
|
|
162
|
-
|
|
117
|
+
return (...middlewareArgs) => {
|
|
118
|
+
const middlewareParser = get(meta, "holder", "middlewareParser") || defaultMiddlewareArgsParser;
|
|
119
|
+
const {
|
|
120
|
+
dispatch,
|
|
121
|
+
getState
|
|
122
|
+
} = middlewareParser(...middlewareArgs);
|
|
123
|
+
const state = getState();
|
|
124
|
+
const isLoading = get(state, meta.prefix, meta.reducerName, "loading");
|
|
163
125
|
if (isLoading) {
|
|
164
126
|
return Promise.reject("isLoading-" + meta.prefix + "-" + meta.reducerName);
|
|
165
127
|
}
|
|
166
|
-
|
|
167
|
-
pathvars
|
|
168
|
-
params
|
|
128
|
+
const requestOptions = {
|
|
129
|
+
pathvars,
|
|
130
|
+
params
|
|
169
131
|
};
|
|
170
132
|
dispatch({
|
|
171
133
|
type: actionFetch,
|
|
172
|
-
syncing
|
|
134
|
+
syncing,
|
|
173
135
|
request: requestOptions
|
|
174
136
|
});
|
|
175
|
-
|
|
176
|
-
dispatch
|
|
177
|
-
getState
|
|
137
|
+
const fetchResolverOpts = {
|
|
138
|
+
dispatch,
|
|
139
|
+
getState,
|
|
178
140
|
request: requestOptions,
|
|
179
141
|
actions: meta.actions,
|
|
180
142
|
prefetch: meta.prefetch
|
|
181
143
|
};
|
|
182
144
|
if (Object.defineProperty) {
|
|
183
145
|
Object.defineProperty(fetchResolverOpts, "requestOptions", {
|
|
184
|
-
get
|
|
146
|
+
get() {
|
|
185
147
|
console.warn("Deprecated option, use `request` option");
|
|
186
148
|
return requestOptions;
|
|
187
149
|
}
|
|
@@ -189,8 +151,8 @@ function actionFn(url, name, options) {
|
|
|
189
151
|
} else {
|
|
190
152
|
fetchResolverOpts.requestOptions = requestOptions;
|
|
191
153
|
}
|
|
192
|
-
|
|
193
|
-
(0,
|
|
154
|
+
const result = new Promise((done, fail) => {
|
|
155
|
+
fetchResolver(0, fetchResolverOpts, err => {
|
|
194
156
|
if (err) {
|
|
195
157
|
dispatch({
|
|
196
158
|
error: err,
|
|
@@ -202,42 +164,42 @@ function actionFn(url, name, options) {
|
|
|
202
164
|
pubsub.reject(err);
|
|
203
165
|
return fail(err);
|
|
204
166
|
}
|
|
205
|
-
new Promise(
|
|
167
|
+
new Promise((resolve, reject) => {
|
|
206
168
|
requestHolder.set({
|
|
207
|
-
resolve
|
|
208
|
-
reject
|
|
169
|
+
resolve,
|
|
170
|
+
reject,
|
|
209
171
|
promise: request(pathvars, params, fetchResolverOpts.request, getState, dispatch).then(resolve, reject)
|
|
210
172
|
});
|
|
211
|
-
}).then(
|
|
173
|
+
}).then(d => {
|
|
212
174
|
requestHolder.pop();
|
|
213
|
-
|
|
214
|
-
|
|
175
|
+
const prevData = get(state, meta.prefix, meta.reducerName, "data");
|
|
176
|
+
const data = meta.transformer(d, prevData, {
|
|
215
177
|
type: actionSuccess,
|
|
216
178
|
request: requestOptions
|
|
217
179
|
}, getState);
|
|
218
180
|
dispatch({
|
|
219
|
-
data
|
|
181
|
+
data,
|
|
220
182
|
origData: d,
|
|
221
183
|
type: actionSuccess,
|
|
222
184
|
syncing: false,
|
|
223
185
|
request: requestOptions
|
|
224
186
|
});
|
|
225
187
|
if (meta.broadcast) {
|
|
226
|
-
meta.broadcast.forEach(
|
|
188
|
+
meta.broadcast.forEach(type => {
|
|
227
189
|
dispatch({
|
|
228
|
-
type
|
|
229
|
-
data
|
|
190
|
+
type,
|
|
191
|
+
data,
|
|
230
192
|
origData: d,
|
|
231
193
|
request: requestOptions
|
|
232
194
|
});
|
|
233
195
|
});
|
|
234
196
|
}
|
|
235
197
|
if (meta.postfetch) {
|
|
236
|
-
meta.postfetch.forEach(
|
|
198
|
+
meta.postfetch.forEach(postfetch => {
|
|
237
199
|
postfetch instanceof Function && postfetch({
|
|
238
|
-
data
|
|
239
|
-
getState
|
|
240
|
-
dispatch
|
|
200
|
+
data,
|
|
201
|
+
getState,
|
|
202
|
+
dispatch,
|
|
241
203
|
actions: meta.actions,
|
|
242
204
|
request: requestOptions
|
|
243
205
|
});
|
|
@@ -246,11 +208,11 @@ function actionFn(url, name, options) {
|
|
|
246
208
|
pubsub.resolve(data);
|
|
247
209
|
done(data);
|
|
248
210
|
if (d && d.persisted && meta.cache && meta.cache.reloadPersisted) {
|
|
249
|
-
fn.force
|
|
211
|
+
fn.force(...args)(dispatch, getState);
|
|
250
212
|
}
|
|
251
|
-
},
|
|
213
|
+
}, error => {
|
|
252
214
|
dispatch({
|
|
253
|
-
error
|
|
215
|
+
error,
|
|
254
216
|
type: actionFail,
|
|
255
217
|
loading: false,
|
|
256
218
|
syncing: false,
|
|
@@ -261,119 +223,103 @@ function actionFn(url, name, options) {
|
|
|
261
223
|
});
|
|
262
224
|
});
|
|
263
225
|
});
|
|
264
|
-
result
|
|
226
|
+
result.catch(none);
|
|
265
227
|
return result;
|
|
266
228
|
};
|
|
267
229
|
}
|
|
268
230
|
fn.request = function (pathvars, params, options) {
|
|
269
231
|
return request(pathvars, params, options || {});
|
|
270
232
|
};
|
|
271
|
-
fn.reset =
|
|
233
|
+
fn.reset = mutation => {
|
|
272
234
|
abort();
|
|
273
235
|
return mutation === "sync" ? {
|
|
274
236
|
type: actionReset,
|
|
275
|
-
mutation
|
|
237
|
+
mutation
|
|
276
238
|
} : {
|
|
277
239
|
type: actionReset
|
|
278
240
|
};
|
|
279
241
|
};
|
|
280
242
|
fn.abort = function () {
|
|
281
|
-
|
|
243
|
+
const error = abort();
|
|
282
244
|
return {
|
|
283
245
|
type: actionAbort,
|
|
284
|
-
error
|
|
246
|
+
error
|
|
285
247
|
};
|
|
286
248
|
};
|
|
287
|
-
fn.force = function () {
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
return function (dispatch, getState) {
|
|
292
|
-
var state = getState();
|
|
293
|
-
var isLoading = (0, _get["default"])(state, meta.prefix, meta.reducerName, "loading");
|
|
249
|
+
fn.force = function (...args) {
|
|
250
|
+
return (dispatch, getState) => {
|
|
251
|
+
const state = getState();
|
|
252
|
+
const isLoading = get(state, meta.prefix, meta.reducerName, "loading");
|
|
294
253
|
if (isLoading) {
|
|
295
254
|
dispatch(fn.abort());
|
|
296
255
|
}
|
|
297
|
-
return fn
|
|
256
|
+
return fn(...args)(dispatch, getState);
|
|
298
257
|
};
|
|
299
258
|
};
|
|
300
|
-
fn.sync =
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
pathvars = _extractArgs4[0],
|
|
307
|
-
params = _extractArgs4[1],
|
|
308
|
-
callback = _extractArgs4[2];
|
|
309
|
-
var isServer = meta.holder ? meta.holder.server : false;
|
|
310
|
-
return function (dispatch, getState) {
|
|
311
|
-
var state = getState();
|
|
312
|
-
var store = (0, _get["default"])(state, meta.prefix, name);
|
|
259
|
+
fn.sync = (...args) => {
|
|
260
|
+
const [pathvars, params, callback] = extractArgs(args);
|
|
261
|
+
const isServer = meta.holder ? meta.holder.server : false;
|
|
262
|
+
return (dispatch, getState) => {
|
|
263
|
+
const state = getState();
|
|
264
|
+
const store = get(state, meta.prefix, name);
|
|
313
265
|
if (!isServer && store && store.sync) {
|
|
314
266
|
callback(null, store.data);
|
|
315
267
|
return;
|
|
316
268
|
}
|
|
317
|
-
|
|
269
|
+
const modifyParams = {
|
|
270
|
+
...params,
|
|
318
271
|
syncing: true
|
|
319
|
-
}
|
|
272
|
+
};
|
|
320
273
|
return fn(pathvars, modifyParams, callback)(dispatch, getState);
|
|
321
274
|
};
|
|
322
275
|
};
|
|
323
|
-
|
|
276
|
+
let helpers = meta.helpers || {};
|
|
324
277
|
if (meta.crud) {
|
|
325
|
-
helpers =
|
|
278
|
+
helpers = {
|
|
279
|
+
...CRUD,
|
|
280
|
+
...helpers
|
|
281
|
+
};
|
|
326
282
|
}
|
|
327
|
-
|
|
283
|
+
const fnHelperCallback = (memo, func, helpername) => {
|
|
328
284
|
if (memo[helpername]) {
|
|
329
|
-
throw new Error(
|
|
285
|
+
throw new Error(`Helper name: "${helpername}" for endpoint "${name}" has been already reserved`);
|
|
330
286
|
}
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
call
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
params = _helpersResult[1];
|
|
366
|
-
(sync ? fn.sync : fn)(pathvars, params, callback)(dispatch, getState);
|
|
367
|
-
}
|
|
368
|
-
});
|
|
369
|
-
result["catch"](_helpers.none);
|
|
370
|
-
return result;
|
|
371
|
-
};
|
|
287
|
+
const {
|
|
288
|
+
sync,
|
|
289
|
+
call
|
|
290
|
+
} = func instanceof Function ? {
|
|
291
|
+
call: func
|
|
292
|
+
} : func;
|
|
293
|
+
memo[helpername] = (...args) => (dispatch, getState) => {
|
|
294
|
+
const index = args.length - 1;
|
|
295
|
+
const callbackFn = args[index] instanceof Function ? args[index] : none;
|
|
296
|
+
const helpersResult = call.apply({
|
|
297
|
+
getState,
|
|
298
|
+
dispatch,
|
|
299
|
+
actions: meta.actions
|
|
300
|
+
}, args);
|
|
301
|
+
const result = new Promise((resolve, reject) => {
|
|
302
|
+
const callback = (err, data) => {
|
|
303
|
+
err ? reject(err) : resolve(data);
|
|
304
|
+
callbackFn(err, data);
|
|
305
|
+
};
|
|
306
|
+
if (helpersResult instanceof Function) {
|
|
307
|
+
helpersResult((error, newArgs = []) => {
|
|
308
|
+
if (error) {
|
|
309
|
+
callback(error);
|
|
310
|
+
} else {
|
|
311
|
+
(sync ? fn.sync : fn)(...newArgs.concat(callback))(dispatch, getState);
|
|
312
|
+
}
|
|
313
|
+
});
|
|
314
|
+
} else {
|
|
315
|
+
const [pathvars, params] = helpersResult;
|
|
316
|
+
(sync ? fn.sync : fn)(pathvars, params, callback)(dispatch, getState);
|
|
317
|
+
}
|
|
318
|
+
});
|
|
319
|
+
result.catch(none);
|
|
320
|
+
return result;
|
|
372
321
|
};
|
|
373
322
|
return memo;
|
|
374
323
|
};
|
|
375
|
-
return Object.keys(helpers).reduce(
|
|
376
|
-
|
|
377
|
-
}, fn);
|
|
378
|
-
}
|
|
379
|
-
module.exports = exports.default;
|
|
324
|
+
return Object.keys(helpers).reduce((memo, key) => fnHelperCallback(memo, helpers[key], key, helpers), fn);
|
|
325
|
+
}
|
package/lib/adapters/fetch.js
CHANGED
|
@@ -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,26 +16,23 @@ function toJSON(resp) {
|
|
|
20
16
|
return Promise.resolve(resp).then(processData);
|
|
21
17
|
}
|
|
22
18
|
}
|
|
23
|
-
function
|
|
24
|
-
return
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
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
|
+
}
|