UNPKG

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