UNPKG

@shopify/app-bridge-core

Version:

**[Join our team and work on libraries like this one.](https://www.shopify.ca/careers)**

119 lines (118 loc) 5.14 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); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Action = void 0; exports.validateProps = validateProps; exports.validateAction = validateAction; var type_validate_1 = require("../type-validate"); var utils_1 = require("../utils"); var Modal_1 = require("../../actions/Modal"); Object.defineProperty(exports, "Action", { enumerable: true, get: function () { return Modal_1.Action; } }); var Button_1 = require("../../actions/Button"); var safe_redirect_1 = require("../safe-redirect"); var button_1 = require("./button"); function matchesSafeOrigin(value, localOrigin) { var hostName; try { hostName = new URL(localOrigin).hostname; } catch (error) { return [ { error: 'invalid_app_origin', value: localOrigin, message: "Provided value for app origin: `".concat(localOrigin, "` is invalid"), }, ]; } var isSafeSrc = (0, safe_redirect_1.isSafe)(value, { requireAbsolute: true, requireSSL: true, allowInternalProtocol: true, allowedDomains: [hostName], }); if (!isSafeSrc) { return [ { error: 'not_matching_app_origin', value: value, message: "Provided URL origin does not match app origin `".concat(hostName, "`"), }, ]; } } function matchesSize() { return function (value) { var values = [Modal_1.Size.Small, Modal_1.Size.Medium, Modal_1.Size.Large]; if (values.includes(value)) { return; } var message = "expected:".concat(values.map(function (val) { return "`".concat(val, "`"); }).join(' or ')); if (value === Modal_1.Size.Full) { message += ". Size `".concat(value, "` is deprecated as of version 1.6.5 and will fall back to size `medium`"); } if (value === Modal_1.Size.Auto) { message += ". Size `".concat(value, "` is deprecated as of version 1.12.x and will fall back to size `medium`. Use the `setUpModalAutoSizing` utility from `app-bridge` instead"); } return [ { error: 'invalid_enum_value', value: value, message: message, }, ]; }; } function getModalSchema(props, localOrigin) { if (props === void 0) { props = {}; } var baseModalSchema = (0, type_validate_1.matchesObject)({ title: (0, type_validate_1.makeOptional)((0, type_validate_1.matchesString)()), footer: (0, type_validate_1.makeOptional)((0, type_validate_1.matchesObject)({ buttons: (0, type_validate_1.matchesObject)({ primary: (0, type_validate_1.makeOptional)(button_1.buttonSchemaWithId), secondary: (0, type_validate_1.makeOptional)((0, type_validate_1.matchesArray)(button_1.buttonSchemaWithId)), }), })), size: (0, type_validate_1.makeOptional)(matchesSize()), }); if ((0, Modal_1.isIframeModal)(props)) { if (props.url) { var urlSchema = (0, type_validate_1.matchesObject)({ url: (0, type_validate_1.composeSchemas)((0, type_validate_1.matchesString)(), function (value) { return localOrigin ? matchesSafeOrigin(value, localOrigin) : undefined; }), }); return (0, type_validate_1.composeSchemas)(baseModalSchema, urlSchema); } return (0, type_validate_1.composeSchemas)(baseModalSchema, utils_1.relativePathSchema); } return (0, type_validate_1.composeSchemas)(baseModalSchema, (0, type_validate_1.matchesObject)({ message: (0, type_validate_1.matchesString)() })); } function validateProps(props, localOrigin) { return (0, type_validate_1.validate)(props, getModalSchema(props, localOrigin)); } function validateAction(action, localOrigin) { var schema = getModalSchema(action.payload, localOrigin); switch (action.type) { case Modal_1.Action.OPEN: case Modal_1.Action.UPDATE: return (0, type_validate_1.validate)(action, (0, utils_1.createActionValidator)(Modal_1.Action, schema, true, action.type === Modal_1.Action.UPDATE)); case Modal_1.Action.FOOTER_BUTTON_CLICK: return (0, button_1.validateAction)(__assign(__assign({}, action), { type: Button_1.Action.CLICK })); case Modal_1.Action.FOOTER_BUTTON_UPDATE: return (0, button_1.validateAction)(__assign(__assign({}, action), { type: Button_1.Action.UPDATE })); case Modal_1.Action.CLOSE: default: return (0, type_validate_1.validate)(action, (0, utils_1.createActionValidator)(Modal_1.Action)); } }