UNPKG

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