UNPKG

@shopify/app-bridge

Version:

**Shopify is doubling our engineering staff in 2021! [Join our team and work on libraries like this one.](https://smrtr.io/5GGrc)**

1,286 lines (1,255 loc) 327 kB
(function webpackUniversalModuleDefinition(root, factory) { if(typeof exports === 'object' && typeof module === 'object') module.exports = factory(); else if(typeof define === 'function' && define.amd) define("app-bridge", [], factory); else if(typeof exports === 'object') exports["app-bridge"] = factory(); else root["app-bridge"] = factory(); })(window, function() { return /******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {}; /******/ /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ /******/ // Check if module is in cache /******/ if(installedModules[moduleId]) { /******/ return installedModules[moduleId].exports; /******/ } /******/ // Create a new module (and put it into the cache) /******/ var module = installedModules[moduleId] = { /******/ i: moduleId, /******/ l: false, /******/ exports: {} /******/ }; /******/ /******/ // Execute the module function /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); /******/ /******/ // Flag the module as loaded /******/ module.l = true; /******/ /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ /******/ /******/ // expose the modules object (__webpack_modules__) /******/ __webpack_require__.m = modules; /******/ /******/ // expose the module cache /******/ __webpack_require__.c = installedModules; /******/ /******/ // define getter function for harmony exports /******/ __webpack_require__.d = function(exports, name, getter) { /******/ if(!__webpack_require__.o(exports, name)) { /******/ Object.defineProperty(exports, name, { enumerable: true, get: getter }); /******/ } /******/ }; /******/ /******/ // define __esModule on exports /******/ __webpack_require__.r = function(exports) { /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); /******/ } /******/ Object.defineProperty(exports, '__esModule', { value: true }); /******/ }; /******/ /******/ // create a fake namespace object /******/ // mode & 1: value is a module id, require it /******/ // mode & 2: merge all properties of value into the ns /******/ // mode & 4: return value when already ns object /******/ // mode & 8|1: behave like require /******/ __webpack_require__.t = function(value, mode) { /******/ if(mode & 1) value = __webpack_require__(value); /******/ if(mode & 8) return value; /******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value; /******/ var ns = Object.create(null); /******/ __webpack_require__.r(ns); /******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value }); /******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key)); /******/ return ns; /******/ }; /******/ /******/ // getDefaultExport function for compatibility with non-harmony modules /******/ __webpack_require__.n = function(module) { /******/ var getter = module && module.__esModule ? /******/ function getDefault() { return module['default']; } : /******/ function getModuleExports() { return module; }; /******/ __webpack_require__.d(getter, 'a', getter); /******/ return getter; /******/ }; /******/ /******/ // Object.prototype.hasOwnProperty.call /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; /******/ /******/ // __webpack_public_path__ /******/ __webpack_require__.p = ""; /******/ /******/ /******/ // Load entry module and return exports /******/ return __webpack_require__(__webpack_require__.s = 43); /******/ }) /************************************************************************/ /******/ ([ /* 0 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); 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 __spreadArrays = (this && this.__spreadArrays) || function () { for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; for (var r = Array(s), k = 0, i = 0; i < il; i++) for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) r[k] = a[j]; return r; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.NonSnakeCaseGroup = exports.findMatchInEnum = exports.forEachInEnum = exports.getMergedProps = exports.updateActionFromPayload = exports.ActionSetWithChildren = exports.ActionSet = exports.isValidOptionalString = exports.isValidOptionalNumber = exports.getEventNameSpace = exports.getVersion = exports.actionWrapper = void 0; var types_1 = __webpack_require__(6); var collection_1 = __webpack_require__(13); var Error_1 = __webpack_require__(5); var constants_1 = __webpack_require__(20); var merge_1 = __importDefault(__webpack_require__(21)); var types_2 = __webpack_require__(1); var uuid_1 = __importDefault(__webpack_require__(46)); // eslint-disable-next-line @typescript-eslint/no-var-requires var packageJson = __webpack_require__(47); function actionWrapper(action) { return __assign(__assign({}, action), { version: getVersion(), clientInterface: { name: packageJson.name, version: getVersion(), } }); } exports.actionWrapper = actionWrapper; function getVersion() { return packageJson.version; } exports.getVersion = getVersion; /** * Returns full event name with prefix, group, subgroups and type formatted with separators * @internal * */ function getEventNameSpace(group, eventName, component) { if (eventName.startsWith("" + constants_1.PREFIX + constants_1.SEPARATOR)) { return eventName; } var eventNameSpace = groupToEventNameSpace(group); if (component) { var subgroups_1 = component.subgroups, type = component.type; if (subgroups_1 && subgroups_1.length > 0) { eventNameSpace += eventNameSpace.length > 0 ? constants_1.SEPARATOR : ''; subgroups_1.forEach(function (subgroup, index) { eventNameSpace += "" + subgroup.toUpperCase() + (index < subgroups_1.length - 1 ? constants_1.SEPARATOR : ''); }); } if (type !== group && type) { eventNameSpace += "" + (eventNameSpace.length > 0 ? constants_1.SEPARATOR : '') + type.toUpperCase(); } } if (eventNameSpace) { eventNameSpace += "" + (eventNameSpace.length > 0 ? constants_1.SEPARATOR : '') + eventName.toUpperCase(); } return "" + constants_1.PREFIX + constants_1.SEPARATOR + eventNameSpace; } exports.getEventNameSpace = getEventNameSpace; function isValidOptionalNumber(value) { return value === null || value === undefined || typeof value === 'number'; } exports.isValidOptionalNumber = isValidOptionalNumber; function isValidOptionalString(value) { return value === null || value === undefined || typeof value === 'string'; } exports.isValidOptionalString = isValidOptionalString; var ActionSet = /** @class */ (function () { function ActionSet(app, type, group, id) { var _this = this; this.app = app; this.type = type; this.group = group; this.subgroups = []; this.subscriptions = []; if (!app) { Error_1.throwError(Error_1.Action.INVALID_ACTION, 'Missing required `app`'); } this.id = id || uuid_1.default(); this.defaultGroup = group; var defaultSet = this.set; this.set = function () { var _a; var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } if (!_this.app.hooks) { return defaultSet.apply(_this, args); } return (_a = _this.app.hooks).run.apply(_a, __spreadArrays([types_1.LifecycleHook.UpdateAction, defaultSet, _this], args)); }; } ActionSet.prototype.set = function () { var _ = []; for (var _i = 0; _i < arguments.length; _i++) { _[_i] = arguments[_i]; } }; Object.defineProperty(ActionSet.prototype, "component", { get: function () { return { id: this.id, subgroups: this.subgroups, type: this.type, }; }, enumerable: false, configurable: true }); ActionSet.prototype.updateSubscription = function (subscriptionToRemove, group, subgroups) { var eventType = subscriptionToRemove.eventType, callback = subscriptionToRemove.callback, component = subscriptionToRemove.component; var currentIndex; currentIndex = this.subscriptions.findIndex(function (subscription) { return subscription === subscriptionToRemove; }); if (currentIndex >= 0) { this.subscriptions[currentIndex].unsubscribe(); } else { currentIndex = undefined; } this.group = group; this.subgroups = subgroups; Object.assign(component, { subgroups: this.subgroups }); return this.subscribe(eventType, callback, component, currentIndex); }; ActionSet.prototype.error = function (callback) { var _this = this; var subscriptionIndices = []; forEachInEnum(Error_1.Action, function (eventNameSpace) { // Keep track of subscription index so we can call unsubscribe later // This ensure it will continue to work even when the subscription has been updated subscriptionIndices.push(_this.subscriptions.length); _this.subscribe(eventNameSpace, callback); }); return function () { var subscriptionsToRemove = subscriptionIndices.map(function (index) { return _this.subscriptions[index]; }); subscriptionsToRemove.forEach(function (toRemove) { collection_1.removeFromCollection(_this.subscriptions, toRemove, function (removed) { removed.unsubscribe(); }); }); }; }; ActionSet.prototype.subscribe = function (eventName, callback, component, currentIndex) { var _this = this; var eventComponent = component || this.component; var eventType = eventName.toUpperCase(); var boundedCallback = typeof currentIndex === 'number' ? callback : callback.bind(this); var eventNameSpace; if (Error_1.isErrorEventName(eventName)) { eventNameSpace = getEventNameSpace(types_2.Group.Error, eventName, __assign(__assign({}, eventComponent), { type: '' })); } else { eventNameSpace = getEventNameSpace(this.group, eventName, eventComponent); } var unsubscribe = this.app.subscribe(eventNameSpace, boundedCallback, component ? component.id : this.id); var subscription = { eventType: eventType, unsubscribe: unsubscribe, callback: boundedCallback, component: eventComponent, updateSubscribe: function (group, subgroups) { return _this.updateSubscription(subscription, group, subgroups); }, }; if (typeof currentIndex === 'number' && currentIndex >= 0 && currentIndex < this.subscriptions.length) { this.subscriptions[currentIndex] = subscription; } else { this.subscriptions.push(subscription); } return unsubscribe; }; ActionSet.prototype.unsubscribe = function (resetOnly) { if (resetOnly === void 0) { resetOnly = false; } unsubscribeActions(this.subscriptions, this.defaultGroup, resetOnly); return this; }; return ActionSet; }()); exports.ActionSet = ActionSet; var ActionSetWithChildren = /** @class */ (function (_super) { __extends(ActionSetWithChildren, _super); function ActionSetWithChildren() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.children = []; return _this; } ActionSetWithChildren.prototype.unsubscribe = function (unsubscribeChildren, resetParentOnly) { if (unsubscribeChildren === void 0) { unsubscribeChildren = true; } if (resetParentOnly === void 0) { resetParentOnly = false; } unsubscribeActions(this.subscriptions, this.defaultGroup, resetParentOnly); this.children.forEach(function (child) { if (child instanceof ActionSetWithChildren) { child.unsubscribe(unsubscribeChildren, !unsubscribeChildren); } else { child.unsubscribe(!unsubscribeChildren); } }); return this; }; ActionSetWithChildren.prototype.getChild = function (id) { var childIndex = this.children.findIndex(function (child) { return child.id === id; }); return childIndex >= 0 ? this.children[childIndex] : undefined; }; ActionSetWithChildren.prototype.getChildIndex = function (id) { return this.children.findIndex(function (child) { return child.id === id; }); }; ActionSetWithChildren.prototype.getChildSubscriptions = function (id, eventType) { return this.subscriptions.filter(function (sub) { return sub.component.id === id && (!eventType || eventType === sub.eventType); }); }; ActionSetWithChildren.prototype.addChild = function (child, group, subgroups) { var _this = this; var subscriptions = child.subscriptions; var existingChild = this.getChild(child.id); // Add child if it doesn't already exist if (!existingChild) { this.children.push(child); } if (!subscriptions || (group === child.group && subgroups === child.subgroups)) { return this; } subscriptions.forEach(function (subscription) { var updateSubscribe = subscription.updateSubscribe; updateSubscribe(group, subgroups); }); // Update child's group and subgroups Object.assign(child, { group: group, subgroups: subgroups }); // Update child's children subscriptions if (child instanceof ActionSetWithChildren) { child.children.forEach(function (childIter) { return _this.addChild(childIter, group, subgroups); }); } return this; }; ActionSetWithChildren.prototype.removeChild = function (id) { var _this = this; collection_1.removeFromCollection(this.children, this.getChild(id), function () { var toBeRemoved = _this.subscriptions.filter(function (subs) { return subs.component.id === id; }); toBeRemoved.forEach(function (toRemove) { collection_1.removeFromCollection(_this.subscriptions, toRemove, function (removed) { removed.unsubscribe(); }); }); }); return this; }; ActionSetWithChildren.prototype.subscribeToChild = function (child, eventName, callback) { var _this = this; var boundedCallback = callback.bind(this); if (eventName instanceof Array) { eventName.forEach(function (eventNameIter) { return _this.subscribeToChild(child, eventNameIter, callback); }); return this; } if (typeof eventName !== 'string') { return this; } var eventType = eventName.toUpperCase(); var currentSubscriptions = this.getChildSubscriptions(child.id, eventType); if (currentSubscriptions.length > 0) { // Subscription is already there, simply update it currentSubscriptions.forEach(function (subs) { return subs.updateSubscribe(_this.group, child.subgroups); }); } else { var childComponent = { id: child.id, subgroups: child.subgroups, type: child.type, }; this.subscribe(eventType, boundedCallback, childComponent); } return this; }; ActionSetWithChildren.prototype.getUpdatedChildActions = function (newActions, currentActions) { if (newActions.length === 0) { while (currentActions.length > 0) { var action = currentActions.pop(); if (!action) { break; } this.removeChild(action.id); } return undefined; } // Only allow unique actions var uniqueActions = newActions.filter(function (action, index, actionsArr) { return index === actionsArr.indexOf(action); }); var newActionIds = uniqueActions.map(function (action) { return action.id; }); // Remove unused actions var unusedActions = currentActions.filter(function (action) { return newActionIds.indexOf(action.id) < 0; }); while (unusedActions.length > 0) { var action = unusedActions.pop(); if (!action) { break; } this.removeChild(action.id); } return uniqueActions; }; return ActionSetWithChildren; }(ActionSet)); exports.ActionSetWithChildren = ActionSetWithChildren; function unsubscribeActions(subscriptions, defaultGroup, reassign) { if (reassign === void 0) { reassign = false; } subscriptions.forEach(function (subscription) { if (reassign) { var updateSubscribe = subscription.updateSubscribe; // eslint-disable-next-line no-warning-comments // TODO: Support cases where we don't wipe out group and subgroups to defaults updateSubscribe(defaultGroup, []); } else { var unsubscribe = subscription.unsubscribe; unsubscribe(); } }); if (!reassign) { subscriptions.length = 0; } } function updateActionFromPayload(action, newProps) { var id = action.id; if (id === newProps.id) { // Merge new properties Object.assign(action, getMergedProps(action, newProps)); return true; } return false; } exports.updateActionFromPayload = updateActionFromPayload; function getMergedProps(props, newProps) { var merged = merge_1.default(props, newProps); if (!merged) { // tslint:disable-next-line:prefer-object-spread var cloned = Object.assign(props, newProps); return cloned; } return merged; } exports.getMergedProps = getMergedProps; function forEachInEnum(types, callback) { Object.keys(types).forEach(function (key) { callback(types[key]); }); } exports.forEachInEnum = forEachInEnum; function findMatchInEnum(types, lookup) { var match = Object.keys(types).find(function (key) { return lookup === types[key]; }); return match ? types[match] : undefined; } exports.findMatchInEnum = findMatchInEnum; function camelCaseToSnakeCase(value) { return value.replace(/([A-Z])/g, function (matcher, _val, index) { return "" + (index === 0 ? '' : '_') + matcher[0].toLowerCase(); }); } exports.NonSnakeCaseGroup = [ types_2.Group.AuthCode, types_2.Group.Button, types_2.Group.ButtonGroup, types_2.Group.Cart, types_2.Group.Error, types_2.Group.Features, types_2.Group.Fullscreen, types_2.Group.Link, types_2.Group.Loading, types_2.Group.Menu, types_2.Group.Modal, types_2.Group.Navigation, types_2.Group.Pos, types_2.Group.Print, types_2.Group.ResourcePicker, types_2.Group.Scanner, types_2.Group.SessionToken, types_2.Group.Share, types_2.Group.TitleBar, types_2.Group.Toast, ]; /** * Maps the group name to its event name * @internal * @remarks - This method is necessary for the new pattern of using snake case * which makes it more readable and easier to reconstruct the group from an event name. * Example: `ContextualSaveBar` becomes `CONTEXTUAL_SAVE_BAR` * */ function groupToEventNameSpace(group) { if (exports.NonSnakeCaseGroup.includes(group)) { return group.toUpperCase(); } return camelCaseToSnakeCase(group).toUpperCase(); } /***/ }), /* 1 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ComponentType = exports.Group = void 0; /** * Various action groups. * @public */ var Group; (function (Group) { Group["AuthCode"] = "AuthCode"; Group["Button"] = "Button"; Group["ButtonGroup"] = "ButtonGroup"; Group["Cart"] = "Cart"; Group["Client"] = "Client"; Group["ContextualSaveBar"] = "ContextualSaveBar"; Group["Error"] = "Error"; Group["Features"] = "Features"; Group["FeedbackModal"] = "FeedbackModal"; Group["Fullscreen"] = "Fullscreen"; Group["LeaveConfirmation"] = "LeaveConfirmation"; Group["Link"] = "Link"; Group["Loading"] = "Loading"; Group["Menu"] = "Menu"; Group["Modal"] = "Modal"; Group["Navigation"] = "Navigation"; Group["Performance"] = "Performance"; Group["Pos"] = "Pos"; Group["Print"] = "Print"; Group["ResourcePicker"] = "Resource_Picker"; Group["Scanner"] = "Scanner"; Group["SessionToken"] = "SessionToken"; Group["Share"] = "Share"; Group["TitleBar"] = "TitleBar"; Group["Toast"] = "Toast"; Group["MarketingExternalActivityTopBar"] = "MarketingExternalActivityTopBar"; })(Group = exports.Group || (exports.Group = {})); /** * @internal */ var ComponentType; (function (ComponentType) { ComponentType["Button"] = "Button"; ComponentType["ButtonGroup"] = "ButtonGroup"; })(ComponentType = exports.ComponentType || (exports.ComponentType = {})); /***/ }), /* 2 */ /***/ (function(module, exports, __webpack_require__) { "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 __spreadArrays = (this && this.__spreadArrays) || function () { for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; for (var r = Array(s), k = 0, i = 0; i < il; i++) for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) r[k] = a[j]; return r; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.validate = exports.composeSchemas = exports.makeOptional = exports.oneOf = exports.matchesObject = exports.matchesArray = exports.matchesEnum = exports.matchesPositiveNumber = exports.matchesPositiveInteger = exports.matchesString = exports.matchesBoolean = void 0; var TYPE_ERROR = 'type_error_expected'; function constructErrors(value, error, options) { if (options === void 0) { options = { message: undefined }; } return [ { value: value, error: error, message: typeof options.message === 'function' ? options.message(error, value) : options.message, }, ]; } function getErrors(obj, validator, key) { var value = key ? obj[key] : obj; var path = key ? "['" + key + "']" : undefined; var error = validator(value); if (!error) { return; } return error.map(function (errorObj) { return (__assign(__assign({}, errorObj), { path: "" + (path || '') + (errorObj.path || '') || undefined })); }); } function matchesBoolean(options) { return function (value) { return typeof value === 'boolean' ? undefined : constructErrors(value, TYPE_ERROR + "_boolean", options); }; } exports.matchesBoolean = matchesBoolean; function matchesString(options) { return function (value) { return typeof value === 'string' ? undefined : constructErrors(value, TYPE_ERROR + "_string", options); }; } exports.matchesString = matchesString; function matchesPositiveInteger(options) { return function (value) { return !Number.isInteger(value) || value < 0 ? constructErrors(value, TYPE_ERROR + "_integer", options) : undefined; }; } exports.matchesPositiveInteger = matchesPositiveInteger; function matchesPositiveNumber(options) { return function (value) { return Number.isNaN(value) || !Number.isFinite(value) || value < 0 ? constructErrors(value, TYPE_ERROR + "_number", options) : undefined; }; } exports.matchesPositiveNumber = matchesPositiveNumber; /** * Returns a validator that matches values in the given enum * @param type - enum to use for match values * @public */ function matchesEnum(types, options) { return function (value) { var values = Object.keys(types).map(function (key) { return types[key]; }); var message = (options && options.message) || "expected:" + values.map(function (val) { return "`" + val + "`"; }).join(' or '); return values.includes(value) ? undefined : constructErrors(value, 'invalid_enum_value', __assign(__assign({}, options), { message: message })); }; } exports.matchesEnum = matchesEnum; function matchesArray(validator, options) { return function (value) { if (!Array.isArray(value)) { return constructErrors(value, TYPE_ERROR + "_array", options); } if (!validator) { return; } var errors = []; value.forEach(function (val, key) { var objectError = validator(val); if (objectError) { errors = errors.concat(objectError.map(function (error) { return (__assign(__assign({}, error), { path: "['" + key + "']" + (error.path || '') })); })); } }); return errors.length ? errors : undefined; }; } exports.matchesArray = matchesArray; function matchesObject(schema, options) { return function (val) { if (typeof val !== 'object' || !val || Array.isArray(val)) { return constructErrors(val, TYPE_ERROR + "_object", options); } var flattened = Object.keys(schema).reduce(function (acc, key) { return __spreadArrays(acc, (getErrors(val, schema[key], key) || [])); }, []); return flattened.length ? flattened : undefined; }; } exports.matchesObject = matchesObject; function oneOf() { var validators = []; for (var _i = 0; _i < arguments.length; _i++) { validators[_i] = arguments[_i]; } return function (val) { var errors = []; for (var _i = 0, validators_1 = validators; _i < validators_1.length; _i++) { var validator = validators_1[_i]; var result = validator(val); if (result == null) return result; errors.push.apply(errors, result); } return errors; }; } exports.oneOf = oneOf; function makeOptional(validator) { return function (value) { if (value === undefined || value === null) { return undefined; } return validator(value); }; } exports.makeOptional = makeOptional; function composeSchemas() { var validators = []; for (var _i = 0; _i < arguments.length; _i++) { validators[_i] = arguments[_i]; } return function (val) { var error; var i = 0; var len = validators.length; while (!error && i < len) { error = validators[i](val); if (error) { return error; } i++; } }; } exports.composeSchemas = composeSchemas; function validate(obj, validator) { return getErrors(obj, validator); } exports.validate = validate; /***/ }), /* 3 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.mixedAppClientCheck = exports.relativePathSchema = exports.relativeUrlSchema = exports.isValidRelativePath = exports.actionMessage = exports.createActionValidator = void 0; var type_validate_1 = __webpack_require__(2); function createActionValidator(type, payloadSchema, payloadRequired, idRequired) { if (payloadSchema === void 0) { payloadSchema = undefined; } if (payloadRequired === void 0) { payloadRequired = false; } if (idRequired === void 0) { idRequired = false; } var idSchema = type_validate_1.matchesObject({ id: idRequired ? type_validate_1.matchesString() : type_validate_1.makeOptional(type_validate_1.matchesString()), }); var schema = payloadSchema ? type_validate_1.composeSchemas(idSchema, payloadSchema) : idSchema; return type_validate_1.matchesObject({ type: type_validate_1.matchesEnum(type, { message: function (_, val) { return "The action type `" + val + "` is invalid or unsupported"; }, }), payload: payloadRequired ? schema : type_validate_1.makeOptional(schema), }); } exports.createActionValidator = createActionValidator; function actionMessage(errors) { return errors .map(function (err) { var path = err.path, error = err.error, message = err.message, value = err.value; var valueStr = typeof value === 'object' ? JSON.stringify(value) : value; return "`" + error + "` thrown for" + (path ? " path: " + path + " and" : '') + " value: `" + valueStr + "`" + (message ? " with message: " + message : ''); }) .join(' | '); } exports.actionMessage = actionMessage; function isValidRelativePath(path) { return typeof path === 'string' && (path === '' || path.startsWith('/')); } exports.isValidRelativePath = isValidRelativePath; exports.relativeUrlSchema = type_validate_1.composeSchemas(type_validate_1.matchesString(), function (value) { return isValidRelativePath(value) ? undefined : [{ error: 'invalid_relative_path', value: value, message: 'expected string to start with `/`' }]; }); exports.relativePathSchema = type_validate_1.matchesObject({ path: exports.relativeUrlSchema, }); function mixedAppClientCheck(window) { window.addEventListener('DOMContentLoaded', function () { if (!window.ShopifyApp) { return; } // eslint-disable-next-line no-console console.error("%cException Detected \uD83D\uDEAB\n\n%cAn instance of the EASDK client was detected while initializing Shopify App Bridge. Using Shopify App Bridge and the EASDK simultaneously is not supported.\n\nIf you're migrating an existing app that was built with the shopify_app gem, then the EASDK client might have been included in the home page view template. In this case, remove it from your app before initializing Shopify App Bridge again.", 'font-size: large;', 'font-size: normal;'); }, { once: true }); } exports.mixedAppClientCheck = mixedAppClientCheck; /***/ }), /* 4 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); 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); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.create = exports.Button = exports.isValidButtonProps = exports.update = exports.clickButton = exports.Style = exports.Icon = exports.Action = void 0; var helper_1 = __webpack_require__(0); var types_1 = __webpack_require__(1); var Action; (function (Action) { Action["CLICK"] = "CLICK"; Action["UPDATE"] = "UPDATE"; })(Action = exports.Action || (exports.Action = {})); var Icon; (function (Icon) { Icon["Print"] = "print"; })(Icon = exports.Icon || (exports.Icon = {})); var Style; (function (Style) { Style["Danger"] = "danger"; })(Style = exports.Style || (exports.Style = {})); function clickButton(group, component, payload) { var id = component.id; var action = helper_1.getEventNameSpace(group, Action.CLICK, component); var buttonPayload = { id: id, payload: payload, }; return helper_1.actionWrapper({ type: action, group: group, payload: buttonPayload }); } exports.clickButton = clickButton; function update(group, component, props) { var id = component.id; var label = props.label; var action = helper_1.getEventNameSpace(group, Action.UPDATE, component); var buttonPayload = __assign(__assign({}, props), { id: id, label: label }); return helper_1.actionWrapper({ type: action, group: group, payload: buttonPayload }); } exports.update = update; function isValidButtonProps(button) { return typeof button.id === 'string' && typeof button.label === 'string'; } exports.isValidButtonProps = isValidButtonProps; var Button = /** @class */ (function (_super) { __extends(Button, _super); function Button(app, options) { var _this = _super.call(this, app, types_1.ComponentType.Button, types_1.Group.Button) || this; _this.disabled = false; _this.loading = false; _this.plain = false; _this.set(options, false); return _this; } Object.defineProperty(Button.prototype, "options", { get: function () { return { disabled: this.disabled, icon: this.icon, label: this.label, style: this.style, loading: this.loading, plain: this.plain, }; }, enumerable: false, configurable: true }); Object.defineProperty(Button.prototype, "payload", { get: function () { return __assign(__assign({}, this.options), { id: this.id }); }, enumerable: false, configurable: true }); Button.prototype.set = function (options, shouldUpdate) { if (shouldUpdate === void 0) { shouldUpdate = true; } var mergedOptions = helper_1.getMergedProps(this.options, options); var label = mergedOptions.label, disabled = mergedOptions.disabled, icon = mergedOptions.icon, style = mergedOptions.style, loading = mergedOptions.loading, plain = mergedOptions.plain; this.label = label; this.disabled = Boolean(disabled); this.icon = icon; this.style = style; this.loading = Boolean(loading); this.plain = Boolean(plain); if (shouldUpdate) { this.dispatch(Action.UPDATE); } return this; }; Button.prototype.dispatch = function (action, payload) { switch (action) { case Action.CLICK: this.app.dispatch(clickButton(this.group, this.component, payload)); break; case Action.UPDATE: { var updateAction = update(this.group, this.component, this.payload); this.app.dispatch(updateAction); break; } } return this; }; return Button; }(helper_1.ActionSet)); exports.Button = Button; function create(app, options) { return new Button(app, options); } exports.create = create; /***/ }), /* 5 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.throwError = exports.fromAction = exports.AppBridgeError = exports.invalidOriginAction = exports.isErrorEventName = exports.permissionAction = exports.networkAction = exports.persistenceAction = exports.unsupportedOperationAction = exports.unexpectedAction = exports.invalidAction = exports.invalidActionType = exports.invalidPayload = exports.Message = exports.AppActionType = exports.Action = void 0; var helper_1 = __webpack_require__(0); var types_1 = __webpack_require__(1); // Errors triggered in response to an action var Action; (function (Action) { Action["INVALID_ACTION"] = "APP::ERROR::INVALID_ACTION"; Action["INVALID_ACTION_TYPE"] = "APP::ERROR::INVALID_ACTION_TYPE"; Action["INVALID_PAYLOAD"] = "APP::ERROR::INVALID_PAYLOAD"; Action["INVALID_OPTIONS"] = "APP::ERROR::INVALID_OPTIONS"; Action["UNEXPECTED_ACTION"] = "APP::ERROR::UNEXPECTED_ACTION"; Action["PERSISTENCE"] = "APP::ERROR::PERSISTENCE"; Action["UNSUPPORTED_OPERATION"] = "APP::ERROR::UNSUPPORTED_OPERATION"; Action["NETWORK"] = "APP::ERROR::NETWORK"; Action["PERMISSION"] = "APP::ERROR::PERMISSION"; Action["FAILED_AUTHENTICATION"] = "APP::ERROR::FAILED_AUTHENTICATION"; Action["INVALID_ORIGIN"] = "APP::ERROR::INVALID_ORIGIN"; })(Action = exports.Action || (exports.Action = {})); // Errors thrown in response to app setup var AppActionType; (function (AppActionType) { AppActionType["INVALID_CONFIG"] = "APP::ERROR::INVALID_CONFIG"; AppActionType["MISSING_CONFIG"] = "APP::APP_ERROR::MISSING_CONFIG"; AppActionType["MISSING_APP_BRIDGE_MIDDLEWARE"] = "APP::APP_ERROR::MISSING_APP_BRIDGE_MIDDLEWARE"; AppActionType["WINDOW_UNDEFINED"] = "APP::APP_ERROR::WINDOW_UNDEFINED"; AppActionType["REDUX_REINSTANTIATED"] = "APP::APP_ERROR::REDUX_REINSTANTIATED"; AppActionType["MISSING_LOCAL_ORIGIN"] = "APP::APP_ERROR::MISSING_LOCAL_ORIGIN"; AppActionType["MISSING_HOST_PROVIDER"] = "APP::APP_ERROR::MISSING_HOST_PROVIDER"; AppActionType["MISSING_ROUTER_CONTEXT"] = "APP::APP_ERROR::MISSING_ROUTER_CONTEXT"; AppActionType["MISSING_HISTORY_BLOCK"] = "APP::APP_ERROR::MISSING_HISTORY_BLOCK"; })(AppActionType = exports.AppActionType || (exports.AppActionType = {})); function errorActionWrapperWithId(type, action, message) { var castPayload = action.payload; return helper_1.actionWrapper({ type: type, group: types_1.Group.Error, payload: { action: action, message: message, type: type, id: castPayload && castPayload.id ? castPayload.id : undefined, }, }); } var Message; (function (Message) { Message["MISSING_PAYLOAD"] = "Missing payload"; Message["INVALID_PAYLOAD_ID"] = "Id in payload is missing or invalid"; })(Message = exports.Message || (exports.Message = {})); function invalidPayload(action, message) { return errorActionWrapperWithId(Action.INVALID_PAYLOAD, action, message || "The action's payload is missing required properties or has invalid properties"); } exports.invalidPayload = invalidPayload; function invalidActionType(action, message) { return helper_1.actionWrapper({ group: types_1.Group.Error, payload: { action: action, message: message || 'The action type is invalid or unsupported', type: Action.INVALID_ACTION_TYPE, }, type: Action.INVALID_ACTION_TYPE, }); } exports.invalidActionType = invalidActionType; function invalidAction(action, message) { return helper_1.actionWrapper({ group: types_1.Group.Error, payload: { action: action, message: message || "The action's has missing/invalid values for `group`, `type` or `version`", type: Action.INVALID_ACTION, }, type: Action.INVALID_ACTION, }); } exports.invalidAction = invalidAction; function unexpectedAction(action, message) { return helper_1.actionWrapper({ group: types_1.Group.Error, payload: { action: action, message: message || 'Action cannot be called at this time', type: Action.UNEXPECTED_ACTION, }, type: Action.UNEXPECTED_ACTION, }); } exports.unexpectedAction = unexpectedAction; function unsupportedOperationAction(action, message) { return errorActionWrapperWithId(Action.UNSUPPORTED_OPERATION, action, message || 'The action type is unsupported'); } exports.unsupportedOperationAction = unsupportedOperationAction; function persistenceAction(action, message) { return errorActionWrapperWithId(Action.PERSISTENCE, action, message || 'Action cannot be persisted on server'); } exports.persistenceAction = persistenceAction; function networkAction(action, message) { return errorActionWrapperWithId(Action.NETWORK, action, message || 'Network error'); } exports.networkAction = networkAction; function permissionAction(action, message) { return errorActionWrapperWithId(Action.PERMISSION, action, message || 'Action is not permitted'); } exports.permissionAction = permissionAction; function isErrorEventName(eventName) { var match = helper_1.findMatchInEnum(Action, eventName); return typeof match === 'string'; } exports.isErrorEventName = isErrorEventName; function invalidOriginAction(message) { return helper_1.actionWrapper({ group: types_1.Group.Error, payload: { message: message, type: Action.INVALID_ORIGIN, }, type: Action.INVALID_ORIGIN, }); } exports.invalidOriginAction = invalidOriginAction; var AppBridgeError = /** @class */ (function () { function AppBridgeError(message) { this.name = 'AppBridgeError'; this.message = message; if (typeof Error.captureStackTrace === 'function') { Error.captureStackTrace(this, this.constructor); } else { this.stack = new Error(this.message).stack; } } return AppBridgeError; }()); exports.AppBridgeError = AppBridgeError; AppBridgeError.prototype = Object.create(Error.prototype); function fromAction(message, type, action) { var errorMessage = message ? type + ": " + message : type; var error = new AppBridgeError(errorMessage); error.action = action; error.type = type; return error; } exports.fromAction = fromAction; function throwError() { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } var type = args[0]; var message; var action; if (typeof args[1] === 'string') { message = args[1]; } else { action = args[1]; message = args[2] || ''; } throw fromAction(message, type, action); } exports.throwError = throwError; /***/ }), /* 6 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LifecycleHook = exports.MessageType = exports.PermissionType = exports.isV1Config = void 0; /** * @internal * @param config */ function isV1Config(config) { return config.shopOrigin !== undefined; } exports.isV1Config = isV1Config; /** * @internal */ var PermissionType; (function (PermissionType) { PermissionType["Dispatch"] = "Dispatch"; PermissionType["Subscribe"] = "Subscribe"; })(PermissionType = exports.PermissionType || (exports.PermissionType = {})); /** * @internal */ var MessageType; (function (MessageType) { MessageType["GetState"] = "getState"; MessageType["Dispatch"] = "dispatch"; MessageType["Subscribe"] = "subscribe"; MessageType["Unsubscribe"] = "unsubscribe"; })(MessageType = exports.MessageType || (exports.MessageType = {})); /** * * There are two types of hooks: `LifecycleHook.DispatchAction` and `LifecycleHook.UpdateAction`. * * @remarks * `DispatchAction` hooks are run when an action is dispatched with the `.dispatch()` function: * * ```js * const toastNotice = Toast.create(app, {message: 'Product saved'}); * toastNotice.dispatch(Toast.Action.SHOW); * ``` * * `UpdateAction` hooks are run when an action is updated, using the `.set()` function: * * ```js * toastNotice.set({message: 'Product could not be saved', isError: true}); * ``` * * @public */ var LifecycleHook; (function (LifecycleHook) { LifecycleHook["UpdateAction"] = "UpdateAction"; LifecycleHook["DispatchAction"] = "DispatchAction"; })(LifecycleHook = exports.LifecycleHook || (exports.LifecycleHook = {})); /***/ }), /* 7 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); 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); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.create = exports.ButtonGroup = exports.isGroupedButtonPayload = exports.isGroupedButton = exports.update = exports.Action = void 0; var buttonHelper_1 = __webpack_require__(10); var helper_1 = __webpack_require__(0); var types_1 = __webpack_require__(1); var Action; (function (Action) { Action["UPDATE"] = "UPDATE"; })(Action = exports.Action || (exports.Action = {})); function update(group, component, props) { return buttonActionWrapper(group, component, Action.UPDATE, props); } exports.update = update; function isGroupedButton(options) { var castOptions = options; return castOptions.buttons && castOptions.buttons.length > 0 && castOptions.label !== undefined; } exports.isGroupedButton = isGroupedButton; function isGroupedButtonPayload(payload) { var castOptions = payload; return (Array.isArray(castOptions.buttons) && typeof castOptions.id === 'string' && typeof castOptions.label === 'string'); } exports.isGroupedButtonPayload = isGroupedButtonPayload; var ButtonGroup = /** @class */ (function (_super) { __extends(ButtonGroup, _super); function ButtonGroup(app, options) { var _this = _super.call(this, app, types_1.ComponentType.ButtonGroup, types_1.Group.ButtonGroup) || this; _this.disabled = false; _this.plain = false; _this.buttonsOptions = []; _this.buttons = []; _this.set(options, false); return _this; } Object.defineProperty(ButtonGroup.prototype, "options", { get: function () { return { buttons: this.buttonsOptions, disabled: this.disabled, label: this.label, plain: this.plain, }; }, enumerable: false, configurable: true }); Object.defineProperty(ButtonGroup.prototype, "payload", { get: function () { return __assign(__assign({}, this.options), { buttons: this.buttons, id: this.id }); }, enumerable: false, configurable: true }); ButtonGroup.prototype.set = function (options, shouldUpdate) { if (shouldUpdate === void 0) { shouldUpdate = true; } var mergedOptions = helper_1.getMergedProps(this.options, options); var label = mergedOptions.label, disabled = mergedOptions.disabled, buttons = mergedOptions.buttons, plain = mergedOptions.plain; this.label = label; this.disabled = Boolean(disabled); this.buttons = this.getButtons(buttons); this.plain = Boolean(plain); if (shouldUpdate) { this.dispatch(Action.UPDATE); } return this; }; ButtonGroup.prototype.dispatch = function (action) { switch (action) { case Action.UPDATE: { var updateAction = update(this.group, this.component, this.payload); this.app.dispatch(updateAction); break; }