UNPKG

@shopify/app-bridge-core

Version:

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

126 lines (125 loc) 5.14 kB
"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 (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.AppBridgeError = exports.AppActionType = exports.Action = void 0; exports.fromAction = fromAction; exports.invalidOriginAction = invalidOriginAction; exports.throwError = throwError; exports.isErrorEventName = isErrorEventName; exports.permissionAction = permissionAction; var types_1 = require("../types"); var helper_1 = require("../helper"); // 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 = 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 = AppActionType = {})); var AppBridgeError = /** @class */ (function (_super) { __extends(AppBridgeError, _super); function AppBridgeError(message) { var _this = _super.call(this, message) || this; _this.name = 'AppBridgeError'; _this.message = message; Object.setPrototypeOf(_this, AppBridgeError.prototype); if (typeof Error.captureStackTrace === 'function') { Error.captureStackTrace(_this, _this.constructor); } else { _this.stack = new Error(_this.message).stack; } return _this; } return AppBridgeError; }(Error)); exports.AppBridgeError = AppBridgeError; function fromAction(message, type, action) { var errorMessage = message ? "".concat(type, ": ").concat(message) : type; var error = new AppBridgeError(errorMessage); error.action = action; error.type = type; return error; } function invalidOriginAction(message) { return (0, helper_1.actionWrapper)({ group: types_1.Group.Error, payload: { message: message, type: Action.INVALID_ORIGIN, }, type: Action.INVALID_ORIGIN, }); } 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); } function isErrorEventName(eventName) { var match = (0, helper_1.findMatchInEnum)(Action, eventName); return typeof match === 'string'; } function errorActionWrapperWithId(type, action, message) { var castPayload = action.payload; return (0, helper_1.actionWrapper)({ type: type, group: types_1.Group.Error, payload: { action: action, message: message, type: type, id: castPayload && castPayload.id ? castPayload.id : undefined, }, }); } function permissionAction(action, message) { return errorActionWrapperWithId(Action.PERMISSION, action, message || 'Action is not permitted'); }