@shopify/hydrogen-react
Version:
React components, hooks, and utilities for creating custom Shopify storefronts
73 lines (72 loc) • 3.98 kB
JavaScript
;
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const storefrontApiConstants = require("./storefront-api-constants.js");
function createStorefrontClient({
storeDomain,
privateStorefrontToken,
publicStorefrontToken,
storefrontApiVersion,
contentType
}) {
if (storefrontApiVersion !== storefrontApiConstants.SFAPI_VERSION) {
console.warn(
`StorefrontClient: The Storefront API version that you're using is different than the version this build of Hydrogen-UI is targeting. You may run into unexpected errors if these versions don't match. Received verion: "${storefrontApiVersion}"; expected version "${storefrontApiConstants.SFAPI_VERSION}"`
);
}
if (!privateStorefrontToken && !globalThis.document) {
console.warn(
`StorefrontClient: 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) {
console.warn(
`StorefrontClient: You are attempting to use a private token in an environment where it can be easily accessed by anyone. This is a security risk; please use the public token and the 'publicStorefrontToken' prop`
);
}
return {
getStorefrontApiUrl(overrideProps) {
var _a, _b;
return `https://${(_a = overrideProps == null ? void 0 : overrideProps.storeDomain) != null ? _a : storeDomain}.myshopify.com/api/${(_b = overrideProps == null ? void 0 : overrideProps.storefrontApiVersion) != null ? _b : storefrontApiVersion}/graphql.json`;
},
getPrivateTokenHeaders(overrideProps) {
var _a, _b, _c;
if (!privateStorefrontToken && !(overrideProps == null ? void 0 : overrideProps.privateStorefrontToken)) {
throw new Error(
`StorefrontClient: You did not pass in a 'privateStorefrontToken' while using 'getPrivateTokenHeaders()'`
);
}
if (!(overrideProps == null ? void 0 : overrideProps.buyerIp)) {
console.warn(
`StorefrontClient: it is recommended to pass in the 'buyerIp' property which improves analytics and data in the admin.`
);
}
const finalContentType = (_a = overrideProps == null ? void 0 : overrideProps.contentType) != null ? _a : contentType;
return {
"content-type": finalContentType === "graphql" ? "application/graphql" : "application/json",
"X-SDK-Variant": "hydrogen-ui",
"X-SDK-Variant-Source": "react",
"X-SDK-Version": storefrontApiVersion,
"Shopify-Storefront-Private-Token": (_c = (_b = overrideProps == null ? void 0 : overrideProps.privateStorefrontToken) != null ? _b : privateStorefrontToken) != null ? _c : "",
...(overrideProps == null ? void 0 : overrideProps.buyerIp) ? { "Shopify-Storefront-Buyer-IP": overrideProps.buyerIp } : {}
};
},
getPublicTokenHeaders(overrideProps) {
var _a, _b, _c;
if (!publicStorefrontToken && !(overrideProps == null ? void 0 : overrideProps.publicStorefrontToken)) {
throw new Error(
`StorefrontClient: You did not pass in a 'publicStorefrontToken' while using 'getPublicTokenHeaders()'`
);
}
const finalContentType = (_a = overrideProps == null ? void 0 : overrideProps.contentType) != null ? _a : contentType;
return {
"content-type": finalContentType === "graphql" ? "application/graphql" : "application/json",
"X-SDK-Variant": "hydrogen-ui",
"X-SDK-Variant-Source": "react",
"X-SDK-Version": storefrontApiVersion,
"X-Shopify-Storefront-Access-Token": (_c = (_b = overrideProps == null ? void 0 : overrideProps.publicStorefrontToken) != null ? _b : publicStorefrontToken) != null ? _c : ""
};
}
};
}
exports.createStorefrontClient = createStorefrontClient;
//# sourceMappingURL=storefront-client.js.map