-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
100 lines (79 loc) · 2.49 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = createMiddleware;
exports.sanitize = exports.setToValue = exports.decycle = void 0;
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
var decycle = function decycle(obj) {
var cache = [];
var stringified = JSON.stringify(obj, function (key, value) {
if (_typeof(value) === 'object' && value !== null) {
if (cache.indexOf(value) !== -1) {
return;
}
cache.push(value);
}
return value;
});
cache = null;
return stringified;
};
exports.decycle = decycle;
var setToValue = function setToValue(obj, value, path) {
if (!obj || _typeof(obj) !== 'object') {
return;
}
path = path.split('.');
var tmp = obj;
for (var i = 0, len = path.length; i < len - 1; i++) {
obj = Object.assign({}, obj[path[i]]);
tmp[path[i]] = obj;
tmp = obj;
}
obj[path[i]] = value;
};
exports.setToValue = setToValue;
var sanitize = function sanitize(state, keyPaths) {
if (typeof keyPaths === 'function') {
return keyPaths(state);
}
var replacement = '********';
var updatedState = Object.assign({}, state);
for (var i = 0, len = keyPaths.length; i < len; i++) {
setToValue(updatedState, replacement, keyPaths[i]);
}
return updatedState;
};
exports.sanitize = sanitize;
var isError = function isError(action) {
return action.error === true;
};
var stateForError = function stateForError(s, ks) {
return ks ? decycle(sanitize(s, ks)) : decycle(s);
};
function createMiddleware(Rollbar, keyPaths, wrapAction) {
return function (store) {
return function (next) {
return function (action) {
if (!isError(action)) {
if (wrapAction) {
try {
return next(action);
} catch (err) {
return Rollbar.error(err, {
action: decycle(action),
state: stateForError(store.getState(), keyPaths)
});
}
}
return next(action);
}
Rollbar.error(action.payload, {
state: stateForError(store.getState(), keyPaths)
});
return next(action);
};
};
};
}