@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)**
165 lines (164 loc) • 6.84 kB
JavaScript
;
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 = require("../helper");
var types_1 = require("../types");
// 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;