@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,72 @@
|
|
|
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
|
+
if (!privateStorefrontToken && !globalThis.document) {
|
|
15
|
+
console.warn(
|
|
16
|
+
`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. `
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
if (privateStorefrontToken && globalThis) {
|
|
20
|
+
console.warn(
|
|
21
|
+
`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`
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
return {
|
|
25
|
+
getStorefrontApiUrl(overrideProps) {
|
|
26
|
+
var _a, _b;
|
|
27
|
+
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`;
|
|
28
|
+
},
|
|
29
|
+
getPrivateTokenHeaders(overrideProps) {
|
|
30
|
+
var _a, _b, _c;
|
|
31
|
+
if (!privateStorefrontToken && !(overrideProps == null ? void 0 : overrideProps.privateStorefrontToken)) {
|
|
32
|
+
throw new Error(
|
|
33
|
+
`StorefrontClient: You did not pass in a 'privateStorefrontToken' while using 'getPrivateTokenHeaders()'`
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
if (!(overrideProps == null ? void 0 : overrideProps.buyerIp)) {
|
|
37
|
+
console.warn(
|
|
38
|
+
`StorefrontClient: it is recommended to pass in the 'buyerIp' property which improves analytics and data in the admin.`
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
const finalContentType = (_a = overrideProps == null ? void 0 : overrideProps.contentType) != null ? _a : contentType;
|
|
42
|
+
return {
|
|
43
|
+
"content-type": finalContentType === "graphql" ? "application/graphql" : "application/json",
|
|
44
|
+
"X-SDK-Variant": "hydrogen-ui",
|
|
45
|
+
"X-SDK-Variant-Source": "react",
|
|
46
|
+
"X-SDK-Version": storefrontApiVersion,
|
|
47
|
+
"Shopify-Storefront-Private-Token": (_c = (_b = overrideProps == null ? void 0 : overrideProps.privateStorefrontToken) != null ? _b : privateStorefrontToken) != null ? _c : "",
|
|
48
|
+
...(overrideProps == null ? void 0 : overrideProps.buyerIp) ? { "Shopify-Storefront-Buyer-IP": overrideProps.buyerIp } : {}
|
|
49
|
+
};
|
|
50
|
+
},
|
|
51
|
+
getPublicTokenHeaders(overrideProps) {
|
|
52
|
+
var _a, _b, _c;
|
|
53
|
+
if (!publicStorefrontToken && !(overrideProps == null ? void 0 : overrideProps.publicStorefrontToken)) {
|
|
54
|
+
throw new Error(
|
|
55
|
+
`StorefrontClient: You did not pass in a 'publicStorefrontToken' while using 'getPublicTokenHeaders()'`
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
const finalContentType = (_a = overrideProps == null ? void 0 : overrideProps.contentType) != null ? _a : contentType;
|
|
59
|
+
return {
|
|
60
|
+
"content-type": finalContentType === "graphql" ? "application/graphql" : "application/json",
|
|
61
|
+
"X-SDK-Variant": "hydrogen-ui",
|
|
62
|
+
"X-SDK-Variant-Source": "react",
|
|
63
|
+
"X-SDK-Version": storefrontApiVersion,
|
|
64
|
+
"X-Shopify-Storefront-Access-Token": (_c = (_b = overrideProps == null ? void 0 : overrideProps.publicStorefrontToken) != null ? _b : publicStorefrontToken) != null ? _c : ""
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
export {
|
|
70
|
+
createStorefrontClient
|
|
71
|
+
};
|
|
72
|
+
//# 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;AAGA,MAAwB,CAAC,0BAA0B,CAAC,WAAW,UAAU;AAC/D,YAAA;AAAA,MACN;AAAA,IAAA;AAAA,EAEJ;AAGI,MAAoB,0BAA0B,YAAY;AACpD,YAAA;AAAA,MACN;AAAA,IAAA;AAAA,EAEJ;AAEO,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;AAEI,UAAoB,EAAC,+CAAe,UAAS;AACvC,gBAAA;AAAA,UACN;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,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,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
3
|
+
const react = require("react");
|
|
4
|
+
const ShopifyProvider = require("./ShopifyProvider.cjs");
|
|
5
|
+
function useMoney(money) {
|
|
6
|
+
const {
|
|
7
|
+
locale
|
|
8
|
+
} = ShopifyProvider.useShop();
|
|
9
|
+
if (!locale) {
|
|
10
|
+
throw new Error(`useMoney(): Unable to get 'locale' from 'useShop()', which means that 'locale' was not passed to '<ShopifyProvider/>'. 'locale' is required for 'useMoney()' to work`);
|
|
11
|
+
}
|
|
12
|
+
const amount = parseFloat(money.amount);
|
|
13
|
+
const options = react.useMemo(() => ({
|
|
14
|
+
style: "currency",
|
|
15
|
+
currency: money.currencyCode
|
|
16
|
+
}), [money.currencyCode]);
|
|
17
|
+
const defaultFormatter = useLazyFormatter(locale, options);
|
|
18
|
+
const nameFormatter = useLazyFormatter(locale, {
|
|
19
|
+
...options,
|
|
20
|
+
currencyDisplay: "name"
|
|
21
|
+
});
|
|
22
|
+
const narrowSymbolFormatter = useLazyFormatter(locale, {
|
|
23
|
+
...options,
|
|
24
|
+
currencyDisplay: "narrowSymbol"
|
|
25
|
+
});
|
|
26
|
+
const withoutTrailingZerosFormatter = useLazyFormatter(locale, {
|
|
27
|
+
...options,
|
|
28
|
+
minimumFractionDigits: 0,
|
|
29
|
+
maximumFractionDigits: 0
|
|
30
|
+
});
|
|
31
|
+
const withoutCurrencyFormatter = useLazyFormatter(locale);
|
|
32
|
+
const withoutTrailingZerosOrCurrencyFormatter = useLazyFormatter(locale, {
|
|
33
|
+
minimumFractionDigits: 0,
|
|
34
|
+
maximumFractionDigits: 0
|
|
35
|
+
});
|
|
36
|
+
const isPartCurrency = (part) => part.type === "currency";
|
|
37
|
+
const lazyFormatters = react.useMemo(() => ({
|
|
38
|
+
original: () => money,
|
|
39
|
+
currencyCode: () => money.currencyCode,
|
|
40
|
+
localizedString: () => defaultFormatter().format(amount),
|
|
41
|
+
parts: () => defaultFormatter().formatToParts(amount),
|
|
42
|
+
withoutTrailingZeros: () => amount % 1 === 0 ? withoutTrailingZerosFormatter().format(amount) : defaultFormatter().format(amount),
|
|
43
|
+
withoutTrailingZerosAndCurrency: () => amount % 1 === 0 ? withoutTrailingZerosOrCurrencyFormatter().format(amount) : withoutCurrencyFormatter().format(amount),
|
|
44
|
+
currencyName: () => {
|
|
45
|
+
var _a, _b;
|
|
46
|
+
return (_b = (_a = nameFormatter().formatToParts(amount).find(isPartCurrency)) == null ? void 0 : _a.value) != null ? _b : money.currencyCode;
|
|
47
|
+
},
|
|
48
|
+
currencySymbol: () => {
|
|
49
|
+
var _a, _b;
|
|
50
|
+
return (_b = (_a = defaultFormatter().formatToParts(amount).find(isPartCurrency)) == null ? void 0 : _a.value) != null ? _b : money.currencyCode;
|
|
51
|
+
},
|
|
52
|
+
currencyNarrowSymbol: () => {
|
|
53
|
+
var _a, _b;
|
|
54
|
+
return (_b = (_a = narrowSymbolFormatter().formatToParts(amount).find(isPartCurrency)) == null ? void 0 : _a.value) != null ? _b : "";
|
|
55
|
+
},
|
|
56
|
+
amount: () => defaultFormatter().formatToParts(amount).filter((part) => ["decimal", "fraction", "group", "integer", "literal"].includes(part.type)).map((part) => part.value).join("")
|
|
57
|
+
}), [money, amount, nameFormatter, defaultFormatter, narrowSymbolFormatter, withoutCurrencyFormatter, withoutTrailingZerosFormatter, withoutTrailingZerosOrCurrencyFormatter]);
|
|
58
|
+
return react.useMemo(() => new Proxy(lazyFormatters, {
|
|
59
|
+
get: (target, key) => {
|
|
60
|
+
var _a;
|
|
61
|
+
return (_a = Reflect.get(target, key)) == null ? void 0 : _a.call(null);
|
|
62
|
+
}
|
|
63
|
+
}), [lazyFormatters]);
|
|
64
|
+
}
|
|
65
|
+
function useLazyFormatter(locale, options) {
|
|
66
|
+
return react.useMemo(() => {
|
|
67
|
+
let memoized;
|
|
68
|
+
return () => memoized != null ? memoized : memoized = new Intl.NumberFormat(locale, options);
|
|
69
|
+
}, [locale, options]);
|
|
70
|
+
}
|
|
71
|
+
exports.useMoney = useMoney;
|
|
72
|
+
//# sourceMappingURL=useMoney.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useMoney.cjs","sources":["../../src/useMoney.tsx"],"sourcesContent":["import {useMemo} from 'react';\nimport {useShop} from './ShopifyProvider.js';\nimport {CurrencyCode, MoneyV2} from './storefront-api-types.js';\n\nexport type UseMoneyValue = {\n /**\n * The currency code from the `MoneyV2` object.\n */\n currencyCode: CurrencyCode;\n /**\n * The name for the currency code, returned by `Intl.NumberFormat`.\n */\n currencyName?: string;\n /**\n * The currency symbol returned by `Intl.NumberFormat`.\n */\n currencySymbol?: string;\n /**\n * The currency narrow symbol returned by `Intl.NumberFormat`.\n */\n currencyNarrowSymbol?: string;\n /**\n * The localized amount, without any currency symbols or non-number types from the `Intl.NumberFormat.formatToParts` parts.\n */\n amount: string;\n /**\n * All parts returned by `Intl.NumberFormat.formatToParts`.\n */\n parts: Intl.NumberFormatPart[];\n /**\n * A string returned by `new Intl.NumberFormat` for the amount and currency code,\n * using the `locale` value in the [`LocalizationProvider` component](https://shopify.dev/api/hydrogen/components/localization/localizationprovider).\n */\n localizedString: string;\n /**\n * The `MoneyV2` object provided as an argument to the hook.\n */\n original: MoneyV2;\n /**\n * A string with trailing zeros removed from the fractional part, if any exist. If there are no trailing zeros, then the fractional part remains.\n * For example, `$640.00` turns into `$640`.\n * `$640.42` remains `$640.42`.\n */\n withoutTrailingZeros: string;\n /**\n * A string without currency and without trailing zeros removed from the fractional part, if any exist. If there are no trailing zeros, then the fractional part remains.\n * For example, `$640.00` turns into `640`.\n * `$640.42` turns into `640.42`.\n */\n withoutTrailingZerosAndCurrency: string;\n};\n\n/**\n * The `useMoney` hook takes a [MoneyV2 object](https://shopify.dev/api/storefront/reference/common-objects/moneyv2) and returns a\n * default-formatted string of the amount with the correct currency indicator, along with some of the parts provided by\n * [Intl.NumberFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat).\n * Uses `locale` from `ShopifyProvider`\n */\nexport function useMoney(money: MoneyV2): UseMoneyValue {\n const {locale} = useShop();\n\n if (!locale) {\n throw new Error(\n `useMoney(): Unable to get 'locale' from 'useShop()', which means that 'locale' was not passed to '<ShopifyProvider/>'. 'locale' is required for 'useMoney()' to work`\n );\n }\n\n const amount = parseFloat(money.amount);\n\n const options = useMemo(\n () => ({\n style: 'currency',\n currency: money.currencyCode,\n }),\n [money.currencyCode]\n );\n\n const defaultFormatter = useLazyFormatter(locale, options);\n\n const nameFormatter = useLazyFormatter(locale, {\n ...options,\n currencyDisplay: 'name',\n });\n\n const narrowSymbolFormatter = useLazyFormatter(locale, {\n ...options,\n currencyDisplay: 'narrowSymbol',\n });\n\n const withoutTrailingZerosFormatter = useLazyFormatter(locale, {\n ...options,\n minimumFractionDigits: 0,\n maximumFractionDigits: 0,\n });\n\n const withoutCurrencyFormatter = useLazyFormatter(locale);\n\n const withoutTrailingZerosOrCurrencyFormatter = useLazyFormatter(locale, {\n minimumFractionDigits: 0,\n maximumFractionDigits: 0,\n });\n\n const isPartCurrency = (part: Intl.NumberFormatPart) =>\n part.type === 'currency';\n\n // By wrapping these properties in functions, we only\n // create formatters if they are going to be used.\n const lazyFormatters = useMemo(\n () => ({\n original: () => money,\n currencyCode: () => money.currencyCode,\n\n localizedString: () => defaultFormatter().format(amount),\n\n parts: () => defaultFormatter().formatToParts(amount),\n\n withoutTrailingZeros: () =>\n amount % 1 === 0\n ? withoutTrailingZerosFormatter().format(amount)\n : defaultFormatter().format(amount),\n\n withoutTrailingZerosAndCurrency: () =>\n amount % 1 === 0\n ? withoutTrailingZerosOrCurrencyFormatter().format(amount)\n : withoutCurrencyFormatter().format(amount),\n\n currencyName: () =>\n nameFormatter().formatToParts(amount).find(isPartCurrency)?.value ??\n money.currencyCode, // e.g. \"US dollars\"\n\n currencySymbol: () =>\n defaultFormatter().formatToParts(amount).find(isPartCurrency)?.value ??\n money.currencyCode, // e.g. \"USD\"\n\n currencyNarrowSymbol: () =>\n narrowSymbolFormatter().formatToParts(amount).find(isPartCurrency)\n ?.value ?? '', // e.g. \"$\"\n\n amount: () =>\n defaultFormatter()\n .formatToParts(amount)\n .filter((part) =>\n ['decimal', 'fraction', 'group', 'integer', 'literal'].includes(\n part.type\n )\n )\n .map((part) => part.value)\n .join(''),\n }),\n [\n money,\n amount,\n nameFormatter,\n defaultFormatter,\n narrowSymbolFormatter,\n withoutCurrencyFormatter,\n withoutTrailingZerosFormatter,\n withoutTrailingZerosOrCurrencyFormatter,\n ]\n );\n\n // Call functions automatically when the properties are accessed\n // to keep these functions as an implementation detail.\n return useMemo(\n () =>\n new Proxy(lazyFormatters as unknown as UseMoneyValue, {\n get: (target, key) => Reflect.get(target, key)?.call(null),\n }),\n [lazyFormatters]\n );\n}\n\nfunction useLazyFormatter(locale: string, options?: Intl.NumberFormatOptions) {\n return useMemo(() => {\n let memoized: Intl.NumberFormat;\n return () => (memoized ??= new Intl.NumberFormat(locale, options));\n }, [locale, options]);\n}\n"],"names":["useMoney","money","locale","useShop","Error","amount","parseFloat","options","useMemo","style","currency","currencyCode","defaultFormatter","useLazyFormatter","nameFormatter","currencyDisplay","narrowSymbolFormatter","withoutTrailingZerosFormatter","minimumFractionDigits","maximumFractionDigits","withoutCurrencyFormatter","withoutTrailingZerosOrCurrencyFormatter","isPartCurrency","part","type","lazyFormatters","original","localizedString","format","parts","formatToParts","withoutTrailingZeros","withoutTrailingZerosAndCurrency","currencyName","find","value","currencySymbol","currencyNarrowSymbol","filter","includes","map","join","Proxy","get","target","key","Reflect","call","memoized","Intl","NumberFormat"],"mappings":";;;;AA0DO,SAASA,SAASC,OAA+B;AAChD,QAAA;AAAA,IAACC;AAAAA,MAAUC,gBAAjB,QAAA;AAEA,MAAI,CAACD,QAAQ;AACL,UAAA,IAAIE,MACP,sKADG;AAAA,EAGP;AAEKC,QAAAA,SAASC,WAAWL,MAAMI,MAAP;AAEnBE,QAAAA,UAAUC,MAAAA,QACd,OAAO;AAAA,IACLC,OAAO;AAAA,IACPC,UAAUT,MAAMU;AAAAA,EAElB,IAAA,CAACV,MAAMU,YAAP,CALqB;AAQjBC,QAAAA,mBAAmBC,iBAAiBX,QAAQK,OAAT;AAEnCO,QAAAA,gBAAgBD,iBAAiBX,QAAQ;AAAA,IAC7C,GAAGK;AAAAA,IACHQ,iBAAiB;AAAA,EAAA,CAFmB;AAKhCC,QAAAA,wBAAwBH,iBAAiBX,QAAQ;AAAA,IACrD,GAAGK;AAAAA,IACHQ,iBAAiB;AAAA,EAAA,CAF2B;AAKxCE,QAAAA,gCAAgCJ,iBAAiBX,QAAQ;AAAA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,SAA5C,EAAuDgB,SACrDhB,KAAKC,IADP,CAHJ,EAOGgB,IAAKjB,CAAAA,SAASA,KAAKY,KAPtB,EAQGM,KAAK,EARR;AAAA,EA/BG,IAyCP,CACExC,OACAI,QACAS,eACAF,kBACAI,uBACAI,0BACAH,+BACAI,uCARF,CA1C4B;AAwD9B,SAAOb,cACL,MACE,IAAIkC,MAAMjB,gBAA4C;AAAA,IACpDkB,KAAK,CAACC,QAAQC;;AAAQC,2BAAQH,IAAIC,QAAQC,GAApB,MAAAC,mBAA0BC,KAAK;AAAA;AAAA,EAA/B,CADxB,GAGF,CAACtB,cAAD,CALY;AAOf;AAED,SAASZ,iBAAiBX,QAAgBK,SAAoC;AAC5E,SAAOC,cAAQ,MAAM;AACfwC,QAAAA;AACJ,WAAO,MAAOA,yCAAa,IAAIC,KAAKC,aAAahD,QAAQK,OAA9B;AAAA,EAAA,GAC1B,CAACL,QAAQK,OAAT,CAHW;AAIf;;"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { useMemo } from "react";
|
|
2
|
+
import { useShop } from "./ShopifyProvider.js";
|
|
3
|
+
function useMoney(money) {
|
|
4
|
+
const {
|
|
5
|
+
locale
|
|
6
|
+
} = useShop();
|
|
7
|
+
if (!locale) {
|
|
8
|
+
throw new Error(`useMoney(): Unable to get 'locale' from 'useShop()', which means that 'locale' was not passed to '<ShopifyProvider/>'. 'locale' is required for 'useMoney()' to work`);
|
|
9
|
+
}
|
|
10
|
+
const amount = parseFloat(money.amount);
|
|
11
|
+
const options = useMemo(() => ({
|
|
12
|
+
style: "currency",
|
|
13
|
+
currency: money.currencyCode
|
|
14
|
+
}), [money.currencyCode]);
|
|
15
|
+
const defaultFormatter = useLazyFormatter(locale, options);
|
|
16
|
+
const nameFormatter = useLazyFormatter(locale, {
|
|
17
|
+
...options,
|
|
18
|
+
currencyDisplay: "name"
|
|
19
|
+
});
|
|
20
|
+
const narrowSymbolFormatter = useLazyFormatter(locale, {
|
|
21
|
+
...options,
|
|
22
|
+
currencyDisplay: "narrowSymbol"
|
|
23
|
+
});
|
|
24
|
+
const withoutTrailingZerosFormatter = useLazyFormatter(locale, {
|
|
25
|
+
...options,
|
|
26
|
+
minimumFractionDigits: 0,
|
|
27
|
+
maximumFractionDigits: 0
|
|
28
|
+
});
|
|
29
|
+
const withoutCurrencyFormatter = useLazyFormatter(locale);
|
|
30
|
+
const withoutTrailingZerosOrCurrencyFormatter = useLazyFormatter(locale, {
|
|
31
|
+
minimumFractionDigits: 0,
|
|
32
|
+
maximumFractionDigits: 0
|
|
33
|
+
});
|
|
34
|
+
const isPartCurrency = (part) => part.type === "currency";
|
|
35
|
+
const lazyFormatters = useMemo(() => ({
|
|
36
|
+
original: () => money,
|
|
37
|
+
currencyCode: () => money.currencyCode,
|
|
38
|
+
localizedString: () => defaultFormatter().format(amount),
|
|
39
|
+
parts: () => defaultFormatter().formatToParts(amount),
|
|
40
|
+
withoutTrailingZeros: () => amount % 1 === 0 ? withoutTrailingZerosFormatter().format(amount) : defaultFormatter().format(amount),
|
|
41
|
+
withoutTrailingZerosAndCurrency: () => amount % 1 === 0 ? withoutTrailingZerosOrCurrencyFormatter().format(amount) : withoutCurrencyFormatter().format(amount),
|
|
42
|
+
currencyName: () => {
|
|
43
|
+
var _a, _b;
|
|
44
|
+
return (_b = (_a = nameFormatter().formatToParts(amount).find(isPartCurrency)) == null ? void 0 : _a.value) != null ? _b : money.currencyCode;
|
|
45
|
+
},
|
|
46
|
+
currencySymbol: () => {
|
|
47
|
+
var _a, _b;
|
|
48
|
+
return (_b = (_a = defaultFormatter().formatToParts(amount).find(isPartCurrency)) == null ? void 0 : _a.value) != null ? _b : money.currencyCode;
|
|
49
|
+
},
|
|
50
|
+
currencyNarrowSymbol: () => {
|
|
51
|
+
var _a, _b;
|
|
52
|
+
return (_b = (_a = narrowSymbolFormatter().formatToParts(amount).find(isPartCurrency)) == null ? void 0 : _a.value) != null ? _b : "";
|
|
53
|
+
},
|
|
54
|
+
amount: () => defaultFormatter().formatToParts(amount).filter((part) => ["decimal", "fraction", "group", "integer", "literal"].includes(part.type)).map((part) => part.value).join("")
|
|
55
|
+
}), [money, amount, nameFormatter, defaultFormatter, narrowSymbolFormatter, withoutCurrencyFormatter, withoutTrailingZerosFormatter, withoutTrailingZerosOrCurrencyFormatter]);
|
|
56
|
+
return useMemo(() => new Proxy(lazyFormatters, {
|
|
57
|
+
get: (target, key) => {
|
|
58
|
+
var _a;
|
|
59
|
+
return (_a = Reflect.get(target, key)) == null ? void 0 : _a.call(null);
|
|
60
|
+
}
|
|
61
|
+
}), [lazyFormatters]);
|
|
62
|
+
}
|
|
63
|
+
function useLazyFormatter(locale, options) {
|
|
64
|
+
return useMemo(() => {
|
|
65
|
+
let memoized;
|
|
66
|
+
return () => memoized != null ? memoized : memoized = new Intl.NumberFormat(locale, options);
|
|
67
|
+
}, [locale, options]);
|
|
68
|
+
}
|
|
69
|
+
export {
|
|
70
|
+
useMoney
|
|
71
|
+
};
|
|
72
|
+
//# sourceMappingURL=useMoney.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useMoney.js","sources":["../../src/useMoney.tsx"],"sourcesContent":["import {useMemo} from 'react';\nimport {useShop} from './ShopifyProvider.js';\nimport {CurrencyCode, MoneyV2} from './storefront-api-types.js';\n\nexport type UseMoneyValue = {\n /**\n * The currency code from the `MoneyV2` object.\n */\n currencyCode: CurrencyCode;\n /**\n * The name for the currency code, returned by `Intl.NumberFormat`.\n */\n currencyName?: string;\n /**\n * The currency symbol returned by `Intl.NumberFormat`.\n */\n currencySymbol?: string;\n /**\n * The currency narrow symbol returned by `Intl.NumberFormat`.\n */\n currencyNarrowSymbol?: string;\n /**\n * The localized amount, without any currency symbols or non-number types from the `Intl.NumberFormat.formatToParts` parts.\n */\n amount: string;\n /**\n * All parts returned by `Intl.NumberFormat.formatToParts`.\n */\n parts: Intl.NumberFormatPart[];\n /**\n * A string returned by `new Intl.NumberFormat` for the amount and currency code,\n * using the `locale` value in the [`LocalizationProvider` component](https://shopify.dev/api/hydrogen/components/localization/localizationprovider).\n */\n localizedString: string;\n /**\n * The `MoneyV2` object provided as an argument to the hook.\n */\n original: MoneyV2;\n /**\n * A string with trailing zeros removed from the fractional part, if any exist. If there are no trailing zeros, then the fractional part remains.\n * For example, `$640.00` turns into `$640`.\n * `$640.42` remains `$640.42`.\n */\n withoutTrailingZeros: string;\n /**\n * A string without currency and without trailing zeros removed from the fractional part, if any exist. If there are no trailing zeros, then the fractional part remains.\n * For example, `$640.00` turns into `640`.\n * `$640.42` turns into `640.42`.\n */\n withoutTrailingZerosAndCurrency: string;\n};\n\n/**\n * The `useMoney` hook takes a [MoneyV2 object](https://shopify.dev/api/storefront/reference/common-objects/moneyv2) and returns a\n * default-formatted string of the amount with the correct currency indicator, along with some of the parts provided by\n * [Intl.NumberFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat).\n * Uses `locale` from `ShopifyProvider`\n */\nexport function useMoney(money: MoneyV2): UseMoneyValue {\n const {locale} = useShop();\n\n if (!locale) {\n throw new Error(\n `useMoney(): Unable to get 'locale' from 'useShop()', which means that 'locale' was not passed to '<ShopifyProvider/>'. 'locale' is required for 'useMoney()' to work`\n );\n }\n\n const amount = parseFloat(money.amount);\n\n const options = useMemo(\n () => ({\n style: 'currency',\n currency: money.currencyCode,\n }),\n [money.currencyCode]\n );\n\n const defaultFormatter = useLazyFormatter(locale, options);\n\n const nameFormatter = useLazyFormatter(locale, {\n ...options,\n currencyDisplay: 'name',\n });\n\n const narrowSymbolFormatter = useLazyFormatter(locale, {\n ...options,\n currencyDisplay: 'narrowSymbol',\n });\n\n const withoutTrailingZerosFormatter = useLazyFormatter(locale, {\n ...options,\n minimumFractionDigits: 0,\n maximumFractionDigits: 0,\n });\n\n const withoutCurrencyFormatter = useLazyFormatter(locale);\n\n const withoutTrailingZerosOrCurrencyFormatter = useLazyFormatter(locale, {\n minimumFractionDigits: 0,\n maximumFractionDigits: 0,\n });\n\n const isPartCurrency = (part: Intl.NumberFormatPart) =>\n part.type === 'currency';\n\n // By wrapping these properties in functions, we only\n // create formatters if they are going to be used.\n const lazyFormatters = useMemo(\n () => ({\n original: () => money,\n currencyCode: () => money.currencyCode,\n\n localizedString: () => defaultFormatter().format(amount),\n\n parts: () => defaultFormatter().formatToParts(amount),\n\n withoutTrailingZeros: () =>\n amount % 1 === 0\n ? withoutTrailingZerosFormatter().format(amount)\n : defaultFormatter().format(amount),\n\n withoutTrailingZerosAndCurrency: () =>\n amount % 1 === 0\n ? withoutTrailingZerosOrCurrencyFormatter().format(amount)\n : withoutCurrencyFormatter().format(amount),\n\n currencyName: () =>\n nameFormatter().formatToParts(amount).find(isPartCurrency)?.value ??\n money.currencyCode, // e.g. \"US dollars\"\n\n currencySymbol: () =>\n defaultFormatter().formatToParts(amount).find(isPartCurrency)?.value ??\n money.currencyCode, // e.g. \"USD\"\n\n currencyNarrowSymbol: () =>\n narrowSymbolFormatter().formatToParts(amount).find(isPartCurrency)\n ?.value ?? '', // e.g. \"$\"\n\n amount: () =>\n defaultFormatter()\n .formatToParts(amount)\n .filter((part) =>\n ['decimal', 'fraction', 'group', 'integer', 'literal'].includes(\n part.type\n )\n )\n .map((part) => part.value)\n .join(''),\n }),\n [\n money,\n amount,\n nameFormatter,\n defaultFormatter,\n narrowSymbolFormatter,\n withoutCurrencyFormatter,\n withoutTrailingZerosFormatter,\n withoutTrailingZerosOrCurrencyFormatter,\n ]\n );\n\n // Call functions automatically when the properties are accessed\n // to keep these functions as an implementation detail.\n return useMemo(\n () =>\n new Proxy(lazyFormatters as unknown as UseMoneyValue, {\n get: (target, key) => Reflect.get(target, key)?.call(null),\n }),\n [lazyFormatters]\n );\n}\n\nfunction useLazyFormatter(locale: string, options?: Intl.NumberFormatOptions) {\n return useMemo(() => {\n let memoized: Intl.NumberFormat;\n return () => (memoized ??= new Intl.NumberFormat(locale, options));\n }, [locale, options]);\n}\n"],"names":["useMoney","money","locale","useShop","Error","amount","parseFloat","options","useMemo","style","currency","currencyCode","defaultFormatter","useLazyFormatter","nameFormatter","currencyDisplay","narrowSymbolFormatter","withoutTrailingZerosFormatter","minimumFractionDigits","maximumFractionDigits","withoutCurrencyFormatter","withoutTrailingZerosOrCurrencyFormatter","isPartCurrency","part","type","lazyFormatters","original","localizedString","format","parts","formatToParts","withoutTrailingZeros","withoutTrailingZerosAndCurrency","currencyName","find","value","currencySymbol","currencyNarrowSymbol","filter","includes","map","join","Proxy","get","target","key","Reflect","call","memoized","Intl","NumberFormat"],"mappings":";;AA0DO,SAASA,SAASC,OAA+B;AAChD,QAAA;AAAA,IAACC;AAAAA,MAAUC,QAAjB;AAEA,MAAI,CAACD,QAAQ;AACL,UAAA,IAAIE,MACP,sKADG;AAAA,EAGP;AAEKC,QAAAA,SAASC,WAAWL,MAAMI,MAAP;AAEnBE,QAAAA,UAAUC,QACd,OAAO;AAAA,IACLC,OAAO;AAAA,IACPC,UAAUT,MAAMU;AAAAA,EAElB,IAAA,CAACV,MAAMU,YAAP,CALqB;AAQjBC,QAAAA,mBAAmBC,iBAAiBX,QAAQK,OAAT;AAEnCO,QAAAA,gBAAgBD,iBAAiBX,QAAQ;AAAA,IAC7C,GAAGK;AAAAA,IACHQ,iBAAiB;AAAA,EAAA,CAFmB;AAKhCC,QAAAA,wBAAwBH,iBAAiBX,QAAQ;AAAA,IACrD,GAAGK;AAAAA,IACHQ,iBAAiB;AAAA,EAAA,CAF2B;AAKxCE,QAAAA,gCAAgCJ,iBAAiBX,QAAQ;AAAA,IAC7D,GAAGK;AAAAA,IACHW,uBAAuB;AAAA,IACvBC,uBAAuB;AAAA,EAAA,CAH6B;AAMhDC,QAAAA,2BAA2BP,iBAAiBX,MAAD;AAE3CmB,QAAAA,0CAA0CR,iBAAiBX,QAAQ;AAAA,IACvEgB,uBAAuB;AAAA,IACvBC,uBAAuB;AAAA,EAAA,CAFuC;AAKhE,QAAMG,iBAAiB,CAACC,SACtBA,KAAKC,S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C;;AAAQC,2BAAQH,IAAIC,QAAQC,GAApB,MAAAC,mBAA0BC,KAAK;AAAA;AAAA,EAA/B,CADxB,GAGF,CAACtB,cAAD,CALY;AAOf;AAED,SAASZ,iBAAiBX,QAAgBK,SAAoC;AAC5E,SAAOC,QAAQ,MAAM;AACfwC,QAAAA;AACJ,WAAO,MAAOA,yCAAa,IAAIC,KAAKC,aAAahD,QAAQK,OAA9B;AAAA,EAAA,GAC1B,CAACL,QAAQK,OAAT,CAHW;AAIf;"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
3
|
+
const jsxRuntime = require("react/jsx-runtime");
|
|
4
|
+
function ExternalVideo(props) {
|
|
5
|
+
var _a, _b;
|
|
6
|
+
const {
|
|
7
|
+
data,
|
|
8
|
+
options,
|
|
9
|
+
id = data.id,
|
|
10
|
+
frameBorder = "0",
|
|
11
|
+
allow = "accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture",
|
|
12
|
+
allowFullScreen = true,
|
|
13
|
+
loading = "lazy",
|
|
14
|
+
...passthroughProps
|
|
15
|
+
} = props;
|
|
16
|
+
if (!data.embedUrl) {
|
|
17
|
+
throw new Error(`<ExternalVideo/> requires the 'embedUrl' property`);
|
|
18
|
+
}
|
|
19
|
+
let finalUrl = data.embedUrl;
|
|
20
|
+
if (options) {
|
|
21
|
+
const urlObject = new URL(data.embedUrl);
|
|
22
|
+
for (const key of Object.keys(options)) {
|
|
23
|
+
urlObject.searchParams.set(key, options[key]);
|
|
24
|
+
}
|
|
25
|
+
finalUrl = urlObject.toString();
|
|
26
|
+
}
|
|
27
|
+
return /* @__PURE__ */ jsxRuntime.jsx("iframe", {
|
|
28
|
+
...passthroughProps,
|
|
29
|
+
id: id != null ? id : data.embedUrl,
|
|
30
|
+
title: (_b = (_a = data.alt) != null ? _a : data.id) != null ? _b : "external video",
|
|
31
|
+
frameBorder,
|
|
32
|
+
allow,
|
|
33
|
+
allowFullScreen,
|
|
34
|
+
src: finalUrl,
|
|
35
|
+
loading
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
exports.ExternalVideo = ExternalVideo;
|
|
39
|
+
//# sourceMappingURL=ExternalVideo.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ExternalVideo.cjs","sources":["../../src/ExternalVideo.tsx"],"sourcesContent":["import type {ExternalVideo as ExternalVideoType} from './storefront-api-types.js';\nimport type {PartialDeep} from 'type-fest';\n\ninterface BaseProps {\n /**\n * An object with fields that correspond to the Storefront API's [ExternalVideo object](https://shopify.dev/api/storefront/reference/products/externalvideo).\n */\n data: PartialDeep<ExternalVideoType, {recurseIntoArrays: true}>;\n /** An object containing the options available for either\n * [YouTube](https://developers.google.com/youtube/player_parameters#Parameters) or\n * [Vimeo](https://vimeo.zendesk.com/hc/en-us/articles/360001494447-Using-Player-Parameters).\n */\n options?: YouTube | Vimeo;\n}\n\ntype PropsWeControl = 'src';\n\nexport type ExternalVideoProps = Omit<\n JSX.IntrinsicElements['iframe'],\n PropsWeControl\n> &\n BaseProps;\n\n/**\n * The `ExternalVideo` component renders an embedded video for the Storefront\n * API's [ExternalVideo object](https://shopify.dev/api/storefront/reference/products/externalvideo).\n */\nexport function ExternalVideo(props: ExternalVideoProps) {\n const {\n data,\n options,\n id = data.id,\n frameBorder = '0',\n allow = 'accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture',\n allowFullScreen = true,\n loading = 'lazy',\n ...passthroughProps\n } = props;\n\n if (!data.embedUrl) {\n throw new Error(`<ExternalVideo/> requires the 'embedUrl' property`);\n }\n\n let finalUrl: string = data.embedUrl;\n\n if (options) {\n const urlObject = new URL(data.embedUrl);\n for (const key of Object.keys(options)) {\n // @ts-expect-error https://github.com/microsoft/TypeScript/issues/32951\n urlObject.searchParams.set(key, options[key]);\n }\n finalUrl = urlObject.toString();\n }\n\n return (\n <iframe\n {...passthroughProps}\n id={id ?? data.embedUrl}\n title={data.alt ?? data.id ?? 'external video'}\n frameBorder={frameBorder}\n allow={allow}\n allowFullScreen={allowFullScreen}\n src={finalUrl}\n loading={loading}\n ></iframe>\n );\n}\n\ninterface YouTube {\n autoplay?: 0 | 1;\n cc_lang_pref?: string;\n cc_load_policy?: 1;\n color?: 'red' | 'white';\n controls?: 0 | 1;\n disablekb?: 0 | 1;\n enablejsapi?: 0 | 1;\n end?: number;\n fs?: 0 | 1;\n hl?: string;\n iv_load_policy?: 1 | 3;\n list?: string;\n list_type?: 'playlist' | 'user_uploads';\n loop?: 0 | 1;\n modest_branding?: 1;\n origin?: string;\n playlist?: string;\n plays_inline?: 0 | 1;\n rel?: 0 | 1;\n start?: number;\n widget_referrer?: string;\n}\n\ntype VimeoBoolean = 0 | 1 | boolean;\n\ninterface Vimeo {\n autopause?: VimeoBoolean;\n autoplay?: VimeoBoolean;\n background?: VimeoBoolean;\n byline?: VimeoBoolean;\n color?: string;\n controls?: VimeoBoolean;\n dnt?: VimeoBoolean;\n loop?: VimeoBoolean;\n muted?: VimeoBoolean;\n pip?: VimeoBoolean;\n playsinline?: VimeoBoolean;\n portrait?: VimeoBoolean;\n quality?: '240p' | '360p' | '540p' | '720p' | '1080p' | '2k' | '4k';\n speed?: VimeoBoolean;\n '#t'?: string;\n texttrack?: string;\n title?: VimeoBoolean;\n transparent?: VimeoBoolean;\n}\n"],"names":["ExternalVideo","props","data","options","id","frameBorder","allow","allowFullScreen","loading","passthroughProps","embedUrl","Error","finalUrl","urlObject","URL","key","Object","keys","searchParams","set","toString","alt"],"mappings":";;;AA2BO,SAASA,cAAcC,OAA2B;;AACjD,QAAA;AAAA,IACJC;AAAAA,IACAC;AAAAA,IACAC,KAAKF,KAAKE;AAAAA,IACVC,cAAc;AAAA,IACdC,QAAQ;AAAA,IACRC,kBAAkB;AAAA,IAClBC,UAAU;AAAA,OACPC;AAAAA,EACDR,IAAAA;AAEA,MAAA,CAACC,KAAKQ,UAAU;AACZ,UAAA,IAAIC,MAAO,mDAAX;AAAA,EACP;AAED,MAAIC,WAAmBV,KAAKQ;AAE5B,MAAIP,SAAS;AACX,UAAMU,YAAY,IAAIC,IAAIZ,KAAKQ,QAAb;AAClB,eAAWK,OAAOC,OAAOC,KAAKd,OAAZ,GAAsB;AAEtCU,gBAAUK,aAAaC,IAAIJ,KAAKZ,QAAQY,IAAxC;AAAA,IACD;AACDH,eAAWC,UAAUO;EACtB;AAED;OAEQX;AAAAA,IACJ,IAAIL,kBAAMF,KAAKQ;AAAAA,IACf,QAAOR,gBAAKmB,QAALnB,YAAYA,KAAKE,OAAjBF,YAAuB;AAAA,IAC9B;AAAA,IACA;AAAA,IACA;AAAA,IACA,KAAKU;AAAAA,IACL;AAAA,EAAA,CATJ;AAYD;;"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { jsx } from "react/jsx-runtime";
|
|
2
|
+
function ExternalVideo(props) {
|
|
3
|
+
var _a, _b;
|
|
4
|
+
const {
|
|
5
|
+
data,
|
|
6
|
+
options,
|
|
7
|
+
id = data.id,
|
|
8
|
+
frameBorder = "0",
|
|
9
|
+
allow = "accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture",
|
|
10
|
+
allowFullScreen = true,
|
|
11
|
+
loading = "lazy",
|
|
12
|
+
...passthroughProps
|
|
13
|
+
} = props;
|
|
14
|
+
if (!data.embedUrl) {
|
|
15
|
+
throw new Error(`<ExternalVideo/> requires the 'embedUrl' property`);
|
|
16
|
+
}
|
|
17
|
+
let finalUrl = data.embedUrl;
|
|
18
|
+
if (options) {
|
|
19
|
+
const urlObject = new URL(data.embedUrl);
|
|
20
|
+
for (const key of Object.keys(options)) {
|
|
21
|
+
urlObject.searchParams.set(key, options[key]);
|
|
22
|
+
}
|
|
23
|
+
finalUrl = urlObject.toString();
|
|
24
|
+
}
|
|
25
|
+
return /* @__PURE__ */ jsx("iframe", {
|
|
26
|
+
...passthroughProps,
|
|
27
|
+
id: id != null ? id : data.embedUrl,
|
|
28
|
+
title: (_b = (_a = data.alt) != null ? _a : data.id) != null ? _b : "external video",
|
|
29
|
+
frameBorder,
|
|
30
|
+
allow,
|
|
31
|
+
allowFullScreen,
|
|
32
|
+
src: finalUrl,
|
|
33
|
+
loading
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
export {
|
|
37
|
+
ExternalVideo
|
|
38
|
+
};
|
|
39
|
+
//# sourceMappingURL=ExternalVideo.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ExternalVideo.js","sources":["../../src/ExternalVideo.tsx"],"sourcesContent":["import type {ExternalVideo as ExternalVideoType} from './storefront-api-types.js';\nimport type {PartialDeep} from 'type-fest';\n\ninterface BaseProps {\n /**\n * An object with fields that correspond to the Storefront API's [ExternalVideo object](https://shopify.dev/api/storefront/reference/products/externalvideo).\n */\n data: PartialDeep<ExternalVideoType, {recurseIntoArrays: true}>;\n /** An object containing the options available for either\n * [YouTube](https://developers.google.com/youtube/player_parameters#Parameters) or\n * [Vimeo](https://vimeo.zendesk.com/hc/en-us/articles/360001494447-Using-Player-Parameters).\n */\n options?: YouTube | Vimeo;\n}\n\ntype PropsWeControl = 'src';\n\nexport type ExternalVideoProps = Omit<\n JSX.IntrinsicElements['iframe'],\n PropsWeControl\n> &\n BaseProps;\n\n/**\n * The `ExternalVideo` component renders an embedded video for the Storefront\n * API's [ExternalVideo object](https://shopify.dev/api/storefront/reference/products/externalvideo).\n */\nexport function ExternalVideo(props: ExternalVideoProps) {\n const {\n data,\n options,\n id = data.id,\n frameBorder = '0',\n allow = 'accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture',\n allowFullScreen = true,\n loading = 'lazy',\n ...passthroughProps\n } = props;\n\n if (!data.embedUrl) {\n throw new Error(`<ExternalVideo/> requires the 'embedUrl' property`);\n }\n\n let finalUrl: string = data.embedUrl;\n\n if (options) {\n const urlObject = new URL(data.embedUrl);\n for (const key of Object.keys(options)) {\n // @ts-expect-error https://github.com/microsoft/TypeScript/issues/32951\n urlObject.searchParams.set(key, options[key]);\n }\n finalUrl = urlObject.toString();\n }\n\n return (\n <iframe\n {...passthroughProps}\n id={id ?? data.embedUrl}\n title={data.alt ?? data.id ?? 'external video'}\n frameBorder={frameBorder}\n allow={allow}\n allowFullScreen={allowFullScreen}\n src={finalUrl}\n loading={loading}\n ></iframe>\n );\n}\n\ninterface YouTube {\n autoplay?: 0 | 1;\n cc_lang_pref?: string;\n cc_load_policy?: 1;\n color?: 'red' | 'white';\n controls?: 0 | 1;\n disablekb?: 0 | 1;\n enablejsapi?: 0 | 1;\n end?: number;\n fs?: 0 | 1;\n hl?: string;\n iv_load_policy?: 1 | 3;\n list?: string;\n list_type?: 'playlist' | 'user_uploads';\n loop?: 0 | 1;\n modest_branding?: 1;\n origin?: string;\n playlist?: string;\n plays_inline?: 0 | 1;\n rel?: 0 | 1;\n start?: number;\n widget_referrer?: string;\n}\n\ntype VimeoBoolean = 0 | 1 | boolean;\n\ninterface Vimeo {\n autopause?: VimeoBoolean;\n autoplay?: VimeoBoolean;\n background?: VimeoBoolean;\n byline?: VimeoBoolean;\n color?: string;\n controls?: VimeoBoolean;\n dnt?: VimeoBoolean;\n loop?: VimeoBoolean;\n muted?: VimeoBoolean;\n pip?: VimeoBoolean;\n playsinline?: VimeoBoolean;\n portrait?: VimeoBoolean;\n quality?: '240p' | '360p' | '540p' | '720p' | '1080p' | '2k' | '4k';\n speed?: VimeoBoolean;\n '#t'?: string;\n texttrack?: string;\n title?: VimeoBoolean;\n transparent?: VimeoBoolean;\n}\n"],"names":["ExternalVideo","props","data","options","id","frameBorder","allow","allowFullScreen","loading","passthroughProps","embedUrl","Error","finalUrl","urlObject","URL","key","Object","keys","searchParams","set","toString","alt"],"mappings":";AA2BO,SAASA,cAAcC,OAA2B;;AACjD,QAAA;AAAA,IACJC;AAAAA,IACAC;AAAAA,IACAC,KAAKF,KAAKE;AAAAA,IACVC,cAAc;AAAA,IACdC,QAAQ;AAAA,IACRC,kBAAkB;AAAA,IAClBC,UAAU;AAAA,OACPC;AAAAA,EACDR,IAAAA;AAEA,MAAA,CAACC,KAAKQ,UAAU;AACZ,UAAA,IAAIC,MAAO,mDAAX;AAAA,EACP;AAED,MAAIC,WAAmBV,KAAKQ;AAE5B,MAAIP,SAAS;AACX,UAAMU,YAAY,IAAIC,IAAIZ,KAAKQ,QAAb;AAClB,eAAWK,OAAOC,OAAOC,KAAKd,OAAZ,GAAsB;AAEtCU,gBAAUK,aAAaC,IAAIJ,KAAKZ,QAAQY,IAAxC;AAAA,IACD;AACDH,eAAWC,UAAUO;EACtB;AAED;OAEQX;AAAAA,IACJ,IAAIL,kBAAMF,KAAKQ;AAAAA,IACf,QAAOR,gBAAKmB,QAALnB,YAAYA,KAAKE,OAAjBF,YAAuB;AAAA,IAC9B;AAAA,IACA;AAAA,IACA;AAAA,IACA,KAAKU;AAAAA,IACL;AAAA,EAAA,CATJ;AAYD;"}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
3
|
+
const imageSize = require("./image-size.cjs");
|
|
4
|
+
const jsxRuntime = require("react/jsx-runtime");
|
|
5
|
+
function Image({
|
|
6
|
+
data,
|
|
7
|
+
width,
|
|
8
|
+
height,
|
|
9
|
+
loading,
|
|
10
|
+
loader = imageSize.shopifyImageLoader,
|
|
11
|
+
loaderOptions,
|
|
12
|
+
widths,
|
|
13
|
+
decoding = "async",
|
|
14
|
+
...rest
|
|
15
|
+
}) {
|
|
16
|
+
var _a, _b, _c, _d, _e, _f;
|
|
17
|
+
if (!data.url) {
|
|
18
|
+
const missingUrlError = `<Image/>: the 'data' prop requires the 'url' property. Image: ${(_a = data.id) != null ? _a : "no ID provided"}`;
|
|
19
|
+
{
|
|
20
|
+
console.error(missingUrlError);
|
|
21
|
+
}
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
const {
|
|
25
|
+
width: imgElementWidth,
|
|
26
|
+
height: imgElementHeight
|
|
27
|
+
} = imageSize.getShopifyImageDimensions({
|
|
28
|
+
data,
|
|
29
|
+
loaderOptions,
|
|
30
|
+
elementProps: {
|
|
31
|
+
width,
|
|
32
|
+
height
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
let finalSrc = data.url;
|
|
36
|
+
if (loader) {
|
|
37
|
+
finalSrc = loader({
|
|
38
|
+
...loaderOptions,
|
|
39
|
+
src: data.url,
|
|
40
|
+
width: imgElementWidth,
|
|
41
|
+
height: imgElementHeight
|
|
42
|
+
});
|
|
43
|
+
if (typeof finalSrc !== "string" || !finalSrc) {
|
|
44
|
+
throw new Error(`<Image/>: 'loader' did not return a valid string. Image: ${(_b = data.id) != null ? _b : data.url}`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
const maxWidth = width && imgElementWidth && width < imgElementWidth ? width : imgElementWidth;
|
|
48
|
+
const finalSrcset = (_c = rest.srcSet) != null ? _c : internalImageSrcSet({
|
|
49
|
+
...loaderOptions,
|
|
50
|
+
widths,
|
|
51
|
+
src: data.url,
|
|
52
|
+
width: maxWidth,
|
|
53
|
+
height: imgElementHeight,
|
|
54
|
+
loader
|
|
55
|
+
});
|
|
56
|
+
return /* @__PURE__ */ jsxRuntime.jsx("img", {
|
|
57
|
+
id: (_d = data.id) != null ? _d : "",
|
|
58
|
+
alt: (_f = (_e = data.altText) != null ? _e : rest.alt) != null ? _f : "",
|
|
59
|
+
loading: loading != null ? loading : "lazy",
|
|
60
|
+
...rest,
|
|
61
|
+
src: finalSrc,
|
|
62
|
+
width: imgElementWidth != null ? imgElementWidth : void 0,
|
|
63
|
+
height: imgElementHeight != null ? imgElementHeight : void 0,
|
|
64
|
+
srcSet: finalSrcset,
|
|
65
|
+
decoding
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
function internalImageSrcSet({
|
|
69
|
+
src,
|
|
70
|
+
width,
|
|
71
|
+
crop,
|
|
72
|
+
scale,
|
|
73
|
+
widths,
|
|
74
|
+
loader,
|
|
75
|
+
height
|
|
76
|
+
}) {
|
|
77
|
+
const hasCustomWidths = widths && Array.isArray(widths);
|
|
78
|
+
if (hasCustomWidths && widths.some((size) => isNaN(size))) {
|
|
79
|
+
throw new Error(`<Image/>: the 'widths' must be an array of numbers. Image: ${src}`);
|
|
80
|
+
}
|
|
81
|
+
let aspectRatio = 1;
|
|
82
|
+
if (width && height) {
|
|
83
|
+
aspectRatio = Number(height) / Number(width);
|
|
84
|
+
}
|
|
85
|
+
let setSizes = hasCustomWidths ? widths : imageSize.IMG_SRC_SET_SIZES;
|
|
86
|
+
if (!hasCustomWidths && width && width < imageSize.IMG_SRC_SET_SIZES[imageSize.IMG_SRC_SET_SIZES.length - 1]) {
|
|
87
|
+
setSizes = imageSize.IMG_SRC_SET_SIZES.filter((size) => size <= width);
|
|
88
|
+
}
|
|
89
|
+
const srcGenerator = loader ? loader : imageSize.addImageSizeParametersToUrl;
|
|
90
|
+
return setSizes.map((size) => `${srcGenerator({
|
|
91
|
+
src,
|
|
92
|
+
width: size,
|
|
93
|
+
height: crop ? Number(size) * aspectRatio : void 0,
|
|
94
|
+
crop,
|
|
95
|
+
scale
|
|
96
|
+
})} ${size}w`).join(", ");
|
|
97
|
+
}
|
|
98
|
+
exports.Image = Image;
|
|
99
|
+
//# sourceMappingURL=Image.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Image.cjs","sources":["../../src/Image.tsx"],"sourcesContent":["import * as React from 'react';\nimport {\n getShopifyImageDimensions,\n shopifyImageLoader,\n addImageSizeParametersToUrl,\n IMG_SRC_SET_SIZES,\n} from './image-size.js';\nimport type {Image as ImageType} from './storefront-api-types.js';\nimport type {PartialDeep, Simplify} from 'type-fest';\n\ntype HtmlImageProps = React.ImgHTMLAttributes<HTMLImageElement>;\n\nexport type ShopifyLoaderOptions = {\n crop?: 'top' | 'bottom' | 'left' | 'right' | 'center';\n scale?: 2 | 3;\n width?: HtmlImageProps['width'] | ImageType['width'];\n height?: HtmlImageProps['height'] | ImageType['height'];\n};\nexport type ShopifyLoaderParams = Simplify<\n ShopifyLoaderOptions & {\n src: ImageType['url'];\n }\n>;\nexport type ShopifyImageProps = Omit<HtmlImageProps, 'src'> & {\n /** An object with fields that correspond to the Storefront API's\n * [Image object](https://shopify.dev/api/storefront/reference/common-objects/image).\n * The `data` prop is required.\n */\n data: PartialDeep<ImageType, {recurseIntoArrays: true}>;\n /** A custom function that generates the image URL. Parameters passed in\n * are `ShopifyLoaderParams`\n */\n loader?: (params: ShopifyLoaderParams) => string;\n /** An object of `loader` function options. For example, if the `loader` function\n * requires a `scale` option, then the value can be a property of the\n * `loaderOptions` object (for example, `{scale: 2}`). The object shape is `ShopifyLoaderOptions`.\n */\n loaderOptions?: ShopifyLoaderOptions;\n /**\n * `src` isn't used, and should instead be passed as part of the `data` object\n */\n src?: never;\n /**\n * An array of pixel widths to overwrite the default generated srcset. For example, `[300, 600, 800]`.\n */\n widths?: (HtmlImageProps['width'] | ImageType['width'])[];\n};\n\n/**\n * The `Image` component renders an image for the Storefront API's\n * [Image object](https://shopify.dev/api/storefront/reference/common-objects/image) by using the `data` prop. You can [customize this component](https://shopify.dev/api/hydrogen/components#customizing-hydrogen-components) using passthrough props.\n *\n * An image's width and height are determined using the following priority list:\n * 1. The width and height values for the `loaderOptions` prop\n * 2. The width and height values for bare props\n * 3. The width and height values for the `data` prop\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 `data.width` and `data.height` aren't available, then the aspect ratio cannot be determined and the missing\n * value will remain as `null`\n */\nexport function Image({\n data,\n width,\n height,\n loading,\n loader = shopifyImageLoader,\n loaderOptions,\n widths,\n decoding = 'async',\n ...rest\n}: ShopifyImageProps) {\n if (!data.url) {\n const missingUrlError = `<Image/>: the 'data' prop requires the 'url' property. Image: ${\n data.id ?? 'no ID provided'\n }`;\n\n if (__HYDROGEN_DEV__) {\n throw new Error(missingUrlError);\n } else {\n console.error(missingUrlError);\n }\n\n return null;\n }\n\n if (__HYDROGEN_DEV__ && !data.altText && !rest.alt) {\n console.warn(\n `<Image/>: the 'data' prop should have the 'altText' property, or the 'alt' prop, and one of them should not be empty. Image: ${\n data.id ?? data.url\n }`\n );\n }\n\n const {width: imgElementWidth, height: imgElementHeight} =\n getShopifyImageDimensions({\n data,\n loaderOptions,\n elementProps: {\n width,\n height,\n },\n });\n\n if (__HYDROGEN_DEV__ && (!imgElementWidth || !imgElementHeight)) {\n console.warn(\n `<Image/>: the 'data' prop requires either 'width' or 'data.width', and 'height' or 'data.height' properties. Image: ${\n data.id ?? data.url\n }`\n );\n }\n\n let finalSrc = data.url;\n\n if (loader) {\n finalSrc = loader({\n ...loaderOptions,\n src: data.url,\n width: imgElementWidth,\n height: imgElementHeight,\n });\n if (typeof finalSrc !== 'string' || !finalSrc) {\n throw new Error(\n `<Image/>: 'loader' did not return a valid string. Image: ${\n data.id ?? data.url\n }`\n );\n }\n }\n\n // determining what the intended width of the image is. For example, if the width is specified and lower than the image width, then that is the maximum image width\n // to prevent generating a srcset with widths bigger than needed or to generate images that would distort because of being larger than original\n const maxWidth =\n width && imgElementWidth && width < imgElementWidth\n ? width\n : imgElementWidth;\n const finalSrcset =\n rest.srcSet ??\n internalImageSrcSet({\n ...loaderOptions,\n widths,\n src: data.url,\n width: maxWidth,\n height: imgElementHeight,\n loader,\n });\n\n /* eslint-disable hydrogen/prefer-image-component */\n return (\n <img\n id={data.id ?? ''}\n alt={data.altText ?? rest.alt ?? ''}\n loading={loading ?? 'lazy'}\n {...rest}\n src={finalSrc}\n width={imgElementWidth ?? undefined}\n height={imgElementHeight ?? undefined}\n srcSet={finalSrcset}\n decoding={decoding}\n />\n );\n /* eslint-enable hydrogen/prefer-image-component */\n}\n\ntype InternalShopifySrcSetGeneratorsParams = Simplify<\n ShopifyLoaderOptions & {\n src: ImageType['url'];\n widths?: (HtmlImageProps['width'] | ImageType['width'])[];\n loader?: (params: ShopifyLoaderParams) => string;\n }\n>;\nfunction internalImageSrcSet({\n src,\n width,\n crop,\n scale,\n widths,\n loader,\n height,\n}: InternalShopifySrcSetGeneratorsParams) {\n const hasCustomWidths = widths && Array.isArray(widths);\n if (hasCustomWidths && widths.some((size) => isNaN(size as number))) {\n throw new Error(\n `<Image/>: the 'widths' must be an array of numbers. Image: ${src}`\n );\n }\n\n let aspectRatio = 1;\n if (width && height) {\n aspectRatio = Number(height) / Number(width);\n }\n\n let setSizes = hasCustomWidths ? widths : IMG_SRC_SET_SIZES;\n if (\n !hasCustomWidths &&\n width &&\n width < IMG_SRC_SET_SIZES[IMG_SRC_SET_SIZES.length - 1]\n ) {\n setSizes = IMG_SRC_SET_SIZES.filter((size) => size <= width);\n }\n const srcGenerator = loader ? loader : addImageSizeParametersToUrl;\n return setSizes\n .map(\n (size) =>\n `${srcGenerator({\n src,\n width: size,\n // height is not applied if there is no crop\n // if there is crop, then height is applied as a ratio of the original width + height aspect ratio * size\n height: crop ? Number(size) * aspectRatio : undefined,\n crop,\n scale,\n })} ${size}w`\n )\n .join(', ');\n}\n"],"names":["Image","data","width","height","loading","loader","shopifyImageLoader","loaderOptions","widths","decoding","rest","url","missingUrlError","id","console","error","imgElementWidth","imgElementHeight","getShopifyImageDimensions","elementProps","finalSrc","src","Error","maxWidth","finalSrcset","srcSet","internalImageSrcSet","altText","alt","undefined","crop","scale","hasCustomWidths","Array","isArray","some","size","isNaN","aspectRatio","Number","setSizes","IMG_SRC_SET_SIZES","length","filter","srcGenerator","addImageSizeParametersToUrl","map","join"],"mappings":";;;;AA6DO,SAASA,MAAM;AAAA,EACpBC;AAAAA,EACAC;AAAAA,EACAC;AAAAA,EACAC;AAAAA,EACAC,SAASC,UAAAA;AAAAA,EACTC;AAAAA,EACAC;AAAAA,EACAC,WAAW;AAAA,KACRC;AATiB,GAUA;;AAChB,MAAA,CAACT,KAAKU,KAAK;AACPC,UA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;AAAAA,EACAC;AAAAA,EACAvB;AAAAA,EACAH;AAAAA,EACAF;AAP2B,GAQa;AACxC,QAAM6B,kBAAkBxB,UAAUyB,MAAMC,QAAQ1B,MAAd;AAClC,MAAIwB,mBAAmBxB,OAAO2B,KAAMC,UAASC,MAAMD,IAAD,CAA3B,GAA8C;AAC7D,UAAA,IAAId,MACP,8DAA6DD,KAD1D;AAAA,EAGP;AAED,MAAIiB,cAAc;AAClB,MAAIpC,SAASC,QAAQ;AACnBmC,kBAAcC,OAAOpC,MAAD,IAAWoC,OAAOrC,KAAD;AAAA,EACtC;AAEGsC,MAAAA,WAAWR,kBAAkBxB,SAASiC;AAC1C,MACE,CAACT,mBACD9B,SACAA,QAAQuC,UAAkBA,kBAAAA,4BAAkBC,SAAS,IACrD;AACAF,eAAWC,UAAkBE,kBAAAA,OAAQP,CAASA,SAAAA,QAAQlC,KAA3C;AAAA,EACZ;AACK0C,QAAAA,eAAevC,SAASA,SAASwC;AACvC,SAAOL,SACJM,IACEV,CACE,SAAA,GAAEQ,aAAa;AAAA,IACdvB;AAAAA,IACAnB,OAAOkC;AAAAA,IAGPjC,QAAQ2B,OAAOS,OAAOH,IAAD,IAASE,cAAcT;AAAAA,IAC5CC;AAAAA,IACAC;AAAAA,EAAAA,CAPa,KAQTK,OAXL,EAaJW,KAAK,IAbD;AAcR;;"}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { getShopifyImageDimensions, shopifyImageLoader, IMG_SRC_SET_SIZES, addImageSizeParametersToUrl } from "./image-size.js";
|
|
2
|
+
import { jsx } from "react/jsx-runtime";
|
|
3
|
+
function Image({
|
|
4
|
+
data,
|
|
5
|
+
width,
|
|
6
|
+
height,
|
|
7
|
+
loading,
|
|
8
|
+
loader = shopifyImageLoader,
|
|
9
|
+
loaderOptions,
|
|
10
|
+
widths,
|
|
11
|
+
decoding = "async",
|
|
12
|
+
...rest
|
|
13
|
+
}) {
|
|
14
|
+
var _a, _b, _c, _d, _e, _f;
|
|
15
|
+
if (!data.url) {
|
|
16
|
+
const missingUrlError = `<Image/>: the 'data' prop requires the 'url' property. Image: ${(_a = data.id) != null ? _a : "no ID provided"}`;
|
|
17
|
+
{
|
|
18
|
+
console.error(missingUrlError);
|
|
19
|
+
}
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
const {
|
|
23
|
+
width: imgElementWidth,
|
|
24
|
+
height: imgElementHeight
|
|
25
|
+
} = getShopifyImageDimensions({
|
|
26
|
+
data,
|
|
27
|
+
loaderOptions,
|
|
28
|
+
elementProps: {
|
|
29
|
+
width,
|
|
30
|
+
height
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
let finalSrc = data.url;
|
|
34
|
+
if (loader) {
|
|
35
|
+
finalSrc = loader({
|
|
36
|
+
...loaderOptions,
|
|
37
|
+
src: data.url,
|
|
38
|
+
width: imgElementWidth,
|
|
39
|
+
height: imgElementHeight
|
|
40
|
+
});
|
|
41
|
+
if (typeof finalSrc !== "string" || !finalSrc) {
|
|
42
|
+
throw new Error(`<Image/>: 'loader' did not return a valid string. Image: ${(_b = data.id) != null ? _b : data.url}`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
const maxWidth = width && imgElementWidth && width < imgElementWidth ? width : imgElementWidth;
|
|
46
|
+
const finalSrcset = (_c = rest.srcSet) != null ? _c : internalImageSrcSet({
|
|
47
|
+
...loaderOptions,
|
|
48
|
+
widths,
|
|
49
|
+
src: data.url,
|
|
50
|
+
width: maxWidth,
|
|
51
|
+
height: imgElementHeight,
|
|
52
|
+
loader
|
|
53
|
+
});
|
|
54
|
+
return /* @__PURE__ */ jsx("img", {
|
|
55
|
+
id: (_d = data.id) != null ? _d : "",
|
|
56
|
+
alt: (_f = (_e = data.altText) != null ? _e : rest.alt) != null ? _f : "",
|
|
57
|
+
loading: loading != null ? loading : "lazy",
|
|
58
|
+
...rest,
|
|
59
|
+
src: finalSrc,
|
|
60
|
+
width: imgElementWidth != null ? imgElementWidth : void 0,
|
|
61
|
+
height: imgElementHeight != null ? imgElementHeight : void 0,
|
|
62
|
+
srcSet: finalSrcset,
|
|
63
|
+
decoding
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
function internalImageSrcSet({
|
|
67
|
+
src,
|
|
68
|
+
width,
|
|
69
|
+
crop,
|
|
70
|
+
scale,
|
|
71
|
+
widths,
|
|
72
|
+
loader,
|
|
73
|
+
height
|
|
74
|
+
}) {
|
|
75
|
+
const hasCustomWidths = widths && Array.isArray(widths);
|
|
76
|
+
if (hasCustomWidths && widths.some((size) => isNaN(size))) {
|
|
77
|
+
throw new Error(`<Image/>: the 'widths' must be an array of numbers. Image: ${src}`);
|
|
78
|
+
}
|
|
79
|
+
let aspectRatio = 1;
|
|
80
|
+
if (width && height) {
|
|
81
|
+
aspectRatio = Number(height) / Number(width);
|
|
82
|
+
}
|
|
83
|
+
let setSizes = hasCustomWidths ? widths : IMG_SRC_SET_SIZES;
|
|
84
|
+
if (!hasCustomWidths && width && width < IMG_SRC_SET_SIZES[IMG_SRC_SET_SIZES.length - 1]) {
|
|
85
|
+
setSizes = IMG_SRC_SET_SIZES.filter((size) => size <= width);
|
|
86
|
+
}
|
|
87
|
+
const srcGenerator = loader ? loader : addImageSizeParametersToUrl;
|
|
88
|
+
return setSizes.map((size) => `${srcGenerator({
|
|
89
|
+
src,
|
|
90
|
+
width: size,
|
|
91
|
+
height: crop ? Number(size) * aspectRatio : void 0,
|
|
92
|
+
crop,
|
|
93
|
+
scale
|
|
94
|
+
})} ${size}w`).join(", ");
|
|
95
|
+
}
|
|
96
|
+
export {
|
|
97
|
+
Image
|
|
98
|
+
};
|
|
99
|
+
//# sourceMappingURL=Image.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Image.js","sources":["../../src/Image.tsx"],"sourcesContent":["import * as React from 'react';\nimport {\n getShopifyImageDimensions,\n shopifyImageLoader,\n addImageSizeParametersToUrl,\n IMG_SRC_SET_SIZES,\n} from './image-size.js';\nimport type {Image as ImageType} from './storefront-api-types.js';\nimport type {PartialDeep, Simplify} from 'type-fest';\n\ntype HtmlImageProps = React.ImgHTMLAttributes<HTMLImageElement>;\n\nexport type ShopifyLoaderOptions = {\n crop?: 'top' | 'bottom' | 'left' | 'right' | 'center';\n scale?: 2 | 3;\n width?: HtmlImageProps['width'] | ImageType['width'];\n height?: HtmlImageProps['height'] | ImageType['height'];\n};\nexport type ShopifyLoaderParams = Simplify<\n ShopifyLoaderOptions & {\n src: ImageType['url'];\n }\n>;\nexport type ShopifyImageProps = Omit<HtmlImageProps, 'src'> & {\n /** An object with fields that correspond to the Storefront API's\n * [Image object](https://shopify.dev/api/storefront/reference/common-objects/image).\n * The `data` prop is required.\n */\n data: PartialDeep<ImageType, {recurseIntoArrays: true}>;\n /** A custom function that generates the image URL. Parameters passed in\n * are `ShopifyLoaderParams`\n */\n loader?: (params: ShopifyLoaderParams) => string;\n /** An object of `loader` function options. For example, if the `loader` function\n * requires a `scale` option, then the value can be a property of the\n * `loaderOptions` object (for example, `{scale: 2}`). The object shape is `ShopifyLoaderOptions`.\n */\n loaderOptions?: ShopifyLoaderOptions;\n /**\n * `src` isn't used, and should instead be passed as part of the `data` object\n */\n src?: never;\n /**\n * An array of pixel widths to overwrite the default generated srcset. For example, `[300, 600, 800]`.\n */\n widths?: (HtmlImageProps['width'] | ImageType['width'])[];\n};\n\n/**\n * The `Image` component renders an image for the Storefront API's\n * [Image object](https://shopify.dev/api/storefront/reference/common-objects/image) by using the `data` prop. You can [customize this component](https://shopify.dev/api/hydrogen/components#customizing-hydrogen-components) using passthrough props.\n *\n * An image's width and height are determined using the following priority list:\n * 1. The width and height values for the `loaderOptions` prop\n * 2. The width and height values for bare props\n * 3. The width and height values for the `data` prop\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 `data.width` and `data.height` aren't available, then the aspect ratio cannot be determined and the missing\n * value will remain as `null`\n */\nexport function Image({\n data,\n width,\n height,\n loading,\n loader = shopifyImageLoader,\n loaderOptions,\n widths,\n decoding = 'async',\n ...rest\n}: ShopifyImageProps) {\n if (!data.url) {\n const missingUrlError = `<Image/>: the 'data' prop requires the 'url' property. Image: ${\n data.id ?? 'no ID provided'\n }`;\n\n if (__HYDROGEN_DEV__) {\n throw new Error(missingUrlError);\n } else {\n console.error(missingUrlError);\n }\n\n return null;\n }\n\n if (__HYDROGEN_DEV__ && !data.altText && !rest.alt) {\n console.warn(\n `<Image/>: the 'data' prop should have the 'altText' property, or the 'alt' prop, and one of them should not be empty. Image: ${\n data.id ?? data.url\n }`\n );\n }\n\n const {width: imgElementWidth, height: imgElementHeight} =\n getShopifyImageDimensions({\n data,\n loaderOptions,\n elementProps: {\n width,\n height,\n },\n });\n\n if (__HYDROGEN_DEV__ && (!imgElementWidth || !imgElementHeight)) {\n console.warn(\n `<Image/>: the 'data' prop requires either 'width' or 'data.width', and 'height' or 'data.height' properties. Image: ${\n data.id ?? data.url\n }`\n );\n }\n\n let finalSrc = data.url;\n\n if (loader) {\n finalSrc = loader({\n ...loaderOptions,\n src: data.url,\n width: imgElementWidth,\n height: imgElementHeight,\n });\n if (typeof finalSrc !== 'string' || !finalSrc) {\n throw new Error(\n `<Image/>: 'loader' did not return a valid string. Image: ${\n data.id ?? data.url\n }`\n );\n }\n }\n\n // determining what the intended width of the image is. For example, if the width is specified and lower than the image width, then that is the maximum image width\n // to prevent generating a srcset with widths bigger than needed or to generate images that would distort because of being larger than original\n const maxWidth =\n width && imgElementWidth && width < imgElementWidth\n ? width\n : imgElementWidth;\n const finalSrcset =\n rest.srcSet ??\n internalImageSrcSet({\n ...loaderOptions,\n widths,\n src: data.url,\n width: maxWidth,\n height: imgElementHeight,\n loader,\n });\n\n /* eslint-disable hydrogen/prefer-image-component */\n return (\n <img\n id={data.id ?? ''}\n alt={data.altText ?? rest.alt ?? ''}\n loading={loading ?? 'lazy'}\n {...rest}\n src={finalSrc}\n width={imgElementWidth ?? undefined}\n height={imgElementHeight ?? undefined}\n srcSet={finalSrcset}\n decoding={decoding}\n />\n );\n /* eslint-enable hydrogen/prefer-image-component */\n}\n\ntype InternalShopifySrcSetGeneratorsParams = Simplify<\n ShopifyLoaderOptions & {\n src: ImageType['url'];\n widths?: (HtmlImageProps['width'] | ImageType['width'])[];\n loader?: (params: ShopifyLoaderParams) => string;\n }\n>;\nfunction internalImageSrcSet({\n src,\n width,\n crop,\n scale,\n widths,\n loader,\n height,\n}: InternalShopifySrcSetGeneratorsParams) {\n const hasCustomWidths = widths && Array.isArray(widths);\n if (hasCustomWidths && widths.some((size) => isNaN(size as number))) {\n throw new Error(\n `<Image/>: the 'widths' must be an array of numbers. Image: ${src}`\n );\n }\n\n let aspectRatio = 1;\n if (width && height) {\n aspectRatio = Number(height) / Number(width);\n }\n\n let setSizes = hasCustomWidths ? widths : IMG_SRC_SET_SIZES;\n if (\n !hasCustomWidths &&\n width &&\n width < IMG_SRC_SET_SIZES[IMG_SRC_SET_SIZES.length - 1]\n ) {\n setSizes = IMG_SRC_SET_SIZES.filter((size) => size <= width);\n }\n const srcGenerator = loader ? loader : addImageSizeParametersToUrl;\n return setSizes\n .map(\n (size) =>\n `${srcGenerator({\n src,\n width: size,\n // height is not applied if there is no crop\n // if there is crop, then height is applied as a ratio of the original width + height aspect ratio * size\n height: crop ? Number(size) * aspectRatio : undefined,\n crop,\n scale,\n })} ${size}w`\n )\n .join(', ');\n}\n"],"names":["Image","data","width","height","loading","loader","shopifyImageLoader","loaderOptions","widths","decoding","rest","url","missingUrlError","id","console","error","imgElementWidth","imgElementHeight","getShopifyImageDimensions","elementProps","finalSrc","src","Error","maxWidth","finalSrcset","srcSet","internalImageSrcSet","altText","alt","undefined","crop","scale","hasCustomWidths","Array","isArray","some","size","isNaN","aspectRatio","Number","setSizes","IMG_SRC_SET_SIZES","length","filter","srcGenerator","addImageSizeParametersToUrl","map","join"],"mappings":";;AA6DO,SAASA,MAAM;AAAA,EACpBC;AAAAA,E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