@shopify/app-bridge-core
Version:
**[Join our team and work on libraries like this one.](https://www.shopify.ca/careers)**
57 lines (54 loc) • 1.81 kB
JavaScript
import { MessageType } from '../client/types.js';
import { PREFIX, SEPARATOR } from './constants.js';
import { findMatchInEnum } from './helper.js';
/**
* Predicate to determine if an action is an App Bridge action.
* @public
*/
function isAppBridgeAction(action) {
return (action instanceof Object &&
Object.prototype.hasOwnProperty.call(action, 'type') &&
action.type.toString().startsWith(PREFIX));
}
/**
* Predicate to determine if an event originated from an application.
* @internal
*/
function isAppMessage(event) {
if (typeof event !== 'object' || !event.data || typeof event.data !== 'object') {
return false;
}
const { data } = event;
return (Object.prototype.hasOwnProperty.call(data, 'type') &&
findMatchInEnum(MessageType, data.type) !== undefined);
}
/**
* Function used to determine if an action is in the Performance or WebVitals groups
* @public
*/
function isPerformanceOrWebVitalsAction({ type }) {
return type.match(/^APP::(PERFORMANCE|WEB_VITALS)::/);
}
/**
* Returns the action type without the prefix and group
* @internal
*/
function getPermissionKey(type) {
return type.replace(new RegExp(`^${PREFIX}${SEPARATOR}\\w+${SEPARATOR}`), '');
}
/**
* Predicate to determine if an action is permitted
* @internal
*/
function isPermitted(features, { group, type }, permissionType) {
if (!group || !Object.prototype.hasOwnProperty.call(features, group)) {
return false;
}
const feature = features[group];
if (!feature) {
return false;
}
const actionType = getPermissionKey(type);
return feature[actionType] ? feature[actionType][permissionType] === true : false;
}
export { getPermissionKey, isAppBridgeAction, isAppMessage, isPerformanceOrWebVitalsAction, isPermitted };