@shopify/hydrogen-react
Version:
React components, hooks, and utilities for creating custom Shopify storefronts
108 lines (107 loc) • 4.77 kB
JavaScript
import { SFAPI_VERSION } from "./storefront-api-constants.mjs";
const MOCK_SHOP_DOMAIN = "mock.shop";
const isMockShop = (domain) => domain.includes(MOCK_SHOP_DOMAIN);
function createStorefrontClient({
storeDomain,
privateStorefrontToken,
publicStorefrontToken,
storefrontApiVersion = SFAPI_VERSION,
contentType
}) {
if (!storeDomain) {
{
storeDomain = MOCK_SHOP_DOMAIN;
warnOnce(
`storeDomain missing, defaulting to ${MOCK_SHOP_DOMAIN}`,
"info"
);
}
}
if (storefrontApiVersion !== SFAPI_VERSION) {
warnOnce(
`The Storefront API version that you're using is different than the version this build of Hydrogen React is targeting.
You may run into unexpected errors if these versions don't match. Received version: "${storefrontApiVersion}"; expected version "${SFAPI_VERSION}"`
);
}
if (!privateStorefrontToken && !globalThis.document && !isMockShop(storeDomain)) {
warnOnce(
`Using a private storefront token is recommended for server environments.
Refer to the authentication https://shopify.dev/api/storefront#authentication documentation for more details.`
);
}
if (privateStorefrontToken && globalThis.document) {
warnOnce(
"You are attempting to use a private token in an environment where it can be easily accessed by anyone.\nThis is a security risk; please use the public token and the `publicStorefrontToken` prop"
);
}
const getShopifyDomain = (overrideProps) => {
const domain = (overrideProps == null ? void 0 : overrideProps.storeDomain) ?? storeDomain;
return domain.includes("://") ? domain : `https://${domain}`;
};
return {
getShopifyDomain,
getStorefrontApiUrl(overrideProps) {
const domain = getShopifyDomain(overrideProps);
const apiUrl = domain + (domain.endsWith("/") ? "api" : "/api");
return `${apiUrl}/${(overrideProps == null ? void 0 : overrideProps.storefrontApiVersion) ?? storefrontApiVersion}/graphql.json`;
},
getPrivateTokenHeaders(overrideProps) {
if (!privateStorefrontToken && !(overrideProps == null ? void 0 : overrideProps.privateStorefrontToken) && !isMockShop(storeDomain)) {
throw new Error(
H2_PREFIX_ERROR + "You did not pass in a `privateStorefrontToken` while using `createStorefrontClient()` or `getPrivateTokenHeaders()`"
);
}
if (!(overrideProps == null ? void 0 : overrideProps.buyerIp)) {
warnOnce(
"It is recommended to pass in the `buyerIp` property which improves analytics and data in the admin."
);
}
const finalContentType = (overrideProps == null ? void 0 : overrideProps.contentType) ?? contentType;
return {
// default to json
"content-type": finalContentType === "graphql" ? "application/graphql" : "application/json",
"X-SDK-Variant": "hydrogen-react",
"X-SDK-Variant-Source": "react",
"X-SDK-Version": storefrontApiVersion,
"Shopify-Storefront-Private-Token": (overrideProps == null ? void 0 : overrideProps.privateStorefrontToken) ?? privateStorefrontToken ?? "",
...(overrideProps == null ? void 0 : overrideProps.buyerIp) ? { "Shopify-Storefront-Buyer-IP": overrideProps.buyerIp } : {}
};
},
getPublicTokenHeaders(overrideProps) {
if (!publicStorefrontToken && !(overrideProps == null ? void 0 : overrideProps.publicStorefrontToken) && !isMockShop(storeDomain)) {
throw new Error(
H2_PREFIX_ERROR + "You did not pass in a `publicStorefrontToken` while using `createStorefrontClient()` or `getPublicTokenHeaders()`"
);
}
const finalContentType = (overrideProps == null ? void 0 : overrideProps.contentType) ?? contentType ?? "json";
return getPublicTokenHeadersRaw(
finalContentType,
storefrontApiVersion,
(overrideProps == null ? void 0 : overrideProps.publicStorefrontToken) ?? publicStorefrontToken ?? ""
);
}
};
}
function getPublicTokenHeadersRaw(contentType, storefrontApiVersion, accessToken) {
return {
// default to json
"content-type": contentType === "graphql" ? "application/graphql" : "application/json",
"X-SDK-Variant": "hydrogen-react",
"X-SDK-Variant-Source": "react",
"X-SDK-Version": storefrontApiVersion,
"X-Shopify-Storefront-Access-Token": accessToken
};
}
const warnings = /* @__PURE__ */ new Set();
const H2_PREFIX_ERROR = "[h2:error:createStorefrontClient] ";
const warnOnce = (string, type = "warn") => {
if (!warnings.has(string)) {
console[type](`[h2:${type}:createStorefrontClient] ` + string);
warnings.add(string);
}
};
export {
createStorefrontClient,
getPublicTokenHeadersRaw
};
//# sourceMappingURL=storefront-client.mjs.map