UNPKG

3.78 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.CALL_FIRESTORE = undefined;
7
8var _isArray2 = require('lodash/isArray');
9
10var _isArray3 = _interopRequireDefault(_isArray2);
11
12var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
13
14var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
15
16exports.default = reduxFirestoreMiddleware;
17
18var _constants = require('./constants');
19
20function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
21
22function callFirestore(firebaseInstance, callInfoObj) {
23 var method = callInfoObj.method;
24 var modelArgs = callInfoObj.modelArgs,
25 methodArgs = callInfoObj.methodArgs;
26
27 if (!(0, _isArray3.default)(modelArgs)) modelArgs = [modelArgs];
28 if (!(0, _isArray3.default)(methodArgs)) methodArgs = [methodArgs];
29 if (!firebaseInstance || !firebaseInstance.firestore) {
30 throw new Error('firestore is not a Firebase namespace');
31 }
32
33 return !methodArgs ? firebaseInstance.firestore()[method] : firebaseInstance.firestore()[method].apply(firebaseInstance, methodArgs);
34}
35
36var CALL_FIRESTORE = exports.CALL_FIRESTORE = 'CALL_FIRESTORE';
37
38var typesMap = {
39 get: [_constants.actionTypes.GET_REQUEST, _constants.actionTypes.GET_SUCCESS, _constants.actionTypes.GET_FAILURE]
40};
41
42function reduxFirestoreMiddleware(firestore) {
43 return function (store) {
44 return function (next) {
45 return function (action) {
46 var callAPI = action.type === CALL_FIRESTORE ? action : undefined;
47 if (typeof callAPI === 'undefined') return next(action);
48
49 var method = callAPI.method;
50
51
52 if (typeof method === 'function') method = method(store.getState());
53
54 if (typeof method !== 'string') throw new Error('Specify a method.');
55
56 var args = callAPI.args;
57
58 var types = typesMap[method];
59
60 if (!Array.isArray(types) || types.length !== 3) {
61 throw new Error('Expected an array of three action types.');
62 }
63
64 if (!types.every(function (type) {
65 return typeof type === 'string';
66 })) {
67 throw new Error('Expected action types to be strings.');
68 }
69
70 function actionWith(data) {
71 var finalAction = _extends({}, action, data);
72 delete finalAction[CALL_FIRESTORE];
73 return finalAction;
74 }
75
76 var _types = _slicedToArray(types, 3),
77 requestType = _types[0],
78 successType = _types[1],
79 failureType = _types[2];
80
81 next({ type: requestType });
82 var callInfoObj = { method: method };
83 return callFirestore(firestore, callInfoObj).then(function (response) {
84 return next({ response: response, method: method, args: args, type: successType });
85 }).catch(function (error) {
86 return next(actionWith({
87 type: failureType,
88 error: error.message || error || 'Something bad happened'
89 }));
90 });
91 };
92 };
93 };
94}
\No newline at end of file