UNPKG

2.02 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var constants_1 = require("./constants");
4var types_1 = require("../client/types");
5var helper_1 = require("./helper");
6/**
7 * Predicate to determine if an action is an App Bridge action.
8 * @public
9 */
10function isAppBridgeAction(action) {
11 return (action instanceof Object &&
12 action.hasOwnProperty('type') &&
13 action.type.toString().startsWith(constants_1.PREFIX));
14}
15exports.isAppBridgeAction = isAppBridgeAction;
16/**
17 * Predicate to determine if an action originated from an application.
18 * @internal
19 */
20function isFromApp(action) {
21 if (typeof action !== 'object' || typeof action.source !== 'object') {
22 return false;
23 }
24 return typeof action.source.apiKey === 'string';
25}
26exports.isFromApp = isFromApp;
27/**
28 * Returns the action type without the prefix and group
29 * @internal
30 */
31function getPermissionKey(type) {
32 return type.replace(new RegExp("^" + constants_1.PREFIX + constants_1.SEPARATOR + "\\w+" + constants_1.SEPARATOR), '');
33}
34exports.getPermissionKey = getPermissionKey;
35/**
36 * Predicate to determine if an action is permitted
37 * @internal
38 */
39function isPermitted(features, _a, permissionType) {
40 var group = _a.group, type = _a.type;
41 if (!group || !features.hasOwnProperty(group)) {
42 return false;
43 }
44 var feature = features[group];
45 if (!feature) {
46 return false;
47 }
48 var actionType = getPermissionKey(type);
49 return feature[actionType] ? feature[actionType][permissionType] === true : false;
50}
51exports.isPermitted = isPermitted;
52/**
53 * Predicate to determine if an event originated from an application.
54 * @internal
55 */
56function isAppMessage(event) {
57 if (typeof event !== 'object' || !event.data || typeof event.data !== 'object') {
58 return false;
59 }
60 var data = event.data;
61 return data.hasOwnProperty('type') && helper_1.findMatchInEnum(types_1.MessageType, data.type) !== undefined;
62}
63exports.isAppMessage = isAppMessage;