@builttocreate/engine-utils 1.17.0-rc1 → 1.17.1-rc2

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.
@@ -0,0 +1,106 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports["default"] = exports.CALL_API = void 0;
9
+
10
+ var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
11
+
12
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
13
+
14
+ function 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; }
15
+
16
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
17
+
18
+ var CALL_API = 'CALL API';
19
+ exports.CALL_API = CALL_API;
20
+
21
+ var callApiMiddleware = function callApiMiddleware(_ref) {
22
+ var dispatch = _ref.dispatch,
23
+ getState = _ref.getState;
24
+
25
+ /**
26
+ * @param next //Callback for the action
27
+ * @param action //Action object
28
+ *
29
+ * async action example: {
30
+ * CALL_API: {
31
+ * types: [ACTION_REQUEST, ACTION_SUCCESS, ACTION_FAILURE],
32
+ * apiCall: apiMethodCall, //apiMethodCall should return a promise
33
+ * success: callbackFunction,
34
+ * error: callbackFunction
35
+ * }
36
+ * }
37
+ */
38
+ return function (next) {
39
+ return function (action) {
40
+ var callApi = action[CALL_API]; //If callApi is not defined then it is a normal action (synchronous) pass it through
41
+
42
+ if (typeof callApi === 'undefined') return next(action);
43
+ var types = callApi.types,
44
+ apiCall = callApi.apiCall;
45
+ var successCallback = callApi.success;
46
+ var errorCallback = callApi.error;
47
+ /**
48
+ * Action validation
49
+ *
50
+ * Types:
51
+ * All async actions should have three action types: requested, success, & failure
52
+ * All actions should be strings.
53
+ *
54
+ * apiCall:
55
+ * All aysnc actions should have an apiCall function
56
+ */
57
+
58
+ if (!Array.isArray(types) || types.length !== 3) {
59
+ throw new Error('action.types: Expected an array of three action types');
60
+ }
61
+
62
+ if (!types.every(function (type) {
63
+ return typeof type === 'string';
64
+ })) {
65
+ throw new Error('action.types: Expected action types to be strings');
66
+ }
67
+
68
+ if (typeof apiCall !== 'function') {
69
+ throw new Error('action.apiCall: Expected apiCall to be a function');
70
+ }
71
+
72
+ var actionWith = function actionWith(data) {
73
+ var finalAction = _objectSpread(_objectSpread({}, action), data);
74
+
75
+ delete finalAction[CALL_API];
76
+ return finalAction;
77
+ };
78
+
79
+ var _callApi$types = (0, _slicedToArray2["default"])(callApi.types, 3),
80
+ requestType = _callApi$types[0],
81
+ successType = _callApi$types[1],
82
+ failureType = _callApi$types[2];
83
+
84
+ next(actionWith({
85
+ type: requestType
86
+ }));
87
+ return apiCall().then(function (response) {
88
+ next(actionWith({
89
+ type: successType,
90
+ payload: response
91
+ }));
92
+ if (successCallback) successCallback(response, dispatch, getState);
93
+ })["catch"](function (error) {
94
+ next(actionWith({
95
+ type: failureType,
96
+ payload: error,
97
+ error: true
98
+ }));
99
+ if (errorCallback) errorCallback(error, dispatch, getState);
100
+ });
101
+ };
102
+ };
103
+ };
104
+
105
+ var _default = callApiMiddleware;
106
+ exports["default"] = _default;
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports["default"] = exports.hasRoles = exports.hasAdminRole = void 0;
9
+
10
+ var _Roles = _interopRequireDefault(require("./constants/Roles"));
11
+
12
+ var hasAdminRole = function hasAdminRole(permissions) {
13
+ return hasRoles([_Roles["default"].admin.value], permissions);
14
+ };
15
+
16
+ exports.hasAdminRole = hasAdminRole;
17
+
18
+ var hasRoles = function hasRoles(roles, permissions) {
19
+ /**
20
+ * If one of users permissions has a role inside of the
21
+ * roles property then return true
22
+ */
23
+ return !permissions || permissions.length < 1 ? false : permissions.find(function (perm) {
24
+ return roles.indexOf(perm.role) !== -1;
25
+ });
26
+ };
27
+
28
+ exports.hasRoles = hasRoles;
29
+ var _default = {
30
+ hasAdminRole: hasAdminRole,
31
+ hasRoles: hasRoles
32
+ };
33
+ exports["default"] = _default;