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