@shopify/hydrogen-react 2022.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +218 -0
- package/dist/dev/ExternalVideo.cjs +39 -0
- package/dist/dev/ExternalVideo.cjs.map +1 -0
- package/dist/dev/ExternalVideo.js +39 -0
- package/dist/dev/ExternalVideo.js.map +1 -0
- package/dist/dev/Image.cjs +104 -0
- package/dist/dev/Image.cjs.map +1 -0
- package/dist/dev/Image.js +104 -0
- package/dist/dev/Image.js.map +1 -0
- package/dist/dev/MediaFile.cjs +57 -0
- package/dist/dev/MediaFile.cjs.map +1 -0
- package/dist/dev/MediaFile.js +57 -0
- package/dist/dev/MediaFile.js.map +1 -0
- package/dist/dev/Metafield.cjs +295 -0
- package/dist/dev/Metafield.cjs.map +1 -0
- package/dist/dev/Metafield.js +295 -0
- package/dist/dev/Metafield.js.map +1 -0
- package/dist/dev/ModelViewer.cjs +145 -0
- package/dist/dev/ModelViewer.cjs.map +1 -0
- package/dist/dev/ModelViewer.js +145 -0
- package/dist/dev/ModelViewer.js.map +1 -0
- package/dist/dev/Money.cjs +40 -0
- package/dist/dev/Money.cjs.map +1 -0
- package/dist/dev/Money.js +40 -0
- package/dist/dev/Money.js.map +1 -0
- package/dist/dev/ProductPrice.cjs +61 -0
- package/dist/dev/ProductPrice.cjs.map +1 -0
- package/dist/dev/ProductPrice.js +61 -0
- package/dist/dev/ProductPrice.js.map +1 -0
- package/dist/dev/ProductProvider.cjs +161 -0
- package/dist/dev/ProductProvider.cjs.map +1 -0
- package/dist/dev/ProductProvider.js +161 -0
- package/dist/dev/ProductProvider.js.map +1 -0
- package/dist/dev/ShopPayButton.cjs +64 -0
- package/dist/dev/ShopPayButton.cjs.map +1 -0
- package/dist/dev/ShopPayButton.js +64 -0
- package/dist/dev/ShopPayButton.js.map +1 -0
- package/dist/dev/ShopifyProvider.cjs +46 -0
- package/dist/dev/ShopifyProvider.cjs.map +1 -0
- package/dist/dev/ShopifyProvider.js +46 -0
- package/dist/dev/ShopifyProvider.js.map +1 -0
- package/dist/dev/Video.cjs +44 -0
- package/dist/dev/Video.cjs.map +1 -0
- package/dist/dev/Video.js +44 -0
- package/dist/dev/Video.js.map +1 -0
- package/dist/dev/flatten-connection.cjs +23 -0
- package/dist/dev/flatten-connection.cjs.map +1 -0
- package/dist/dev/flatten-connection.js +23 -0
- package/dist/dev/flatten-connection.js.map +1 -0
- package/dist/dev/image-size.cjs +80 -0
- package/dist/dev/image-size.cjs.map +1 -0
- package/dist/dev/image-size.js +80 -0
- package/dist/dev/image-size.js.map +1 -0
- package/dist/dev/index.cjs +35 -0
- package/dist/dev/index.cjs.map +1 -0
- package/dist/dev/index.js +35 -0
- package/dist/dev/index.js.map +1 -0
- package/dist/dev/load-script.cjs +52 -0
- package/dist/dev/load-script.cjs.map +1 -0
- package/dist/dev/load-script.js +52 -0
- package/dist/dev/load-script.js.map +1 -0
- package/dist/dev/storefront-api-constants.cjs +5 -0
- package/dist/dev/storefront-api-constants.cjs.map +1 -0
- package/dist/dev/storefront-api-constants.js +5 -0
- package/dist/dev/storefront-api-constants.js.map +1 -0
- package/dist/dev/storefront-client.cjs +72 -0
- package/dist/dev/storefront-client.cjs.map +1 -0
- package/dist/dev/storefront-client.js +72 -0
- package/dist/dev/storefront-client.js.map +1 -0
- package/dist/dev/useMoney.cjs +72 -0
- package/dist/dev/useMoney.cjs.map +1 -0
- package/dist/dev/useMoney.js +72 -0
- package/dist/dev/useMoney.js.map +1 -0
- package/dist/prod/ExternalVideo.cjs +39 -0
- package/dist/prod/ExternalVideo.cjs.map +1 -0
- package/dist/prod/ExternalVideo.js +39 -0
- package/dist/prod/ExternalVideo.js.map +1 -0
- package/dist/prod/Image.cjs +99 -0
- package/dist/prod/Image.cjs.map +1 -0
- package/dist/prod/Image.js +99 -0
- package/dist/prod/Image.js.map +1 -0
- package/dist/prod/MediaFile.cjs +59 -0
- package/dist/prod/MediaFile.cjs.map +1 -0
- package/dist/prod/MediaFile.js +59 -0
- package/dist/prod/MediaFile.js.map +1 -0
- package/dist/prod/Metafield.cjs +288 -0
- package/dist/prod/Metafield.cjs.map +1 -0
- package/dist/prod/Metafield.js +288 -0
- package/dist/prod/Metafield.js.map +1 -0
- package/dist/prod/ModelViewer.cjs +143 -0
- package/dist/prod/ModelViewer.cjs.map +1 -0
- package/dist/prod/ModelViewer.js +143 -0
- package/dist/prod/ModelViewer.js.map +1 -0
- package/dist/prod/Money.cjs +40 -0
- package/dist/prod/Money.cjs.map +1 -0
- package/dist/prod/Money.js +40 -0
- package/dist/prod/Money.js.map +1 -0
- package/dist/prod/ProductPrice.cjs +61 -0
- package/dist/prod/ProductPrice.cjs.map +1 -0
- package/dist/prod/ProductPrice.js +61 -0
- package/dist/prod/ProductPrice.js.map +1 -0
- package/dist/prod/ProductProvider.cjs +161 -0
- package/dist/prod/ProductProvider.cjs.map +1 -0
- package/dist/prod/ProductProvider.js +161 -0
- package/dist/prod/ProductProvider.js.map +1 -0
- package/dist/prod/ShopPayButton.cjs +64 -0
- package/dist/prod/ShopPayButton.cjs.map +1 -0
- package/dist/prod/ShopPayButton.js +64 -0
- package/dist/prod/ShopPayButton.js.map +1 -0
- package/dist/prod/ShopifyProvider.cjs +46 -0
- package/dist/prod/ShopifyProvider.cjs.map +1 -0
- package/dist/prod/ShopifyProvider.js +46 -0
- package/dist/prod/ShopifyProvider.js.map +1 -0
- package/dist/prod/Video.cjs +44 -0
- package/dist/prod/Video.cjs.map +1 -0
- package/dist/prod/Video.js +44 -0
- package/dist/prod/Video.js.map +1 -0
- package/dist/prod/flatten-connection.cjs +18 -0
- package/dist/prod/flatten-connection.cjs.map +1 -0
- package/dist/prod/flatten-connection.js +18 -0
- package/dist/prod/flatten-connection.js.map +1 -0
- package/dist/prod/image-size.cjs +80 -0
- package/dist/prod/image-size.cjs.map +1 -0
- package/dist/prod/image-size.js +80 -0
- package/dist/prod/image-size.js.map +1 -0
- package/dist/prod/index.cjs +35 -0
- package/dist/prod/index.cjs.map +1 -0
- package/dist/prod/index.js +35 -0
- package/dist/prod/index.js.map +1 -0
- package/dist/prod/load-script.cjs +52 -0
- package/dist/prod/load-script.cjs.map +1 -0
- package/dist/prod/load-script.js +52 -0
- package/dist/prod/load-script.js.map +1 -0
- package/dist/prod/storefront-api-constants.cjs +5 -0
- package/dist/prod/storefront-api-constants.cjs.map +1 -0
- package/dist/prod/storefront-api-constants.js +5 -0
- package/dist/prod/storefront-api-constants.js.map +1 -0
- package/dist/prod/storefront-client.cjs +57 -0
- package/dist/prod/storefront-client.cjs.map +1 -0
- package/dist/prod/storefront-client.js +57 -0
- package/dist/prod/storefront-client.js.map +1 -0
- package/dist/prod/useMoney.cjs +72 -0
- package/dist/prod/useMoney.cjs.map +1 -0
- package/dist/prod/useMoney.js +72 -0
- package/dist/prod/useMoney.js.map +1 -0
- package/dist/types/ExternalVideo.d.ts +67 -0
- package/dist/types/Image.d.ts +54 -0
- package/dist/types/MediaFile.d.ts +31 -0
- package/dist/types/Metafield.d.ts +58 -0
- package/dist/types/ModelViewer.d.ts +57 -0
- package/dist/types/Money.d.ts +29 -0
- package/dist/types/ProductPrice.d.ts +22 -0
- package/dist/types/ProductProvider.d.ts +74 -0
- package/dist/types/ShopPayButton.d.ts +42 -0
- package/dist/types/ShopifyProvider.d.ts +42 -0
- package/dist/types/Video.d.ts +20 -0
- package/dist/types/flatten-connection.d.ts +17 -0
- package/dist/types/image-size.d.ts +36 -0
- package/dist/types/index.d.cts +15 -0
- package/dist/types/index.d.ts +15 -0
- package/dist/types/load-script.d.ts +11 -0
- package/dist/types/storefront-api-constants.d.ts +1 -0
- package/dist/types/storefront-api-response.types.d.ts +68 -0
- package/dist/types/storefront-api-types.d.ts +6537 -0
- package/dist/types/storefront-client.d.ts +63 -0
- package/dist/types/useMoney.d.ts +55 -0
- package/dist/umd/hydrogen-react.dev.js +1472 -0
- package/dist/umd/hydrogen-react.dev.js.map +1 -0
- package/dist/umd/hydrogen-react.prod.js +3 -0
- package/dist/umd/hydrogen-react.prod.js.map +1 -0
- package/package.json +106 -0
- package/storefront.schema.json +1 -0
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
3
|
+
const PRODUCTION_CDN_HOSTNAMES = [
|
|
4
|
+
"cdn.shopify.com",
|
|
5
|
+
"cdn.shopifycdn.net",
|
|
6
|
+
"shopify-assets.shopifycdn.com",
|
|
7
|
+
"shopify-assets.shopifycdn.net"
|
|
8
|
+
];
|
|
9
|
+
const LOCAL_CDN_HOSTNAMES = ["spin.dev"];
|
|
10
|
+
const ALL_CDN_HOSTNAMES = [...PRODUCTION_CDN_HOSTNAMES, ...LOCAL_CDN_HOSTNAMES];
|
|
11
|
+
const IMG_SRC_SET_SIZES = [352, 832, 1200, 1920, 2560];
|
|
12
|
+
function addImageSizeParametersToUrl({
|
|
13
|
+
src,
|
|
14
|
+
width,
|
|
15
|
+
height,
|
|
16
|
+
crop,
|
|
17
|
+
scale
|
|
18
|
+
}) {
|
|
19
|
+
const newUrl = new URL(src);
|
|
20
|
+
const multipliedScale = scale != null ? scale : 1;
|
|
21
|
+
if (width) {
|
|
22
|
+
let finalWidth;
|
|
23
|
+
if (typeof width === "string") {
|
|
24
|
+
finalWidth = (IMG_SRC_SET_SIZES[0] * multipliedScale).toString();
|
|
25
|
+
} else {
|
|
26
|
+
finalWidth = (Number(width) * multipliedScale).toString();
|
|
27
|
+
}
|
|
28
|
+
newUrl.searchParams.append("width", finalWidth);
|
|
29
|
+
}
|
|
30
|
+
if (height && typeof height === "number") {
|
|
31
|
+
newUrl.searchParams.append("height", (height * multipliedScale).toString());
|
|
32
|
+
}
|
|
33
|
+
crop && newUrl.searchParams.append("crop", crop);
|
|
34
|
+
return newUrl.toString();
|
|
35
|
+
}
|
|
36
|
+
function shopifyImageLoader(params) {
|
|
37
|
+
const newSrc = new URL(params.src);
|
|
38
|
+
const isShopifyServedImage = ALL_CDN_HOSTNAMES.some(
|
|
39
|
+
(allowedHostname) => newSrc.hostname.endsWith(allowedHostname)
|
|
40
|
+
);
|
|
41
|
+
if (!isShopifyServedImage || !params.width && !params.height && !params.crop && !params.scale) {
|
|
42
|
+
return params.src;
|
|
43
|
+
}
|
|
44
|
+
return addImageSizeParametersToUrl(params);
|
|
45
|
+
}
|
|
46
|
+
function getShopifyImageDimensions({
|
|
47
|
+
data: sfapiImage,
|
|
48
|
+
loaderOptions,
|
|
49
|
+
elementProps
|
|
50
|
+
}) {
|
|
51
|
+
var _a, _b, _c, _d, _e, _f;
|
|
52
|
+
let aspectRatio = null;
|
|
53
|
+
if ((sfapiImage == null ? void 0 : sfapiImage.width) && (sfapiImage == null ? void 0 : sfapiImage.height)) {
|
|
54
|
+
aspectRatio = (sfapiImage == null ? void 0 : sfapiImage.width) / (sfapiImage == null ? void 0 : sfapiImage.height);
|
|
55
|
+
}
|
|
56
|
+
if ((loaderOptions == null ? void 0 : loaderOptions.width) || (loaderOptions == null ? void 0 : loaderOptions.height)) {
|
|
57
|
+
return {
|
|
58
|
+
width: (_a = loaderOptions == null ? void 0 : loaderOptions.width) != null ? _a : aspectRatio && typeof loaderOptions.height === "number" ? Math.round(aspectRatio * loaderOptions.height) : null,
|
|
59
|
+
height: (_b = loaderOptions == null ? void 0 : loaderOptions.height) != null ? _b : aspectRatio && typeof loaderOptions.width === "number" ? Math.round(aspectRatio * loaderOptions.width) : null
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
if ((elementProps == null ? void 0 : elementProps.width) || (elementProps == null ? void 0 : elementProps.height)) {
|
|
63
|
+
return {
|
|
64
|
+
width: (_c = elementProps == null ? void 0 : elementProps.width) != null ? _c : aspectRatio && typeof elementProps.height === "number" ? Math.round(aspectRatio * elementProps.height) : null,
|
|
65
|
+
height: (_d = elementProps == null ? void 0 : elementProps.height) != null ? _d : aspectRatio && typeof elementProps.width === "number" ? Math.round(aspectRatio * elementProps.width) : null
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
if ((sfapiImage == null ? void 0 : sfapiImage.width) || (sfapiImage == null ? void 0 : sfapiImage.height)) {
|
|
69
|
+
return {
|
|
70
|
+
width: (_e = sfapiImage == null ? void 0 : sfapiImage.width) != null ? _e : null,
|
|
71
|
+
height: (_f = sfapiImage == null ? void 0 : sfapiImage.height) != null ? _f : null
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
return { width: null, height: null };
|
|
75
|
+
}
|
|
76
|
+
exports.IMG_SRC_SET_SIZES = IMG_SRC_SET_SIZES;
|
|
77
|
+
exports.addImageSizeParametersToUrl = addImageSizeParametersToUrl;
|
|
78
|
+
exports.getShopifyImageDimensions = getShopifyImageDimensions;
|
|
79
|
+
exports.shopifyImageLoader = shopifyImageLoader;
|
|
80
|
+
//# sourceMappingURL=image-size.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"image-size.cjs","sources":["../../src/image-size.ts"],"sourcesContent":["import type {Image as ImageType} from './storefront-api-types.js';\nimport type {PartialDeep} from 'type-fest';\nimport type {ShopifyLoaderOptions, ShopifyLoaderParams} from './Image.js';\n\n// TODO: Are there other CDNs missing from here?\nconst PRODUCTION_CDN_HOSTNAMES = [\n 'cdn.shopify.com',\n 'cdn.shopifycdn.net',\n 'shopify-assets.shopifycdn.com',\n 'shopify-assets.shopifycdn.net',\n];\nconst LOCAL_CDN_HOSTNAMES = ['spin.dev'];\nconst ALL_CDN_HOSTNAMES = [...PRODUCTION_CDN_HOSTNAMES, ...LOCAL_CDN_HOSTNAMES];\n\n// based on the default width sizes used by the Shopify liquid HTML tag img_tag plus a 2560 width to account for 2k resolutions\n// reference: https://shopify.dev/api/liquid/filters/html-filters#image_tag\nexport const IMG_SRC_SET_SIZES = [352, 832, 1200, 1920, 2560];\n\n/**\n * Adds image size parameters to an image URL hosted by Shopify's CDN\n */\nexport function addImageSizeParametersToUrl({\n src,\n width,\n height,\n crop,\n scale,\n}: ShopifyLoaderParams) {\n const newUrl = new URL(src);\n\n const multipliedScale = scale ?? 1;\n\n if (width) {\n let finalWidth: string;\n\n if (typeof width === 'string') {\n finalWidth = (IMG_SRC_SET_SIZES[0] * multipliedScale).toString();\n } else {\n finalWidth = (Number(width) * multipliedScale).toString();\n }\n\n newUrl.searchParams.append('width', finalWidth);\n }\n\n if (height && typeof height === 'number') {\n newUrl.searchParams.append('height', (height * multipliedScale).toString());\n }\n\n crop && newUrl.searchParams.append('crop', crop);\n\n // for now we intentionally leave off the scale param, and instead multiple width & height by scale instead\n // scale && newUrl.searchParams.append('scale', scale.toString());\n\n return newUrl.toString();\n}\n\nexport function shopifyImageLoader(params: ShopifyLoaderParams) {\n const newSrc = new URL(params.src);\n const isShopifyServedImage = ALL_CDN_HOSTNAMES.some((allowedHostname) =>\n newSrc.hostname.endsWith(allowedHostname)\n );\n\n if (\n !isShopifyServedImage ||\n (!params.width && !params.height && !params.crop && !params.scale)\n ) {\n return params.src;\n }\n\n return addImageSizeParametersToUrl(params);\n}\n\ntype HtmlImageProps = React.ImgHTMLAttributes<HTMLImageElement>;\n\nexport type GetShopifyImageDimensionsProps = {\n data: Pick<\n PartialDeep<ImageType, {recurseIntoArrays: true}>,\n 'altText' | 'url' | 'id' | 'width' | 'height'\n >;\n loaderOptions?: ShopifyLoaderOptions;\n elementProps?: {\n width?: HtmlImageProps['width'];\n height?: HtmlImageProps['height'];\n };\n};\n\ntype GetShopifyImageDimensionsPropsReturn = {\n width: number | string | null;\n height: number | string | null;\n};\n\n/**\n * Width and height are determined using the followiing priority list:\n * 1. `loaderOptions`'s width/height\n * 2. `elementProps`'s width/height\n * 3. `data`'s width/height\n *\n * If only one of `width` or `height` are defined, then the other will attempt to be calculated based on the Image's aspect ratio,\n * provided that both `data.width` and `data.height` are available. If not, then the aspect ratio cannot be determined and the missing\n * value will reamin as `null`\n */\nexport function getShopifyImageDimensions({\n data: sfapiImage,\n loaderOptions,\n elementProps,\n}: GetShopifyImageDimensionsProps): GetShopifyImageDimensionsPropsReturn {\n let aspectRatio: number | null = null;\n\n if (sfapiImage?.width && sfapiImage?.height) {\n aspectRatio = sfapiImage?.width / sfapiImage?.height;\n }\n\n // * 1. `loaderOptions`'s width/height\n if (loaderOptions?.width || loaderOptions?.height) {\n return {\n width:\n loaderOptions?.width ??\n (aspectRatio && typeof loaderOptions.height === 'number'\n ? Math.round(aspectRatio * loaderOptions.height)\n : null),\n height:\n loaderOptions?.height ??\n (aspectRatio && typeof loaderOptions.width === 'number'\n ? Math.round(aspectRatio * loaderOptions.width)\n : null),\n };\n }\n\n // * 2. `elementProps`'s width/height\n if (elementProps?.width || elementProps?.height) {\n return {\n width:\n elementProps?.width ??\n (aspectRatio && typeof elementProps.height === 'number'\n ? Math.round(aspectRatio * elementProps.height)\n : null),\n height:\n elementProps?.height ??\n (aspectRatio && typeof elementProps.width === 'number'\n ? Math.round(aspectRatio * elementProps.width)\n : null),\n };\n }\n\n // * 3. `data`'s width/height\n if (sfapiImage?.width || sfapiImage?.height) {\n return {\n // can't calculate the aspect ratio here\n width: sfapiImage?.width ?? null,\n height: sfapiImage?.height ?? null,\n };\n }\n\n return {width: null, height: null};\n}\n"],"names":[],"mappings":";;AAKA,MAAM,2BAA2B;AAAA,EAC/B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AACA,MAAM,sBAAsB,CAAC,UAAU;AACvC,MAAM,oBAAoB,CAAC,GAAG,0BAA0B,GAAG,mBAAmB;AAIvE,MAAM,oBAAoB,CAAC,KAAK,KAAK,MAAM,MAAM,IAAI;AAKrD,SAAS,4BAA4B;AAAA,EAC1C;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GAAwB;AAChB,QAAA,SAAS,IAAI,IAAI,GAAG;AAE1B,QAAM,kBAAkB,wBAAS;AAEjC,MAAI,OAAO;AACL,QAAA;AAEA,QAAA,OAAO,UAAU,UAAU;AACf,oBAAA,kBAAkB,KAAK,iBAAiB,SAAS;AAAA,IAAA,OAC1D;AACL,oBAAc,OAAO,KAAK,IAAI,iBAAiB,SAAS;AAAA,IAC1D;AAEO,WAAA,aAAa,OAAO,SAAS,UAAU;AAAA,EAChD;AAEI,MAAA,UAAU,OAAO,WAAW,UAAU;AACxC,WAAO,aAAa,OAAO,WAAW,SAAS,iBAAiB,UAAU;AAAA,EAC5E;AAEA,UAAQ,OAAO,aAAa,OAAO,QAAQ,IAAI;AAK/C,SAAO,OAAO;AAChB;AAEO,SAAS,mBAAmB,QAA6B;AAC9D,QAAM,SAAS,IAAI,IAAI,OAAO,GAAG;AACjC,QAAM,uBAAuB,kBAAkB;AAAA,IAAK,CAAC,oBACnD,OAAO,SAAS,SAAS,eAAe;AAAA,EAAA;AAG1C,MACE,CAAC,wBACA,CAAC,OAAO,SAAS,CAAC,OAAO,UAAU,CAAC,OAAO,QAAQ,CAAC,OAAO,OAC5D;AACA,WAAO,OAAO;AAAA,EAChB;AAEA,SAAO,4BAA4B,MAAM;AAC3C;AA+BO,SAAS,0BAA0B;AAAA,EACxC,MAAM;AAAA,EACN;AAAA,EACA;AACF,GAAyE;;AACvE,MAAI,cAA6B;AAE7B,OAAA,yCAAY,WAAS,yCAAY,SAAQ;AAC7B,mBAAA,yCAAY,UAAQ,yCAAY;AAAA,EAChD;AAGI,OAAA,+CAAe,WAAS,+CAAe,SAAQ;AAC1C,WAAA;AAAA,MACL,QACE,oDAAe,UAAf,YACC,eAAe,OAAO,cAAc,WAAW,WAC5C,KAAK,MAAM,cAAc,cAAc,MAAM,IAC7C;AAAA,MACN,SACE,oDAAe,WAAf,YACC,eAAe,OAAO,cAAc,UAAU,WAC3C,KAAK,MAAM,cAAc,cAAc,KAAK,IAC5C;AAAA,IAAA;AAAA,EAEV;AAGI,OAAA,6CAAc,WAAS,6CAAc,SAAQ;AACxC,WAAA;AAAA,MACL,QACE,kDAAc,UAAd,YACC,eAAe,OAAO,aAAa,WAAW,WAC3C,KAAK,MAAM,cAAc,aAAa,MAAM,IAC5C;AAAA,MACN,SACE,kDAAc,WAAd,YACC,eAAe,OAAO,aAAa,UAAU,WAC1C,KAAK,MAAM,cAAc,aAAa,KAAK,IAC3C;AAAA,IAAA;AAAA,EAEV;AAGI,OAAA,yCAAY,WAAS,yCAAY,SAAQ;AACpC,WAAA;AAAA,MAEL,QAAO,8CAAY,UAAZ,YAAqB;AAAA,MAC5B,SAAQ,8CAAY,WAAZ,YAAsB;AAAA,IAAA;AAAA,EAElC;AAEA,SAAO,EAAC,OAAO,MAAM,QAAQ,KAAI;AACnC;;;;;"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
const PRODUCTION_CDN_HOSTNAMES = [
|
|
2
|
+
"cdn.shopify.com",
|
|
3
|
+
"cdn.shopifycdn.net",
|
|
4
|
+
"shopify-assets.shopifycdn.com",
|
|
5
|
+
"shopify-assets.shopifycdn.net"
|
|
6
|
+
];
|
|
7
|
+
const LOCAL_CDN_HOSTNAMES = ["spin.dev"];
|
|
8
|
+
const ALL_CDN_HOSTNAMES = [...PRODUCTION_CDN_HOSTNAMES, ...LOCAL_CDN_HOSTNAMES];
|
|
9
|
+
const IMG_SRC_SET_SIZES = [352, 832, 1200, 1920, 2560];
|
|
10
|
+
function addImageSizeParametersToUrl({
|
|
11
|
+
src,
|
|
12
|
+
width,
|
|
13
|
+
height,
|
|
14
|
+
crop,
|
|
15
|
+
scale
|
|
16
|
+
}) {
|
|
17
|
+
const newUrl = new URL(src);
|
|
18
|
+
const multipliedScale = scale != null ? scale : 1;
|
|
19
|
+
if (width) {
|
|
20
|
+
let finalWidth;
|
|
21
|
+
if (typeof width === "string") {
|
|
22
|
+
finalWidth = (IMG_SRC_SET_SIZES[0] * multipliedScale).toString();
|
|
23
|
+
} else {
|
|
24
|
+
finalWidth = (Number(width) * multipliedScale).toString();
|
|
25
|
+
}
|
|
26
|
+
newUrl.searchParams.append("width", finalWidth);
|
|
27
|
+
}
|
|
28
|
+
if (height && typeof height === "number") {
|
|
29
|
+
newUrl.searchParams.append("height", (height * multipliedScale).toString());
|
|
30
|
+
}
|
|
31
|
+
crop && newUrl.searchParams.append("crop", crop);
|
|
32
|
+
return newUrl.toString();
|
|
33
|
+
}
|
|
34
|
+
function shopifyImageLoader(params) {
|
|
35
|
+
const newSrc = new URL(params.src);
|
|
36
|
+
const isShopifyServedImage = ALL_CDN_HOSTNAMES.some(
|
|
37
|
+
(allowedHostname) => newSrc.hostname.endsWith(allowedHostname)
|
|
38
|
+
);
|
|
39
|
+
if (!isShopifyServedImage || !params.width && !params.height && !params.crop && !params.scale) {
|
|
40
|
+
return params.src;
|
|
41
|
+
}
|
|
42
|
+
return addImageSizeParametersToUrl(params);
|
|
43
|
+
}
|
|
44
|
+
function getShopifyImageDimensions({
|
|
45
|
+
data: sfapiImage,
|
|
46
|
+
loaderOptions,
|
|
47
|
+
elementProps
|
|
48
|
+
}) {
|
|
49
|
+
var _a, _b, _c, _d, _e, _f;
|
|
50
|
+
let aspectRatio = null;
|
|
51
|
+
if ((sfapiImage == null ? void 0 : sfapiImage.width) && (sfapiImage == null ? void 0 : sfapiImage.height)) {
|
|
52
|
+
aspectRatio = (sfapiImage == null ? void 0 : sfapiImage.width) / (sfapiImage == null ? void 0 : sfapiImage.height);
|
|
53
|
+
}
|
|
54
|
+
if ((loaderOptions == null ? void 0 : loaderOptions.width) || (loaderOptions == null ? void 0 : loaderOptions.height)) {
|
|
55
|
+
return {
|
|
56
|
+
width: (_a = loaderOptions == null ? void 0 : loaderOptions.width) != null ? _a : aspectRatio && typeof loaderOptions.height === "number" ? Math.round(aspectRatio * loaderOptions.height) : null,
|
|
57
|
+
height: (_b = loaderOptions == null ? void 0 : loaderOptions.height) != null ? _b : aspectRatio && typeof loaderOptions.width === "number" ? Math.round(aspectRatio * loaderOptions.width) : null
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
if ((elementProps == null ? void 0 : elementProps.width) || (elementProps == null ? void 0 : elementProps.height)) {
|
|
61
|
+
return {
|
|
62
|
+
width: (_c = elementProps == null ? void 0 : elementProps.width) != null ? _c : aspectRatio && typeof elementProps.height === "number" ? Math.round(aspectRatio * elementProps.height) : null,
|
|
63
|
+
height: (_d = elementProps == null ? void 0 : elementProps.height) != null ? _d : aspectRatio && typeof elementProps.width === "number" ? Math.round(aspectRatio * elementProps.width) : null
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
if ((sfapiImage == null ? void 0 : sfapiImage.width) || (sfapiImage == null ? void 0 : sfapiImage.height)) {
|
|
67
|
+
return {
|
|
68
|
+
width: (_e = sfapiImage == null ? void 0 : sfapiImage.width) != null ? _e : null,
|
|
69
|
+
height: (_f = sfapiImage == null ? void 0 : sfapiImage.height) != null ? _f : null
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
return { width: null, height: null };
|
|
73
|
+
}
|
|
74
|
+
export {
|
|
75
|
+
IMG_SRC_SET_SIZES,
|
|
76
|
+
addImageSizeParametersToUrl,
|
|
77
|
+
getShopifyImageDimensions,
|
|
78
|
+
shopifyImageLoader
|
|
79
|
+
};
|
|
80
|
+
//# sourceMappingURL=image-size.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"image-size.js","sources":["../../src/image-size.ts"],"sourcesContent":["import type {Image as ImageType} from './storefront-api-types.js';\nimport type {PartialDeep} from 'type-fest';\nimport type {ShopifyLoaderOptions, ShopifyLoaderParams} from './Image.js';\n\n// TODO: Are there other CDNs missing from here?\nconst PRODUCTION_CDN_HOSTNAMES = [\n 'cdn.shopify.com',\n 'cdn.shopifycdn.net',\n 'shopify-assets.shopifycdn.com',\n 'shopify-assets.shopifycdn.net',\n];\nconst LOCAL_CDN_HOSTNAMES = ['spin.dev'];\nconst ALL_CDN_HOSTNAMES = [...PRODUCTION_CDN_HOSTNAMES, ...LOCAL_CDN_HOSTNAMES];\n\n// based on the default width sizes used by the Shopify liquid HTML tag img_tag plus a 2560 width to account for 2k resolutions\n// reference: https://shopify.dev/api/liquid/filters/html-filters#image_tag\nexport const IMG_SRC_SET_SIZES = [352, 832, 1200, 1920, 2560];\n\n/**\n * Adds image size parameters to an image URL hosted by Shopify's CDN\n */\nexport function addImageSizeParametersToUrl({\n src,\n width,\n height,\n crop,\n scale,\n}: ShopifyLoaderParams) {\n const newUrl = new URL(src);\n\n const multipliedScale = scale ?? 1;\n\n if (width) {\n let finalWidth: string;\n\n if (typeof width === 'string') {\n finalWidth = (IMG_SRC_SET_SIZES[0] * multipliedScale).toString();\n } else {\n finalWidth = (Number(width) * multipliedScale).toString();\n }\n\n newUrl.searchParams.append('width', finalWidth);\n }\n\n if (height && typeof height === 'number') {\n newUrl.searchParams.append('height', (height * multipliedScale).toString());\n }\n\n crop && newUrl.searchParams.append('crop', crop);\n\n // for now we intentionally leave off the scale param, and instead multiple width & height by scale instead\n // scale && newUrl.searchParams.append('scale', scale.toString());\n\n return newUrl.toString();\n}\n\nexport function shopifyImageLoader(params: ShopifyLoaderParams) {\n const newSrc = new URL(params.src);\n const isShopifyServedImage = ALL_CDN_HOSTNAMES.some((allowedHostname) =>\n newSrc.hostname.endsWith(allowedHostname)\n );\n\n if (\n !isShopifyServedImage ||\n (!params.width && !params.height && !params.crop && !params.scale)\n ) {\n return params.src;\n }\n\n return addImageSizeParametersToUrl(params);\n}\n\ntype HtmlImageProps = React.ImgHTMLAttributes<HTMLImageElement>;\n\nexport type GetShopifyImageDimensionsProps = {\n data: Pick<\n PartialDeep<ImageType, {recurseIntoArrays: true}>,\n 'altText' | 'url' | 'id' | 'width' | 'height'\n >;\n loaderOptions?: ShopifyLoaderOptions;\n elementProps?: {\n width?: HtmlImageProps['width'];\n height?: HtmlImageProps['height'];\n };\n};\n\ntype GetShopifyImageDimensionsPropsReturn = {\n width: number | string | null;\n height: number | string | null;\n};\n\n/**\n * Width and height are determined using the followiing priority list:\n * 1. `loaderOptions`'s width/height\n * 2. `elementProps`'s width/height\n * 3. `data`'s width/height\n *\n * If only one of `width` or `height` are defined, then the other will attempt to be calculated based on the Image's aspect ratio,\n * provided that both `data.width` and `data.height` are available. If not, then the aspect ratio cannot be determined and the missing\n * value will reamin as `null`\n */\nexport function getShopifyImageDimensions({\n data: sfapiImage,\n loaderOptions,\n elementProps,\n}: GetShopifyImageDimensionsProps): GetShopifyImageDimensionsPropsReturn {\n let aspectRatio: number | null = null;\n\n if (sfapiImage?.width && sfapiImage?.height) {\n aspectRatio = sfapiImage?.width / sfapiImage?.height;\n }\n\n // * 1. `loaderOptions`'s width/height\n if (loaderOptions?.width || loaderOptions?.height) {\n return {\n width:\n loaderOptions?.width ??\n (aspectRatio && typeof loaderOptions.height === 'number'\n ? Math.round(aspectRatio * loaderOptions.height)\n : null),\n height:\n loaderOptions?.height ??\n (aspectRatio && typeof loaderOptions.width === 'number'\n ? Math.round(aspectRatio * loaderOptions.width)\n : null),\n };\n }\n\n // * 2. `elementProps`'s width/height\n if (elementProps?.width || elementProps?.height) {\n return {\n width:\n elementProps?.width ??\n (aspectRatio && typeof elementProps.height === 'number'\n ? Math.round(aspectRatio * elementProps.height)\n : null),\n height:\n elementProps?.height ??\n (aspectRatio && typeof elementProps.width === 'number'\n ? Math.round(aspectRatio * elementProps.width)\n : null),\n };\n }\n\n // * 3. `data`'s width/height\n if (sfapiImage?.width || sfapiImage?.height) {\n return {\n // can't calculate the aspect ratio here\n width: sfapiImage?.width ?? null,\n height: sfapiImage?.height ?? null,\n };\n }\n\n return {width: null, height: null};\n}\n"],"names":[],"mappings":"AAKA,MAAM,2BAA2B;AAAA,EAC/B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AACA,MAAM,sBAAsB,CAAC,UAAU;AACvC,MAAM,oBAAoB,CAAC,GAAG,0BAA0B,GAAG,mBAAmB;AAIvE,MAAM,oBAAoB,CAAC,KAAK,KAAK,MAAM,MAAM,IAAI;AAKrD,SAAS,4BAA4B;AAAA,EAC1C;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GAAwB;AAChB,QAAA,SAAS,IAAI,IAAI,GAAG;AAE1B,QAAM,kBAAkB,wBAAS;AAEjC,MAAI,OAAO;AACL,QAAA;AAEA,QAAA,OAAO,UAAU,UAAU;AACf,oBAAA,kBAAkB,KAAK,iBAAiB,SAAS;AAAA,IAAA,OAC1D;AACL,oBAAc,OAAO,KAAK,IAAI,iBAAiB,SAAS;AAAA,IAC1D;AAEO,WAAA,aAAa,OAAO,SAAS,UAAU;AAAA,EAChD;AAEI,MAAA,UAAU,OAAO,WAAW,UAAU;AACxC,WAAO,aAAa,OAAO,WAAW,SAAS,iBAAiB,UAAU;AAAA,EAC5E;AAEA,UAAQ,OAAO,aAAa,OAAO,QAAQ,IAAI;AAK/C,SAAO,OAAO;AAChB;AAEO,SAAS,mBAAmB,QAA6B;AAC9D,QAAM,SAAS,IAAI,IAAI,OAAO,GAAG;AACjC,QAAM,uBAAuB,kBAAkB;AAAA,IAAK,CAAC,oBACnD,OAAO,SAAS,SAAS,eAAe;AAAA,EAAA;AAG1C,MACE,CAAC,wBACA,CAAC,OAAO,SAAS,CAAC,OAAO,UAAU,CAAC,OAAO,QAAQ,CAAC,OAAO,OAC5D;AACA,WAAO,OAAO;AAAA,EAChB;AAEA,SAAO,4BAA4B,MAAM;AAC3C;AA+BO,SAAS,0BAA0B;AAAA,EACxC,MAAM;AAAA,EACN;AAAA,EACA;AACF,GAAyE;AApGzE;AAqGE,MAAI,cAA6B;AAE7B,OAAA,yCAAY,WAAS,yCAAY,SAAQ;AAC7B,mBAAA,yCAAY,UAAQ,yCAAY;AAAA,EAChD;AAGI,OAAA,+CAAe,WAAS,+CAAe,SAAQ;AAC1C,WAAA;AAAA,MACL,QACE,oDAAe,UAAf,YACC,eAAe,OAAO,cAAc,WAAW,WAC5C,KAAK,MAAM,cAAc,cAAc,MAAM,IAC7C;AAAA,MACN,SACE,oDAAe,WAAf,YACC,eAAe,OAAO,cAAc,UAAU,WAC3C,KAAK,MAAM,cAAc,cAAc,KAAK,IAC5C;AAAA,IAAA;AAAA,EAEV;AAGI,OAAA,6CAAc,WAAS,6CAAc,SAAQ;AACxC,WAAA;AAAA,MACL,QACE,kDAAc,UAAd,YACC,eAAe,OAAO,aAAa,WAAW,WAC3C,KAAK,MAAM,cAAc,aAAa,MAAM,IAC5C;AAAA,MACN,SACE,kDAAc,WAAd,YACC,eAAe,OAAO,aAAa,UAAU,WAC1C,KAAK,MAAM,cAAc,aAAa,KAAK,IAC3C;AAAA,IAAA;AAAA,EAEV;AAGI,OAAA,yCAAY,WAAS,yCAAY,SAAQ;AACpC,WAAA;AAAA,MAEL,QAAO,8CAAY,UAAZ,YAAqB;AAAA,MAC5B,SAAQ,8CAAY,WAAZ,YAAsB;AAAA,IAAA;AAAA,EAElC;AAEA,SAAO,EAAC,OAAO,MAAM,QAAQ,KAAI;AACnC;"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
3
|
+
const ExternalVideo = require("./ExternalVideo.cjs");
|
|
4
|
+
const flattenConnection = require("./flatten-connection.cjs");
|
|
5
|
+
const Image = require("./Image.cjs");
|
|
6
|
+
const MediaFile = require("./MediaFile.cjs");
|
|
7
|
+
const Metafield = require("./Metafield.cjs");
|
|
8
|
+
const ModelViewer = require("./ModelViewer.cjs");
|
|
9
|
+
const Money = require("./Money.cjs");
|
|
10
|
+
const ProductPrice = require("./ProductPrice.cjs");
|
|
11
|
+
const ProductProvider = require("./ProductProvider.cjs");
|
|
12
|
+
const ShopifyProvider = require("./ShopifyProvider.cjs");
|
|
13
|
+
const ShopPayButton = require("./ShopPayButton.cjs");
|
|
14
|
+
const storefrontClient = require("./storefront-client.cjs");
|
|
15
|
+
const useMoney = require("./useMoney.cjs");
|
|
16
|
+
const Video = require("./Video.cjs");
|
|
17
|
+
exports.ExternalVideo = ExternalVideo.ExternalVideo;
|
|
18
|
+
exports.flattenConnection = flattenConnection.flattenConnection;
|
|
19
|
+
exports.Image = Image.Image;
|
|
20
|
+
exports.MediaFile = MediaFile.MediaFile;
|
|
21
|
+
exports.Metafield = Metafield.Metafield;
|
|
22
|
+
exports.parseMetafield = Metafield.parseMetafield;
|
|
23
|
+
exports.parseMetafieldValue = Metafield.parseMetafieldValue;
|
|
24
|
+
exports.ModelViewer = ModelViewer.ModelViewer;
|
|
25
|
+
exports.Money = Money.Money;
|
|
26
|
+
exports.ProductPrice = ProductPrice.ProductPrice;
|
|
27
|
+
exports.ProductProvider = ProductProvider.ProductProvider;
|
|
28
|
+
exports.useProduct = ProductProvider.useProduct;
|
|
29
|
+
exports.ShopifyProvider = ShopifyProvider.ShopifyProvider;
|
|
30
|
+
exports.useShop = ShopifyProvider.useShop;
|
|
31
|
+
exports.ShopPayButton = ShopPayButton.ShopPayButton;
|
|
32
|
+
exports.createStorefrontClient = storefrontClient.createStorefrontClient;
|
|
33
|
+
exports.useMoney = useMoney.useMoney;
|
|
34
|
+
exports.Video = Video.Video;
|
|
35
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { ExternalVideo } from "./ExternalVideo.js";
|
|
2
|
+
import { flattenConnection } from "./flatten-connection.js";
|
|
3
|
+
import { Image } from "./Image.js";
|
|
4
|
+
import { MediaFile } from "./MediaFile.js";
|
|
5
|
+
import { Metafield, parseMetafield, parseMetafieldValue } from "./Metafield.js";
|
|
6
|
+
import { ModelViewer } from "./ModelViewer.js";
|
|
7
|
+
import { Money } from "./Money.js";
|
|
8
|
+
import { ProductPrice } from "./ProductPrice.js";
|
|
9
|
+
import { ProductProvider, useProduct } from "./ProductProvider.js";
|
|
10
|
+
import { ShopifyProvider, useShop } from "./ShopifyProvider.js";
|
|
11
|
+
import { ShopPayButton } from "./ShopPayButton.js";
|
|
12
|
+
import { createStorefrontClient } from "./storefront-client.js";
|
|
13
|
+
import { useMoney } from "./useMoney.js";
|
|
14
|
+
import { Video } from "./Video.js";
|
|
15
|
+
export {
|
|
16
|
+
ExternalVideo,
|
|
17
|
+
Image,
|
|
18
|
+
MediaFile,
|
|
19
|
+
Metafield,
|
|
20
|
+
ModelViewer,
|
|
21
|
+
Money,
|
|
22
|
+
ProductPrice,
|
|
23
|
+
ProductProvider,
|
|
24
|
+
ShopPayButton,
|
|
25
|
+
ShopifyProvider,
|
|
26
|
+
Video,
|
|
27
|
+
createStorefrontClient,
|
|
28
|
+
flattenConnection,
|
|
29
|
+
parseMetafield,
|
|
30
|
+
parseMetafieldValue,
|
|
31
|
+
useMoney,
|
|
32
|
+
useProduct,
|
|
33
|
+
useShop
|
|
34
|
+
};
|
|
35
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
3
|
+
const react = require("react");
|
|
4
|
+
const SCRIPTS_LOADED = {};
|
|
5
|
+
function loadScript(src, options) {
|
|
6
|
+
const isScriptLoaded = SCRIPTS_LOADED[src];
|
|
7
|
+
if (isScriptLoaded) {
|
|
8
|
+
return isScriptLoaded;
|
|
9
|
+
}
|
|
10
|
+
const promise = new Promise((resolve, reject) => {
|
|
11
|
+
const script = document.createElement("script");
|
|
12
|
+
if (options == null ? void 0 : options.module) {
|
|
13
|
+
script.type = "module";
|
|
14
|
+
} else {
|
|
15
|
+
script.type = "text/javascript";
|
|
16
|
+
}
|
|
17
|
+
script.src = src;
|
|
18
|
+
script.onload = () => {
|
|
19
|
+
resolve(true);
|
|
20
|
+
};
|
|
21
|
+
script.onerror = () => {
|
|
22
|
+
reject(false);
|
|
23
|
+
};
|
|
24
|
+
if ((options == null ? void 0 : options.in) === "head") {
|
|
25
|
+
document.head.appendChild(script);
|
|
26
|
+
} else {
|
|
27
|
+
document.body.appendChild(script);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
SCRIPTS_LOADED[src] = promise;
|
|
31
|
+
return promise;
|
|
32
|
+
}
|
|
33
|
+
function useLoadScript(url, options) {
|
|
34
|
+
const [status, setStatus] = react.useState("loading");
|
|
35
|
+
const stringifiedOptions = JSON.stringify(options);
|
|
36
|
+
react.useEffect(() => {
|
|
37
|
+
async function loadScriptWrapper() {
|
|
38
|
+
try {
|
|
39
|
+
setStatus("loading");
|
|
40
|
+
await loadScript(url, options);
|
|
41
|
+
setStatus("done");
|
|
42
|
+
} catch (error) {
|
|
43
|
+
setStatus("error");
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
loadScriptWrapper();
|
|
47
|
+
}, [url, stringifiedOptions, options]);
|
|
48
|
+
return status;
|
|
49
|
+
}
|
|
50
|
+
exports.loadScript = loadScript;
|
|
51
|
+
exports.useLoadScript = useLoadScript;
|
|
52
|
+
//# sourceMappingURL=load-script.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"load-script.cjs","sources":["../../src/load-script.tsx"],"sourcesContent":["import {useState, useEffect} from 'react';\n\nconst SCRIPTS_LOADED: Record<string, Promise<boolean>> = {};\n\nexport function loadScript(\n src: string,\n options?: {module?: boolean; in?: 'head' | 'body'}\n): Promise<boolean> {\n const isScriptLoaded: Promise<boolean> = SCRIPTS_LOADED[src];\n\n if (isScriptLoaded) {\n return isScriptLoaded;\n }\n\n const promise = new Promise<boolean>((resolve, reject) => {\n const script = document.createElement('script');\n if (options?.module) {\n script.type = 'module';\n } else {\n script.type = 'text/javascript';\n }\n script.src = src;\n script.onload = () => {\n resolve(true);\n };\n script.onerror = () => {\n reject(false);\n };\n if (options?.in === 'head') {\n document.head.appendChild(script);\n } else {\n document.body.appendChild(script);\n }\n });\n\n SCRIPTS_LOADED[src] = promise;\n\n return promise;\n}\n\ntype LoadScriptParams = Parameters<typeof loadScript>;\n\n/**\n * The `useLoadScript` hook loads an external script tag in the browser. It allows React components to lazy-load large third-party dependencies.\n */\nexport function useLoadScript(\n url: LoadScriptParams[0],\n options?: LoadScriptParams[1]\n): ScriptState {\n const [status, setStatus] = useState<ScriptState>('loading');\n const stringifiedOptions = JSON.stringify(options);\n\n useEffect(() => {\n async function loadScriptWrapper() {\n try {\n setStatus('loading');\n await loadScript(url, options);\n setStatus('done');\n } catch (error) {\n setStatus('error');\n }\n }\n\n loadScriptWrapper();\n }, [url, stringifiedOptions, options]);\n\n return status;\n}\n\ntype ScriptState = 'loading' | 'done' | 'error';\n"],"names":["SCRIPTS_LOADED","loadScript","src","options","isScriptLoaded","promise","Promise","resolve","reject","script","document","createElement","module","type","onload","onerror","in","head","appendChild","body","useLoadScript","url","status","setStatus","useState","stringifiedOptions","JSON","stringify","useEffect","loadScriptWrapper","error"],"mappings":";;;AAEA,MAAMA,iBAAmD,CAAA;AAEzCC,SAAAA,WACdC,KACAC,SACkB;AAClB,QAAMC,iBAAmCJ,eAAeE;AAExD,MAAIE,gBAAgB;AACXA,WAAAA;AAAAA,EACR;AAED,QAAMC,UAAU,IAAIC,QAAiB,CAACC,SAASC,WAAW;AAClDC,UAAAA,SAASC,SAASC,cAAc,QAAvB;AACf,QAAIR,mCAASS,QAAQ;AACnBH,aAAOI,OAAO;AAAA,IAAA,OACT;AACLJ,aAAOI,OAAO;AAAA,IACf;AACDJ,WAAOP,MAAMA;AACbO,WAAOK,SAAS,MAAM;AACpBP,cAAQ,IAAD;AAAA,IAAA;AAETE,WAAOM,UAAU,MAAM;AACrBP,aAAO,KAAD;AAAA,IAAA;AAEJL,SAAAA,mCAASa,QAAO,QAAQ;AACjBC,eAAAA,KAAKC,YAAYT,MAA1B;AAAA,IAAA,OACK;AACIU,eAAAA,KAAKD,YAAYT,MAA1B;AAAA,IACD;AAAA,EAAA,CAlBa;AAqBhBT,iBAAeE,OAAOG;AAEfA,SAAAA;AACR;AAOee,SAAAA,cACdC,KACAlB,SACa;AACb,QAAM,CAACmB,QAAQC,SAAT,IAAsBC,eAAsB,SAAd;AAC9BC,QAAAA,qBAAqBC,KAAKC,UAAUxB,OAAf;AAE3ByB,QAAAA,UAAU,MAAM;AACd,mBAAeC,oBAAoB;AAC7B,UAAA;AACFN,kBAAU,SAAD;AACHtB,cAAAA,WAAWoB,KAAKlB,OAAN;AAChBoB,kBAAU,MAAD;AAAA,eACFO;AACPP,kBAAU,OAAD;AAAA,MACV;AAAA,IACF;AAEgB;EAChB,GAAA,CAACF,KAAKI,oBAAoBtB,OAA1B,CAZM;AAcFmB,SAAAA;AACR;;;"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { useState, useEffect } from "react";
|
|
2
|
+
const SCRIPTS_LOADED = {};
|
|
3
|
+
function loadScript(src, options) {
|
|
4
|
+
const isScriptLoaded = SCRIPTS_LOADED[src];
|
|
5
|
+
if (isScriptLoaded) {
|
|
6
|
+
return isScriptLoaded;
|
|
7
|
+
}
|
|
8
|
+
const promise = new Promise((resolve, reject) => {
|
|
9
|
+
const script = document.createElement("script");
|
|
10
|
+
if (options == null ? void 0 : options.module) {
|
|
11
|
+
script.type = "module";
|
|
12
|
+
} else {
|
|
13
|
+
script.type = "text/javascript";
|
|
14
|
+
}
|
|
15
|
+
script.src = src;
|
|
16
|
+
script.onload = () => {
|
|
17
|
+
resolve(true);
|
|
18
|
+
};
|
|
19
|
+
script.onerror = () => {
|
|
20
|
+
reject(false);
|
|
21
|
+
};
|
|
22
|
+
if ((options == null ? void 0 : options.in) === "head") {
|
|
23
|
+
document.head.appendChild(script);
|
|
24
|
+
} else {
|
|
25
|
+
document.body.appendChild(script);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
SCRIPTS_LOADED[src] = promise;
|
|
29
|
+
return promise;
|
|
30
|
+
}
|
|
31
|
+
function useLoadScript(url, options) {
|
|
32
|
+
const [status, setStatus] = useState("loading");
|
|
33
|
+
const stringifiedOptions = JSON.stringify(options);
|
|
34
|
+
useEffect(() => {
|
|
35
|
+
async function loadScriptWrapper() {
|
|
36
|
+
try {
|
|
37
|
+
setStatus("loading");
|
|
38
|
+
await loadScript(url, options);
|
|
39
|
+
setStatus("done");
|
|
40
|
+
} catch (error) {
|
|
41
|
+
setStatus("error");
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
loadScriptWrapper();
|
|
45
|
+
}, [url, stringifiedOptions, options]);
|
|
46
|
+
return status;
|
|
47
|
+
}
|
|
48
|
+
export {
|
|
49
|
+
loadScript,
|
|
50
|
+
useLoadScript
|
|
51
|
+
};
|
|
52
|
+
//# sourceMappingURL=load-script.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"load-script.js","sources":["../../src/load-script.tsx"],"sourcesContent":["import {useState, useEffect} from 'react';\n\nconst SCRIPTS_LOADED: Record<string, Promise<boolean>> = {};\n\nexport function loadScript(\n src: string,\n options?: {module?: boolean; in?: 'head' | 'body'}\n): Promise<boolean> {\n const isScriptLoaded: Promise<boolean> = SCRIPTS_LOADED[src];\n\n if (isScriptLoaded) {\n return isScriptLoaded;\n }\n\n const promise = new Promise<boolean>((resolve, reject) => {\n const script = document.createElement('script');\n if (options?.module) {\n script.type = 'module';\n } else {\n script.type = 'text/javascript';\n }\n script.src = src;\n script.onload = () => {\n resolve(true);\n };\n script.onerror = () => {\n reject(false);\n };\n if (options?.in === 'head') {\n document.head.appendChild(script);\n } else {\n document.body.appendChild(script);\n }\n });\n\n SCRIPTS_LOADED[src] = promise;\n\n return promise;\n}\n\ntype LoadScriptParams = Parameters<typeof loadScript>;\n\n/**\n * The `useLoadScript` hook loads an external script tag in the browser. It allows React components to lazy-load large third-party dependencies.\n */\nexport function useLoadScript(\n url: LoadScriptParams[0],\n options?: LoadScriptParams[1]\n): ScriptState {\n const [status, setStatus] = useState<ScriptState>('loading');\n const stringifiedOptions = JSON.stringify(options);\n\n useEffect(() => {\n async function loadScriptWrapper() {\n try {\n setStatus('loading');\n await loadScript(url, options);\n setStatus('done');\n } catch (error) {\n setStatus('error');\n }\n }\n\n loadScriptWrapper();\n }, [url, stringifiedOptions, options]);\n\n return status;\n}\n\ntype ScriptState = 'loading' | 'done' | 'error';\n"],"names":["SCRIPTS_LOADED","loadScript","src","options","isScriptLoaded","promise","Promise","resolve","reject","script","document","createElement","module","type","onload","onerror","in","head","appendChild","body","useLoadScript","url","status","setStatus","useState","stringifiedOptions","JSON","stringify","useEffect","loadScriptWrapper","error"],"mappings":";AAEA,MAAMA,iBAAmD,CAAA;AAEzCC,SAAAA,WACdC,KACAC,SACkB;AAClB,QAAMC,iBAAmCJ,eAAeE;AAExD,MAAIE,gBAAgB;AACXA,WAAAA;AAAAA,EACR;AAED,QAAMC,UAAU,IAAIC,QAAiB,CAACC,SAASC,WAAW;AAClDC,UAAAA,SAASC,SAASC,cAAc,QAAvB;AACf,QAAIR,mCAASS,QAAQ;AACnBH,aAAOI,OAAO;AAAA,IAAA,OACT;AACLJ,aAAOI,OAAO;AAAA,IACf;AACDJ,WAAOP,MAAMA;AACbO,WAAOK,SAAS,MAAM;AACpBP,cAAQ,IAAD;AAAA,IAAA;AAETE,WAAOM,UAAU,MAAM;AACrBP,aAAO,KAAD;AAAA,IAAA;AAEJL,SAAAA,mCAASa,QAAO,QAAQ;AACjBC,eAAAA,KAAKC,YAAYT,MAA1B;AAAA,IAAA,OACK;AACIU,eAAAA,KAAKD,YAAYT,MAA1B;AAAA,IACD;AAAA,EAAA,CAlBa;AAqBhBT,iBAAeE,OAAOG;AAEfA,SAAAA;AACR;AAOee,SAAAA,cACdC,KACAlB,SACa;AACb,QAAM,CAACmB,QAAQC,SAAT,IAAsBC,SAAsB,SAAd;AAC9BC,QAAAA,qBAAqBC,KAAKC,UAAUxB,OAAf;AAE3ByB,YAAU,MAAM;AACd,mBAAeC,oBAAoB;AAC7B,UAAA;AACFN,kBAAU,SAAD;AACHtB,cAAAA,WAAWoB,KAAKlB,OAAN;AAChBoB,kBAAU,MAAD;AAAA,eACFO;AACPP,kBAAU,OAAD;AAAA,MACV;AAAA,IACF;AAEgB;EAChB,GAAA,CAACF,KAAKI,oBAAoBtB,OAA1B,CAZM;AAcFmB,SAAAA;AACR;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storefront-api-constants.cjs","sources":["../../src/storefront-api-constants.ts"],"sourcesContent":["export const SFAPI_VERSION = '2022-07';\n"],"names":[],"mappings":";;AAAO,MAAM,gBAAgB;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storefront-api-constants.js","sources":["../../src/storefront-api-constants.ts"],"sourcesContent":["export const SFAPI_VERSION = '2022-07';\n"],"names":[],"mappings":"AAAO,MAAM,gBAAgB;"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
3
|
+
const storefrontApiConstants = require("./storefront-api-constants.cjs");
|
|
4
|
+
function createStorefrontClient({
|
|
5
|
+
storeDomain,
|
|
6
|
+
privateStorefrontToken,
|
|
7
|
+
publicStorefrontToken,
|
|
8
|
+
storefrontApiVersion,
|
|
9
|
+
contentType
|
|
10
|
+
}) {
|
|
11
|
+
if (storefrontApiVersion !== storefrontApiConstants.SFAPI_VERSION) {
|
|
12
|
+
console.warn(
|
|
13
|
+
`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}`
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
return {
|
|
17
|
+
getStorefrontApiUrl(overrideProps) {
|
|
18
|
+
var _a, _b;
|
|
19
|
+
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`;
|
|
20
|
+
},
|
|
21
|
+
getPrivateTokenHeaders(overrideProps) {
|
|
22
|
+
var _a, _b, _c;
|
|
23
|
+
if (!privateStorefrontToken && !(overrideProps == null ? void 0 : overrideProps.privateStorefrontToken)) {
|
|
24
|
+
throw new Error(
|
|
25
|
+
`StorefrontClient: You did not pass in a 'privateStorefrontToken' while using 'getPrivateTokenHeaders()'`
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
const finalContentType = (_a = overrideProps == null ? void 0 : overrideProps.contentType) != null ? _a : contentType;
|
|
29
|
+
return {
|
|
30
|
+
"content-type": finalContentType === "graphql" ? "application/graphql" : "application/json",
|
|
31
|
+
"X-SDK-Variant": "hydrogen-ui",
|
|
32
|
+
"X-SDK-Variant-Source": "react",
|
|
33
|
+
"X-SDK-Version": storefrontApiVersion,
|
|
34
|
+
"Shopify-Storefront-Private-Token": (_c = (_b = overrideProps == null ? void 0 : overrideProps.privateStorefrontToken) != null ? _b : privateStorefrontToken) != null ? _c : "",
|
|
35
|
+
...(overrideProps == null ? void 0 : overrideProps.buyerIp) ? { "Shopify-Storefront-Buyer-IP": overrideProps.buyerIp } : {}
|
|
36
|
+
};
|
|
37
|
+
},
|
|
38
|
+
getPublicTokenHeaders(overrideProps) {
|
|
39
|
+
var _a, _b, _c;
|
|
40
|
+
if (!publicStorefrontToken && !(overrideProps == null ? void 0 : overrideProps.publicStorefrontToken)) {
|
|
41
|
+
throw new Error(
|
|
42
|
+
`StorefrontClient: You did not pass in a 'publicStorefrontToken' while using 'getPublicTokenHeaders()'`
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
const finalContentType = (_a = overrideProps == null ? void 0 : overrideProps.contentType) != null ? _a : contentType;
|
|
46
|
+
return {
|
|
47
|
+
"content-type": finalContentType === "graphql" ? "application/graphql" : "application/json",
|
|
48
|
+
"X-SDK-Variant": "hydrogen-ui",
|
|
49
|
+
"X-SDK-Variant-Source": "react",
|
|
50
|
+
"X-SDK-Version": storefrontApiVersion,
|
|
51
|
+
"X-Shopify-Storefront-Access-Token": (_c = (_b = overrideProps == null ? void 0 : overrideProps.publicStorefrontToken) != null ? _b : publicStorefrontToken) != null ? _c : ""
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
exports.createStorefrontClient = createStorefrontClient;
|
|
57
|
+
//# sourceMappingURL=storefront-client.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storefront-client.cjs","sources":["../../src/storefront-client.ts"],"sourcesContent":["import {SFAPI_VERSION} from './storefront-api-constants.js';\n\n/**\n * The `createStorefrontClient()` function creates helpers that enable you to quickly query the Shopify Storefront API.\n *\n * When used on the server, it is recommended to use the `privateStorefrontToken` prop. When used on the client, it is recommended to use the `publicStorefrontToken` prop.\n */\nexport function createStorefrontClient({\n storeDomain,\n privateStorefrontToken,\n publicStorefrontToken,\n storefrontApiVersion,\n contentType,\n}: StorefrontClientProps): StorefrontClientReturn {\n if (storefrontApiVersion !== SFAPI_VERSION) {\n console.warn(\n `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 ${SFAPI_VERSION}`\n );\n }\n\n // only warn if not in a browser environment\n if (__HYDROGEN_DEV__ && !privateStorefrontToken && !globalThis.document) {\n console.warn(\n `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. `\n );\n }\n\n // only warn if in a browser environment and you're using the privateStorefrontToken\n if (__HYDROGEN_DEV__ && privateStorefrontToken && globalThis) {\n console.warn(\n `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`\n );\n }\n\n return {\n getStorefrontApiUrl(overrideProps) {\n return `https://${\n overrideProps?.storeDomain ?? storeDomain\n }.myshopify.com/api/${\n overrideProps?.storefrontApiVersion ?? storefrontApiVersion\n }/graphql.json`;\n },\n getPrivateTokenHeaders(overrideProps) {\n if (!privateStorefrontToken && !overrideProps?.privateStorefrontToken) {\n throw new Error(\n `StorefrontClient: You did not pass in a 'privateStorefrontToken' while using 'getPrivateTokenHeaders()'`\n );\n }\n\n if (__HYDROGEN_DEV__ && !overrideProps?.buyerIp) {\n console.warn(\n `StorefrontClient: it is recommended to pass in the 'buyerIp' property which improves analytics and data in the admin.`\n );\n }\n\n const finalContentType = overrideProps?.contentType ?? contentType;\n\n return {\n // default to json\n 'content-type':\n finalContentType === 'graphql'\n ? 'application/graphql'\n : 'application/json',\n 'X-SDK-Variant': 'hydrogen-ui',\n 'X-SDK-Variant-Source': 'react',\n 'X-SDK-Version': storefrontApiVersion,\n 'Shopify-Storefront-Private-Token':\n overrideProps?.privateStorefrontToken ?? privateStorefrontToken ?? '',\n ...(overrideProps?.buyerIp\n ? {'Shopify-Storefront-Buyer-IP': overrideProps.buyerIp}\n : {}),\n };\n },\n getPublicTokenHeaders(overrideProps) {\n if (!publicStorefrontToken && !overrideProps?.publicStorefrontToken) {\n throw new Error(\n `StorefrontClient: You did not pass in a 'publicStorefrontToken' while using 'getPublicTokenHeaders()'`\n );\n }\n\n const finalContentType = overrideProps?.contentType ?? contentType;\n\n return {\n // default to json\n 'content-type':\n finalContentType === 'graphql'\n ? 'application/graphql'\n : 'application/json',\n 'X-SDK-Variant': 'hydrogen-ui',\n 'X-SDK-Variant-Source': 'react',\n 'X-SDK-Version': storefrontApiVersion,\n 'X-Shopify-Storefront-Access-Token':\n overrideProps?.publicStorefrontToken ?? publicStorefrontToken ?? '',\n };\n },\n };\n}\n\ntype StorefrontClientProps = {\n /** The host name of the domain (eg: `{shop}.myshopify.com`). */\n storeDomain: string;\n /** The Storefront API delegate access token. Refer to the [authentication](https://shopify.dev/api/storefront#authentication) and [delegate access token](https://shopify.dev/apps/auth/oauth/delegate-access-tokens) documentation for more details. */\n privateStorefrontToken?: string;\n /** The Storefront API access token. Refer to the [authentication](https://shopify.dev/api/storefront#authentication) documentation for more details. */\n publicStorefrontToken?: string;\n /** The Storefront API version. This should almost always be the same as the version Hydrogen-UI was built for. Learn more about Shopify [API versioning](https://shopify.dev/api/usage/versioning) for more details. */\n storefrontApiVersion: string;\n /**\n * Customizes which `\"content-type\"` header is added when using `getPrivateTokenHeaders()` and `getPublicTokenHeaders()`. When fetching with a `JSON.stringify()`-ed `body`, use `\"json\"`. When fetching with a `body` that is a plain string, use `\"graphql\"`. Defaults to `\"json\"`\n *\n * Can also be customized on a call-by-call basis by passing in `'contentType'` to both `getPrivateTokenHeaders({...})` and `getPublicTokenHeaders({...})`, for example: `getPublicTokenHeaders({contentType: 'graphql'})`\n */\n contentType?: 'json' | 'graphql';\n};\n\ntype OverrideTokenHeaderProps = Partial<\n Pick<StorefrontClientProps, 'contentType'>\n>;\n\ntype StorefrontClientReturn = {\n /**\n * Creates the fully-qualified URL to your store's GraphQL endpoint.\n *\n * By default, it will use the config you passed in when calling `createStorefrontClient()`. However, you can override the following settings on each invocation of `getStorefrontApiUrl({...})`:\n *\n * - `storeDomain`\n * - `storefrontApiVersion`\n */\n getStorefrontApiUrl: (\n props?: Partial<\n Pick<StorefrontClientProps, 'storeDomain' | 'storefrontApiVersion'>\n >\n ) => string;\n /**\n * Returns an object that contains headers that are needed for each query to Storefront API GraphQL endpoint. This method uses the private Server-to-Server token which reduces the chance of throttling but must not be exposed to clients. Server-side calls should prefer using this over `getPublicTokenHeaders()`.\n *\n * By default, it will use the config you passed in when calling `createStorefrontClient()`. However, you can override the following settings on each invocation of `getPrivateTokenHeaders({...})`:\n *\n * - `contentType`\n * - `privateStorefrontToken`\n * - `buyerIp`\n *\n * Note that `contentType` defaults to what you configured in `createStorefrontClient({...})` and defaults to `'json'`, but a specific call may require using `graphql`. When using `JSON.stringify()` on the `body`, use `'json'`; otherwise, use `'graphql'`.\n */\n getPrivateTokenHeaders: (\n props?: OverrideTokenHeaderProps &\n Pick<StorefrontClientProps, 'privateStorefrontToken'> & {\n /**\n * The client's IP address. Passing this to the Storefront API when using a server-to-server token will help improve your store's analytics data.\n */\n buyerIp?: string;\n }\n ) => Record<string, string>;\n /**\n * Returns an object that contains headers that are needed for each query to Storefront API GraphQL endpoint. This method uses the private Server-to-Server token which reduces the chance of throttling but must not be exposed to clients. Server-side calls should prefer using this over `getPublicTokenHeaders()`.\n *\n * By default, it will use the config you passed in when calling `createStorefrontClient()`. However, you can override the following settings on each invocation of `getPrivateTokenHeaders({...})`:\n *\n * - `contentType`\n * - `publicStorefrontToken`\n *\n * Note that `contentType` defaults to what you configured in `createStorefrontClient({...})` and defaults to `'json'`, but a specific call may require using `graphql`. When using `JSON.stringify()` on the `body`, use `'json'`; otherwise, use `'graphql'`.\n */\n getPublicTokenHeaders: (\n props?: OverrideTokenHeaderProps &\n Pick<StorefrontClientProps, 'publicStorefrontToken'>\n ) => Record<string, string>;\n};\n"],"names":["SFAPI_VERSION"],"mappings":";;;AAOO,SAAS,uBAAuB;AAAA,EACrC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GAAkD;AAChD,MAAI,yBAAyBA,uBAAAA,eAAe;AAClC,YAAA;AAAA,MACN,4NAA4N,2CAA2CA,uBAAA;AAAA,IAAA;AAAA,EAE3Q;AAgBO,SAAA;AAAA,IACL,oBAAoB,eAAe;;AACjC,aAAO,YACL,oDAAe,gBAAf,YAA8B,kCAE9B,oDAAe,yBAAf,YAAuC;AAAA,IAE3C;AAAA,IACA,uBAAuB,eAAe;;AACpC,UAAI,CAAC,0BAA0B,EAAC,+CAAe,yBAAwB;AACrE,cAAM,IAAI;AAAA,UACR;AAAA,QAAA;AAAA,MAEJ;AAQM,YAAA,oBAAmB,oDAAe,gBAAf,YAA8B;AAEhD,aAAA;AAAA,QAEL,gBACE,qBAAqB,YACjB,wBACA;AAAA,QACN,iBAAiB;AAAA,QACjB,wBAAwB;AAAA,QACxB,iBAAiB;AAAA,QACjB,qCACE,0DAAe,2BAAf,YAAyC,2BAAzC,YAAmE;AAAA,QACrE,IAAI,+CAAe,WACf,EAAC,+BAA+B,cAAc,QAAA,IAC9C,CAAC;AAAA,MAAA;AAAA,IAET;AAAA,IACA,sBAAsB,eAAe;;AACnC,UAAI,CAAC,yBAAyB,EAAC,+CAAe,wBAAuB;AACnE,cAAM,IAAI;AAAA,UACR;AAAA,QAAA;AAAA,MAEJ;AAEM,YAAA,oBAAmB,oDAAe,gBAAf,YAA8B;AAEhD,aAAA;AAAA,QAEL,gBACE,qBAAqB,YACjB,wBACA;AAAA,QACN,iBAAiB;AAAA,QACjB,wBAAwB;AAAA,QACxB,iBAAiB;AAAA,QACjB,sCACE,0DAAe,0BAAf,YAAwC,0BAAxC,YAAiE;AAAA,MAAA;AAAA,IAEvE;AAAA,EAAA;AAEJ;;"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { SFAPI_VERSION } from "./storefront-api-constants.js";
|
|
2
|
+
function createStorefrontClient({
|
|
3
|
+
storeDomain,
|
|
4
|
+
privateStorefrontToken,
|
|
5
|
+
publicStorefrontToken,
|
|
6
|
+
storefrontApiVersion,
|
|
7
|
+
contentType
|
|
8
|
+
}) {
|
|
9
|
+
if (storefrontApiVersion !== SFAPI_VERSION) {
|
|
10
|
+
console.warn(
|
|
11
|
+
`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 ${SFAPI_VERSION}`
|
|
12
|
+
);
|
|
13
|
+
}
|
|
14
|
+
return {
|
|
15
|
+
getStorefrontApiUrl(overrideProps) {
|
|
16
|
+
var _a, _b;
|
|
17
|
+
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`;
|
|
18
|
+
},
|
|
19
|
+
getPrivateTokenHeaders(overrideProps) {
|
|
20
|
+
var _a, _b, _c;
|
|
21
|
+
if (!privateStorefrontToken && !(overrideProps == null ? void 0 : overrideProps.privateStorefrontToken)) {
|
|
22
|
+
throw new Error(
|
|
23
|
+
`StorefrontClient: You did not pass in a 'privateStorefrontToken' while using 'getPrivateTokenHeaders()'`
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
const finalContentType = (_a = overrideProps == null ? void 0 : overrideProps.contentType) != null ? _a : contentType;
|
|
27
|
+
return {
|
|
28
|
+
"content-type": finalContentType === "graphql" ? "application/graphql" : "application/json",
|
|
29
|
+
"X-SDK-Variant": "hydrogen-ui",
|
|
30
|
+
"X-SDK-Variant-Source": "react",
|
|
31
|
+
"X-SDK-Version": storefrontApiVersion,
|
|
32
|
+
"Shopify-Storefront-Private-Token": (_c = (_b = overrideProps == null ? void 0 : overrideProps.privateStorefrontToken) != null ? _b : privateStorefrontToken) != null ? _c : "",
|
|
33
|
+
...(overrideProps == null ? void 0 : overrideProps.buyerIp) ? { "Shopify-Storefront-Buyer-IP": overrideProps.buyerIp } : {}
|
|
34
|
+
};
|
|
35
|
+
},
|
|
36
|
+
getPublicTokenHeaders(overrideProps) {
|
|
37
|
+
var _a, _b, _c;
|
|
38
|
+
if (!publicStorefrontToken && !(overrideProps == null ? void 0 : overrideProps.publicStorefrontToken)) {
|
|
39
|
+
throw new Error(
|
|
40
|
+
`StorefrontClient: You did not pass in a 'publicStorefrontToken' while using 'getPublicTokenHeaders()'`
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
const finalContentType = (_a = overrideProps == null ? void 0 : overrideProps.contentType) != null ? _a : contentType;
|
|
44
|
+
return {
|
|
45
|
+
"content-type": finalContentType === "graphql" ? "application/graphql" : "application/json",
|
|
46
|
+
"X-SDK-Variant": "hydrogen-ui",
|
|
47
|
+
"X-SDK-Variant-Source": "react",
|
|
48
|
+
"X-SDK-Version": storefrontApiVersion,
|
|
49
|
+
"X-Shopify-Storefront-Access-Token": (_c = (_b = overrideProps == null ? void 0 : overrideProps.publicStorefrontToken) != null ? _b : publicStorefrontToken) != null ? _c : ""
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
export {
|
|
55
|
+
createStorefrontClient
|
|
56
|
+
};
|
|
57
|
+
//# sourceMappingURL=storefront-client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storefront-client.js","sources":["../../src/storefront-client.ts"],"sourcesContent":["import {SFAPI_VERSION} from './storefront-api-constants.js';\n\n/**\n * The `createStorefrontClient()` function creates helpers that enable you to quickly query the Shopify Storefront API.\n *\n * When used on the server, it is recommended to use the `privateStorefrontToken` prop. When used on the client, it is recommended to use the `publicStorefrontToken` prop.\n */\nexport function createStorefrontClient({\n storeDomain,\n privateStorefrontToken,\n publicStorefrontToken,\n storefrontApiVersion,\n contentType,\n}: StorefrontClientProps): StorefrontClientReturn {\n if (storefrontApiVersion !== SFAPI_VERSION) {\n console.warn(\n `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 ${SFAPI_VERSION}`\n );\n }\n\n // only warn if not in a browser environment\n if (__HYDROGEN_DEV__ && !privateStorefrontToken && !globalThis.document) {\n console.warn(\n `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. `\n );\n }\n\n // only warn if in a browser environment and you're using the privateStorefrontToken\n if (__HYDROGEN_DEV__ && privateStorefrontToken && globalThis) {\n console.warn(\n `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`\n );\n }\n\n return {\n getStorefrontApiUrl(overrideProps) {\n return `https://${\n overrideProps?.storeDomain ?? storeDomain\n }.myshopify.com/api/${\n overrideProps?.storefrontApiVersion ?? storefrontApiVersion\n }/graphql.json`;\n },\n getPrivateTokenHeaders(overrideProps) {\n if (!privateStorefrontToken && !overrideProps?.privateStorefrontToken) {\n throw new Error(\n `StorefrontClient: You did not pass in a 'privateStorefrontToken' while using 'getPrivateTokenHeaders()'`\n );\n }\n\n if (__HYDROGEN_DEV__ && !overrideProps?.buyerIp) {\n console.warn(\n `StorefrontClient: it is recommended to pass in the 'buyerIp' property which improves analytics and data in the admin.`\n );\n }\n\n const finalContentType = overrideProps?.contentType ?? contentType;\n\n return {\n // default to json\n 'content-type':\n finalContentType === 'graphql'\n ? 'application/graphql'\n : 'application/json',\n 'X-SDK-Variant': 'hydrogen-ui',\n 'X-SDK-Variant-Source': 'react',\n 'X-SDK-Version': storefrontApiVersion,\n 'Shopify-Storefront-Private-Token':\n overrideProps?.privateStorefrontToken ?? privateStorefrontToken ?? '',\n ...(overrideProps?.buyerIp\n ? {'Shopify-Storefront-Buyer-IP': overrideProps.buyerIp}\n : {}),\n };\n },\n getPublicTokenHeaders(overrideProps) {\n if (!publicStorefrontToken && !overrideProps?.publicStorefrontToken) {\n throw new Error(\n `StorefrontClient: You did not pass in a 'publicStorefrontToken' while using 'getPublicTokenHeaders()'`\n );\n }\n\n const finalContentType = overrideProps?.contentType ?? contentType;\n\n return {\n // default to json\n 'content-type':\n finalContentType === 'graphql'\n ? 'application/graphql'\n : 'application/json',\n 'X-SDK-Variant': 'hydrogen-ui',\n 'X-SDK-Variant-Source': 'react',\n 'X-SDK-Version': storefrontApiVersion,\n 'X-Shopify-Storefront-Access-Token':\n overrideProps?.publicStorefrontToken ?? publicStorefrontToken ?? '',\n };\n },\n };\n}\n\ntype StorefrontClientProps = {\n /** The host name of the domain (eg: `{shop}.myshopify.com`). */\n storeDomain: string;\n /** The Storefront API delegate access token. Refer to the [authentication](https://shopify.dev/api/storefront#authentication) and [delegate access token](https://shopify.dev/apps/auth/oauth/delegate-access-tokens) documentation for more details. */\n privateStorefrontToken?: string;\n /** The Storefront API access token. Refer to the [authentication](https://shopify.dev/api/storefront#authentication) documentation for more details. */\n publicStorefrontToken?: string;\n /** The Storefront API version. This should almost always be the same as the version Hydrogen-UI was built for. Learn more about Shopify [API versioning](https://shopify.dev/api/usage/versioning) for more details. */\n storefrontApiVersion: string;\n /**\n * Customizes which `\"content-type\"` header is added when using `getPrivateTokenHeaders()` and `getPublicTokenHeaders()`. When fetching with a `JSON.stringify()`-ed `body`, use `\"json\"`. When fetching with a `body` that is a plain string, use `\"graphql\"`. Defaults to `\"json\"`\n *\n * Can also be customized on a call-by-call basis by passing in `'contentType'` to both `getPrivateTokenHeaders({...})` and `getPublicTokenHeaders({...})`, for example: `getPublicTokenHeaders({contentType: 'graphql'})`\n */\n contentType?: 'json' | 'graphql';\n};\n\ntype OverrideTokenHeaderProps = Partial<\n Pick<StorefrontClientProps, 'contentType'>\n>;\n\ntype StorefrontClientReturn = {\n /**\n * Creates the fully-qualified URL to your store's GraphQL endpoint.\n *\n * By default, it will use the config you passed in when calling `createStorefrontClient()`. However, you can override the following settings on each invocation of `getStorefrontApiUrl({...})`:\n *\n * - `storeDomain`\n * - `storefrontApiVersion`\n */\n getStorefrontApiUrl: (\n props?: Partial<\n Pick<StorefrontClientProps, 'storeDomain' | 'storefrontApiVersion'>\n >\n ) => string;\n /**\n * Returns an object that contains headers that are needed for each query to Storefront API GraphQL endpoint. This method uses the private Server-to-Server token which reduces the chance of throttling but must not be exposed to clients. Server-side calls should prefer using this over `getPublicTokenHeaders()`.\n *\n * By default, it will use the config you passed in when calling `createStorefrontClient()`. However, you can override the following settings on each invocation of `getPrivateTokenHeaders({...})`:\n *\n * - `contentType`\n * - `privateStorefrontToken`\n * - `buyerIp`\n *\n * Note that `contentType` defaults to what you configured in `createStorefrontClient({...})` and defaults to `'json'`, but a specific call may require using `graphql`. When using `JSON.stringify()` on the `body`, use `'json'`; otherwise, use `'graphql'`.\n */\n getPrivateTokenHeaders: (\n props?: OverrideTokenHeaderProps &\n Pick<StorefrontClientProps, 'privateStorefrontToken'> & {\n /**\n * The client's IP address. Passing this to the Storefront API when using a server-to-server token will help improve your store's analytics data.\n */\n buyerIp?: string;\n }\n ) => Record<string, string>;\n /**\n * Returns an object that contains headers that are needed for each query to Storefront API GraphQL endpoint. This method uses the private Server-to-Server token which reduces the chance of throttling but must not be exposed to clients. Server-side calls should prefer using this over `getPublicTokenHeaders()`.\n *\n * By default, it will use the config you passed in when calling `createStorefrontClient()`. However, you can override the following settings on each invocation of `getPrivateTokenHeaders({...})`:\n *\n * - `contentType`\n * - `publicStorefrontToken`\n *\n * Note that `contentType` defaults to what you configured in `createStorefrontClient({...})` and defaults to `'json'`, but a specific call may require using `graphql`. When using `JSON.stringify()` on the `body`, use `'json'`; otherwise, use `'graphql'`.\n */\n getPublicTokenHeaders: (\n props?: OverrideTokenHeaderProps &\n Pick<StorefrontClientProps, 'publicStorefrontToken'>\n ) => Record<string, string>;\n};\n"],"names":[],"mappings":";AAOO,SAAS,uBAAuB;AAAA,EACrC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GAAkD;AAChD,MAAI,yBAAyB,eAAe;AAClC,YAAA;AAAA,MACN,4NAA4N,2CAA2C;AAAA,IAAA;AAAA,EAE3Q;AAgBO,SAAA;AAAA,IACL,oBAAoB,eAAe;;AACjC,aAAO,YACL,oDAAe,gBAAf,YAA8B,kCAE9B,oDAAe,yBAAf,YAAuC;AAAA,IAE3C;AAAA,IACA,uBAAuB,eAAe;;AACpC,UAAI,CAAC,0BAA0B,EAAC,+CAAe,yBAAwB;AACrE,cAAM,IAAI;AAAA,UACR;AAAA,QAAA;AAAA,MAEJ;AAQM,YAAA,oBAAmB,oDAAe,gBAAf,YAA8B;AAEhD,aAAA;AAAA,QAEL,gBACE,qBAAqB,YACjB,wBACA;AAAA,QACN,iBAAiB;AAAA,QACjB,wBAAwB;AAAA,QACxB,iBAAiB;AAAA,QACjB,qCACE,0DAAe,2BAAf,YAAyC,2BAAzC,YAAmE;AAAA,QACrE,IAAI,+CAAe,WACf,EAAC,+BAA+B,cAAc,QAAA,IAC9C,CAAC;AAAA,MAAA;AAAA,IAET;AAAA,IACA,sBAAsB,eAAe;;AACnC,UAAI,CAAC,yBAAyB,EAAC,+CAAe,wBAAuB;AACnE,cAAM,IAAI;AAAA,UACR;AAAA,QAAA;AAAA,MAEJ;AAEM,YAAA,oBAAmB,oDAAe,gBAAf,YAA8B;AAEhD,aAAA;AAAA,QAEL,gBACE,qBAAqB,YACjB,wBACA;AAAA,QACN,iBAAiB;AAAA,QACjB,wBAAwB;AAAA,QACxB,iBAAiB;AAAA,QACjB,sCACE,0DAAe,0BAAf,YAAwC,0BAAxC,YAAiE;AAAA,MAAA;AAAA,IAEvE;AAAA,EAAA;AAEJ;"}
|