@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
130 lines (129 loc) • 4.85 kB
JavaScript
;
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 });
var actions_1 = require("@shopify/app-bridge/actions");
exports.CART_PERMISSION_MESSAGE = 'Missing read_orders or write_orders permission to perform this action';
function isCartAction(action) {
return action && action.type.indexOf('APP::CART::') === 0;
}
exports.isCartAction = isCartAction;
function isMobileMiddlewareAvailable() {
return typeof window !== 'undefined' && typeof window.__MOBILE_MIDDLEWARE__ === 'function';
}
exports.isMobileMiddlewareAvailable = isMobileMiddlewareAvailable;
function isMobileMiddlewareSupported() {
return (typeof navigator !== 'undefined' &&
navigator.userAgent.indexOf('MobileMiddlewareSupported') >= 0);
}
exports.isMobileMiddlewareSupported = isMobileMiddlewareSupported;
function isMobile() {
return isShopifyMobile() || isShopifyPOS() || isShopifyPing();
}
exports.isMobile = isMobile;
function isShopifyMobile() {
return typeof navigator !== 'undefined' && navigator.userAgent.indexOf('Shopify Mobile') >= 0;
}
exports.isShopifyMobile = isShopifyMobile;
function isShopifyPOS() {
return typeof navigator !== 'undefined' && navigator.userAgent.indexOf('Shopify POS') >= 0;
}
exports.isShopifyPOS = isShopifyPOS;
function isShopifyPing() {
return typeof navigator !== 'undefined' && navigator.userAgent.indexOf('Shopify Ping') >= 0;
}
exports.isShopifyPing = isShopifyPing;
function isLegacyShopifyMobile() {
return isShopifyMobile() && !isMobileMiddlewareSupported();
}
exports.isLegacyShopifyMobile = isLegacyShopifyMobile;
function isLegacyShopifyPOS() {
return isShopifyPOS() && !isMobileMiddlewareSupported();
}
exports.isLegacyShopifyPOS = isLegacyShopifyPOS;
function applyPrintToLegacyButton(button) {
if (button.icon === actions_1.Button.Icon.Print) {
button.print = true;
}
}
exports.applyPrintToLegacyButton = applyPrintToLegacyButton;
function buildCallbackQueueItem(message, action, data) {
return buildQueueItem(message, __assign({}, data, { callbackId: action.payload.id, action: action }));
}
exports.buildCallbackQueueItem = buildCallbackQueueItem;
function buildQueueItem(message, data) {
return {
message: message,
data: data,
};
}
exports.buildQueueItem = buildQueueItem;
function traverseButtonPayload(payload, onEach, legacyMutate) {
if (legacyMutate === void 0) { legacyMutate = false; }
payload.forEach(function (current) {
if (onEach) {
onEach(current);
}
if (legacyMutate) {
current.message = current.id;
}
if (current.hasOwnProperty('buttons') && Array.isArray(current.buttons)) {
var inner = traverseButtonPayload(current.buttons, onEach, legacyMutate);
if (legacyMutate) {
current.links = inner;
}
}
});
return payload;
}
exports.traverseButtonPayload = traverseButtonPayload;
/**
* Ref https://github.com/shopify/web/pull/10693
*/
function fixDiscount(discount) {
if (typeof discount === 'object') {
if (typeof discount.amount === 'object') {
Object.assign(discount, discount.amount);
}
if (typeof discount.code === 'object') {
Object.assign(discount, discount.code);
}
}
return discount;
}
function fixDiscountInCartResponse(data, supportBothDiscountPayload) {
if (supportBothDiscountPayload === void 0) { supportBothDiscountPayload = true; }
if (data.cartDiscount) {
data.cartDiscount = fixDiscount(data.cartDiscount);
}
if (Array.isArray(data.lineItems)) {
data.lineItems.forEach(function (lineItem) {
if (Array.isArray(lineItem.discounts) && lineItem.discounts.length > 0) {
lineItem.discounts = lineItem.discounts.map(fixDiscount);
if (!lineItem.discount) {
lineItem.discount = lineItem.discounts[0];
}
}
else if (typeof lineItem.discount === 'object') {
lineItem.discount = fixDiscount(lineItem.discount);
if (!Array.isArray(lineItem.discounts)) {
lineItem.discounts = [lineItem.discount];
}
}
if (!supportBothDiscountPayload) {
delete lineItem.discounts;
}
});
}
return data;
}
exports.fixDiscountInCartResponse = fixDiscountInCartResponse;