UNPKG

@shopify/app-bridge-host

Version:

App Bridge Host contains components and middleware to be consumed by the app's host, as well as the host itself. The middleware and `Frame` component are responsible for facilitating communication between the client and host, and used to act on actions se

111 lines (108 loc) 4.18 kB
import { __assign } from 'tslib'; import { Button } from '@shopify/app-bridge-core/actions'; var 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; } function isMobileMiddlewareAvailable() { return typeof window !== 'undefined' && typeof window.__MOBILE_MIDDLEWARE__ === 'function'; } function isMobileMiddlewareSupported() { return (typeof navigator !== 'undefined' && navigator.userAgent.indexOf('MobileMiddlewareSupported') >= 0); } function isMobile() { return isShopifyMobile() || isShopifyPOS() || isShopifyPing(); } function isShopifyMobile() { return typeof navigator !== 'undefined' && navigator.userAgent.indexOf('Shopify Mobile') >= 0; } function isShopifyPOS() { return typeof navigator !== 'undefined' && navigator.userAgent.indexOf('Shopify POS') >= 0; } function isShopifyPing() { return typeof navigator !== 'undefined' && navigator.userAgent.indexOf('Shopify Ping') >= 0; } function isLegacyShopifyMobile() { return isShopifyMobile() && !isMobileMiddlewareSupported(); } function isLegacyShopifyPOS() { return isShopifyPOS() && !isMobileMiddlewareSupported(); } function applyPrintToLegacyButton(button) { if (button.icon === Button.Icon.Print) { button.print = true; } } function buildCallbackQueueItem(message, action, data) { return buildQueueItem(message, __assign(__assign({}, data), { callbackId: action.payload.id, action: action })); } function buildQueueItem(message, data) { return { message: message, data: data, }; } 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 (Object.prototype.hasOwnProperty.call(current, 'buttons') && Array.isArray(current.buttons)) { var inner = traverseButtonPayload(current.buttons, onEach, legacyMutate); if (legacyMutate) { current.links = inner; } } }); return payload; } /** * 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 (data.cartDiscounts) { data.cartDiscounts = data.cartDiscounts.map(fixDiscount); } 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; } export { CART_PERMISSION_MESSAGE, applyPrintToLegacyButton, buildCallbackQueueItem, buildQueueItem, fixDiscountInCartResponse, isCartAction, isLegacyShopifyMobile, isLegacyShopifyPOS, isMobile, isMobileMiddlewareAvailable, isMobileMiddlewareSupported, isShopifyMobile, isShopifyPOS, isShopifyPing, traverseButtonPayload };