UNPKG

@shopify/hydrogen-react

Version:

React components, hooks, and utilities for creating custom Shopify storefronts

93 lines (92 loc) 3.75 kB
"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const jsxRuntime = require("react/jsx-runtime"); const React = require("react"); const storefrontApiConstants = require("./storefront-api-constants.js"); const storefrontClient = require("./storefront-client.js"); const defaultShopifyContext = { storeDomain: "test", storefrontToken: "abc123", storefrontApiVersion: storefrontApiConstants.SFAPI_VERSION, countryIsoCode: "US", languageIsoCode: "EN", getStorefrontApiUrl() { return ""; }, getPublicTokenHeaders() { return {}; }, getShopifyDomain() { return ""; } }; const ShopifyContext = React.createContext( defaultShopifyContext ); function isSfapiProxyEnabled() { var _a, _b, _c; if (typeof window === "undefined") return false; try { const navigationEntry = (_b = (_a = window.performance) == null ? void 0 : _a.getEntriesByType) == null ? void 0 : _b.call( _a, "navigation" )[0]; return !!((_c = navigationEntry == null ? void 0 : navigationEntry.serverTiming) == null ? void 0 : _c.some( (entry) => entry.name === "_sfapi_proxy" )); } catch (e) { return false; } } function ShopifyProvider({ children, ...shopifyConfig }) { if (!shopifyConfig.countryIsoCode || !shopifyConfig.languageIsoCode || !shopifyConfig.storeDomain || !shopifyConfig.storefrontToken || !shopifyConfig.storefrontApiVersion) { throw new Error( `Please provide the necessary props to '<ShopifyProvider/>'` ); } if (shopifyConfig.storefrontApiVersion !== storefrontApiConstants.SFAPI_VERSION) { console.warn( `<ShopifyProvider/>: This version of Hydrogen React is built for Shopify's Storefront API version ${storefrontApiConstants.SFAPI_VERSION}, but it looks like you're using version ${shopifyConfig.storefrontApiVersion}. There may be issues or bugs if you use a mismatched version of Hydrogen React and the Storefront API.` ); } const finalConfig = React.useMemo(() => { const sameDomainForStorefrontApi = shopifyConfig.sameDomainForStorefrontApi ?? isSfapiProxyEnabled(); function getShopifyDomain(overrideProps) { const domain = (overrideProps == null ? void 0 : overrideProps.storeDomain) ?? shopifyConfig.storeDomain; return domain.includes("://") ? domain : `https://${domain}`; } return { ...shopifyConfig, sameDomainForStorefrontApi, getPublicTokenHeaders(overrideProps) { return storefrontClient.getPublicTokenHeadersRaw( overrideProps.contentType, shopifyConfig.storefrontApiVersion, overrideProps.storefrontToken ?? shopifyConfig.storefrontToken ); }, getShopifyDomain, getStorefrontApiUrl(overrideProps) { const finalDomainUrl = sameDomainForStorefrontApi && typeof window !== "undefined" ? window.location.origin : getShopifyDomain({ storeDomain: (overrideProps == null ? void 0 : overrideProps.storeDomain) ?? shopifyConfig.storeDomain }); return `${finalDomainUrl}${finalDomainUrl.endsWith("/") ? "" : "/"}api/${(overrideProps == null ? void 0 : overrideProps.storefrontApiVersion) ?? shopifyConfig.storefrontApiVersion}/graphql.json`; } }; }, [shopifyConfig]); return /* @__PURE__ */ jsxRuntime.jsx(ShopifyContext.Provider, { value: finalConfig, children }); } function useShop() { const shopContext = React.useContext(ShopifyContext); if (!shopContext) { throw new Error(`'useShop()' must be a descendent of <ShopifyProvider/>`); } return shopContext; } exports.ShopifyProvider = ShopifyProvider; exports.defaultShopifyContext = defaultShopifyContext; exports.useShop = useShop; //# sourceMappingURL=ShopifyProvider.js.map