UNPKG

@shopify/app-bridge-host

Version:

App Bridge Host contains middleware and components that are meant to be consumed by the app's host. The middleware and `Frame` component are responsible for facilitating messages posted between the client and host, and used to act on actions sent from the

221 lines (220 loc) 8.37 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var _a; var app_bridge_1 = require("@shopify/app-bridge"); var Actions = __importStar(require("@shopify/app-bridge/actions")); var merge_1 = __importDefault(require("@shopify/app-bridge/actions/merge")); var validator_1 = require("@shopify/app-bridge/actions/validator"); var utilities_1 = require("../utilities"); var APP_REDIRECT_KEY = validator_1.getPermissionKey(Actions.Redirect.ActionType.APP); var DEFAULT_FEATURES_STATE = (_a = {}, _a[Actions.Features.Action.UPDATE] = { Dispatch: false, Subscribe: true, }, _a[Actions.Features.Action.REQUEST] = { Dispatch: true, Subscribe: false, }, _a[Actions.Features.Action.REQUEST_UPDATE] = { Dispatch: false, Subscribe: true, }, _a); var DEFAULT_ENABLED_GROUPS = [ Actions.Group.Button, Actions.Group.ButtonGroup, Actions.Group.Error, ]; exports.defaultFeaturesStore = getDefaultFeatures(); function getDefaultFeatures() { var _a, _b, _c, _d, _e; var excludeGroups = [ Actions.Group.Cart, Actions.Group.Scanner, Actions.Group.Share, Actions.Group.Fullscreen, Actions.Group.ContextualSaveBar, ]; var overrideFeatures = (_a = {}, _a[app_bridge_1.Context.Modal] = (_b = {}, _b[Actions.Group.Print] = setFeature(utilities_1.getGroupActionType(Actions.Group.Print), false), _b[Actions.Group.Features] = DEFAULT_FEATURES_STATE, _b[Actions.Group.Navigation] = (_c = {}, _c[APP_REDIRECT_KEY] = { Dispatch: true, Subscribe: false, }, _c), _b), _a[app_bridge_1.Context.Main] = (_d = {}, _d[Actions.Group.Features] = DEFAULT_FEATURES_STATE, _d), _a); var groups = Object.keys(Actions.Group) .map(function (key) { return Actions.Group[key]; }) .reduce(function (acc, group) { var _a; var castedGroup = group; return __assign({}, acc, (_a = {}, _a[castedGroup] = setFeature(utilities_1.getGroupActionType(castedGroup), !excludeGroups.includes(castedGroup)), _a)); }, {}); return merge_1.default((_e = {}, _e[app_bridge_1.Context.Main] = groups, _e[app_bridge_1.Context.Modal] = groups, _e), overrideFeatures); } /** * Map an action enum to the given permission * @public * @param actions - the actions enum * @param value - the permission to set * @returns an object with keys mapped to the actions and value set to the given permission */ function setFeature(actions, value) { if (value === void 0) { value = false; } var permission = value; var features = {}; if (typeof value === 'boolean') { permission = { Dispatch: value, Subscribe: value }; } Object.keys(actions) .map(function (key) { return actions[key]; }) .forEach(function (type) { features[validator_1.getPermissionKey(type)] = permission; }); return features; } exports.setFeature = setFeature; /** * Returns the updated feature state * @param state - the current state * @param action - the update action with partial features in the payload * @internal * */ function featuresReducer(state, action) { if (state === void 0) { state = exports.defaultFeaturesStore; } switch (action.type) { case Actions.Features.ActionType.UPDATE: { var payload = action.payload; return merge_1.default(state, getUpdatePayload(payload)); } default: return state; } } exports.default = featuresReducer; function normalizePermissions(features) { if (features === void 0) { features = {}; } return Object.keys(features).reduce(function (acc, group) { var _a; var featureGroup = features[group]; if (!featureGroup) { return acc; } var actionPermissions = Object.keys(featureGroup).reduce(function (actionAcc, action) { var _a, _b; var permission = featureGroup[action]; return __assign({}, actionAcc, (_a = {}, _a[action] = hasNewPermission(permission) ? permission : (_b = {}, _b[app_bridge_1.PermissionType.Dispatch] = permission, _b[app_bridge_1.PermissionType.Subscribe] = permission, _b), _a)); }, {}); return __assign({}, acc, (_a = {}, _a[group] = actionPermissions, _a)); }, {}); } function hasNewPermission(permission) { return (permission.hasOwnProperty(app_bridge_1.PermissionType.Dispatch) && permission.hasOwnProperty(app_bridge_1.PermissionType.Subscribe)); } function getUpdatePayload(payload) { var _a, _b; if (payload.hasOwnProperty(app_bridge_1.Context.Main) || payload.hasOwnProperty(app_bridge_1.Context.Modal)) { return _a = {}, _a[app_bridge_1.Context.Main] = normalizePermissions(payload[app_bridge_1.Context.Main]), _a[app_bridge_1.Context.Modal] = normalizePermissions(payload[app_bridge_1.Context.Modal]), _a; } return _b = {}, _b[app_bridge_1.Context.Main] = normalizePermissions(payload), _b; } function getDefaultAsyncFeatures() { var _a; var features = Object.keys(Actions.Group) .map(function (key) { return Actions.Group[key]; }) .reduce(function (acc, group) { var _a; var castedGroup = group; var permissions = group === Actions.Group.Features ? DEFAULT_FEATURES_STATE : setFeature(utilities_1.getGroupActionType(castedGroup), DEFAULT_ENABLED_GROUPS.includes(castedGroup)); return __assign({}, acc, (_a = {}, _a[castedGroup] = permissions, _a)); }, {}); return _a = {}, _a[app_bridge_1.Context.Main] = features, _a[app_bridge_1.Context.Modal] = features, _a; } var defaultAsyncFeaturesStore = getDefaultAsyncFeatures(); /** * Returns the updated feature state * @param state - the current state * @param action - the update action with partial features in the payload * @internal * */ function asyncFeaturesReducer(state, action) { if (state === void 0) { state = defaultAsyncFeaturesStore; } return featuresReducer(state, action); } exports.asyncFeaturesReducer = asyncFeaturesReducer; /** * Returns the features state with the provided group(s) enabled * The permission is set for both Main and Modal context * according to the default feature permission rules * @argument keys - the group(s) to enable the feature permission * @public * */ function setFeaturesAvailable() { var features = []; for (var _i = 0; _i < arguments.length; _i++) { features[_i] = arguments[_i]; } var _a; var Modal = exports.defaultFeaturesStore.Modal, Main = exports.defaultFeaturesStore.Main; var modalPermission = features.reduce(function (acc, curr) { var _a; return __assign({}, acc, (_a = {}, _a[curr] = Modal && Modal[curr], _a)); }, {}); var mainPermission = features.reduce(function (acc, curr) { var _a; return __assign({}, acc, (_a = {}, _a[curr] = Main && Main[curr], _a)); }, {}); return merge_1.default(defaultAsyncFeaturesStore, (_a = {}, _a[app_bridge_1.Context.Main] = mainPermission, _a[app_bridge_1.Context.Modal] = modalPermission, _a)); } exports.setFeaturesAvailable = setFeaturesAvailable;