@shopify/hydrogen-react 2022.7.0 → 2022.10.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.
Files changed (274) hide show
  1. package/README.md +7 -5
  2. package/dist/dev/CartProvider.cjs +485 -0
  3. package/dist/dev/CartProvider.cjs.map +1 -0
  4. package/dist/dev/CartProvider.js +485 -0
  5. package/dist/dev/CartProvider.js.map +1 -0
  6. package/dist/dev/MediaFile.cjs.map +1 -1
  7. package/dist/dev/MediaFile.js.map +1 -1
  8. package/dist/dev/Metafield.cjs +2 -2
  9. package/dist/dev/Metafield.cjs.map +1 -1
  10. package/dist/dev/Metafield.js.map +1 -1
  11. package/dist/dev/ModelViewer.cjs +4 -4
  12. package/dist/dev/ModelViewer.cjs.map +1 -1
  13. package/dist/dev/Money.cjs.map +1 -1
  14. package/dist/dev/Money.js.map +1 -1
  15. package/dist/dev/ProductPrice.cjs.map +1 -1
  16. package/dist/dev/ProductPrice.js.map +1 -1
  17. package/dist/dev/ProductProvider.cjs +14 -14
  18. package/dist/dev/ProductProvider.cjs.map +1 -1
  19. package/dist/dev/ShopPayButton.cjs.map +1 -1
  20. package/dist/dev/ShopPayButton.js.map +1 -1
  21. package/dist/dev/ShopifyProvider.cjs +4 -4
  22. package/dist/dev/ShopifyProvider.cjs.map +1 -1
  23. package/dist/dev/Video.cjs +2 -2
  24. package/dist/dev/_virtual/index.cjs +5 -0
  25. package/dist/dev/_virtual/index.cjs.map +1 -0
  26. package/dist/dev/_virtual/index.js +5 -0
  27. package/dist/dev/_virtual/index.js.map +1 -0
  28. package/dist/dev/_virtual/use-sync-external-store-shim.development.cjs +5 -0
  29. package/dist/dev/_virtual/use-sync-external-store-shim.development.cjs.map +1 -0
  30. package/dist/dev/_virtual/use-sync-external-store-shim.development.js +5 -0
  31. package/dist/dev/_virtual/use-sync-external-store-shim.development.js.map +1 -0
  32. package/dist/dev/_virtual/use-sync-external-store-shim.production.min.cjs +5 -0
  33. package/dist/dev/_virtual/use-sync-external-store-shim.production.min.cjs.map +1 -0
  34. package/dist/dev/_virtual/use-sync-external-store-shim.production.min.js +5 -0
  35. package/dist/dev/_virtual/use-sync-external-store-shim.production.min.js.map +1 -0
  36. package/dist/dev/_virtual/with-selector.cjs +5 -0
  37. package/dist/dev/_virtual/with-selector.cjs.map +1 -0
  38. package/dist/dev/_virtual/with-selector.development.cjs +5 -0
  39. package/dist/dev/_virtual/with-selector.development.cjs.map +1 -0
  40. package/dist/dev/_virtual/with-selector.development.js +5 -0
  41. package/dist/dev/_virtual/with-selector.development.js.map +1 -0
  42. package/dist/dev/_virtual/with-selector.js +5 -0
  43. package/dist/dev/_virtual/with-selector.js.map +1 -0
  44. package/dist/dev/_virtual/with-selector.production.min.cjs +5 -0
  45. package/dist/dev/_virtual/with-selector.production.min.cjs.map +1 -0
  46. package/dist/dev/_virtual/with-selector.production.min.js +5 -0
  47. package/dist/dev/_virtual/with-selector.production.min.js.map +1 -0
  48. package/dist/dev/cart-constants.cjs +17 -0
  49. package/dist/dev/cart-constants.cjs.map +1 -0
  50. package/dist/dev/cart-constants.js +17 -0
  51. package/dist/dev/cart-constants.js.map +1 -0
  52. package/dist/dev/cart-hooks.cjs +48 -0
  53. package/dist/dev/cart-hooks.cjs.map +1 -0
  54. package/dist/dev/cart-hooks.js +48 -0
  55. package/dist/dev/cart-hooks.js.map +1 -0
  56. package/dist/dev/cart-queries.cjs +114 -0
  57. package/dist/dev/cart-queries.cjs.map +1 -0
  58. package/dist/dev/cart-queries.js +114 -0
  59. package/dist/dev/cart-queries.js.map +1 -0
  60. package/dist/dev/flatten-connection.cjs +6 -0
  61. package/dist/dev/flatten-connection.cjs.map +1 -1
  62. package/dist/dev/flatten-connection.js +6 -0
  63. package/dist/dev/flatten-connection.js.map +1 -1
  64. package/dist/dev/index.cjs +3 -0
  65. package/dist/dev/index.cjs.map +1 -1
  66. package/dist/dev/index.js +3 -0
  67. package/dist/dev/index.js.map +1 -1
  68. package/dist/dev/load-script.cjs +3 -3
  69. package/dist/dev/load-script.cjs.map +1 -1
  70. package/dist/dev/node_modules/@xstate/fsm/es/index.cjs +158 -0
  71. package/dist/dev/node_modules/@xstate/fsm/es/index.cjs.map +1 -0
  72. package/dist/dev/node_modules/@xstate/fsm/es/index.js +159 -0
  73. package/dist/dev/node_modules/@xstate/fsm/es/index.js.map +1 -0
  74. package/dist/dev/node_modules/@xstate/react/es/fsm.cjs +94 -0
  75. package/dist/dev/node_modules/@xstate/react/es/fsm.cjs.map +1 -0
  76. package/dist/dev/node_modules/@xstate/react/es/fsm.js +94 -0
  77. package/dist/dev/node_modules/@xstate/react/es/fsm.js.map +1 -0
  78. package/dist/dev/node_modules/@xstate/react/es/useConstant.cjs +30 -0
  79. package/dist/dev/node_modules/@xstate/react/es/useConstant.cjs.map +1 -0
  80. package/dist/dev/node_modules/@xstate/react/es/useConstant.js +12 -0
  81. package/dist/dev/node_modules/@xstate/react/es/useConstant.js.map +1 -0
  82. package/dist/dev/node_modules/use-isomorphic-layout-effect/dist/use-isomorphic-layout-effect.browser.esm.cjs +5 -0
  83. package/dist/dev/node_modules/use-isomorphic-layout-effect/dist/use-isomorphic-layout-effect.browser.esm.cjs.map +1 -0
  84. package/dist/dev/node_modules/use-isomorphic-layout-effect/dist/use-isomorphic-layout-effect.browser.esm.js +6 -0
  85. package/dist/dev/node_modules/use-isomorphic-layout-effect/dist/use-isomorphic-layout-effect.browser.esm.js.map +1 -0
  86. package/dist/dev/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim/with-selector.development.cjs +107 -0
  87. package/dist/dev/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim/with-selector.development.cjs.map +1 -0
  88. package/dist/dev/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim/with-selector.development.js +105 -0
  89. package/dist/dev/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim/with-selector.development.js.map +1 -0
  90. package/dist/dev/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim/with-selector.production.min.cjs +74 -0
  91. package/dist/dev/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim/with-selector.production.min.cjs.map +1 -0
  92. package/dist/dev/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim/with-selector.production.min.js +72 -0
  93. package/dist/dev/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim/with-selector.production.min.js.map +1 -0
  94. package/dist/dev/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim.development.cjs +138 -0
  95. package/dist/dev/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim.development.cjs.map +1 -0
  96. package/dist/dev/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim.development.js +136 -0
  97. package/dist/dev/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim.development.js.map +1 -0
  98. package/dist/dev/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim.production.min.cjs +60 -0
  99. package/dist/dev/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim.production.min.cjs.map +1 -0
  100. package/dist/dev/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim.production.min.js +58 -0
  101. package/dist/dev/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim.production.min.js.map +1 -0
  102. package/dist/dev/node_modules/use-sync-external-store/shim/index.cjs +21 -0
  103. package/dist/dev/node_modules/use-sync-external-store/shim/index.cjs.map +1 -0
  104. package/dist/dev/node_modules/use-sync-external-store/shim/index.js +21 -0
  105. package/dist/dev/node_modules/use-sync-external-store/shim/index.js.map +1 -0
  106. package/dist/dev/node_modules/use-sync-external-store/shim/with-selector.cjs +12 -0
  107. package/dist/dev/node_modules/use-sync-external-store/shim/with-selector.cjs.map +1 -0
  108. package/dist/dev/node_modules/use-sync-external-store/shim/with-selector.js +11 -0
  109. package/dist/dev/node_modules/use-sync-external-store/shim/with-selector.js.map +1 -0
  110. package/dist/dev/node_modules/worktop/cookie/index.cjs +27 -0
  111. package/dist/dev/node_modules/worktop/cookie/index.cjs.map +1 -0
  112. package/dist/dev/node_modules/worktop/cookie/index.js +27 -0
  113. package/dist/dev/node_modules/worktop/cookie/index.js.map +1 -0
  114. package/dist/dev/storefront-api-constants.cjs +1 -1
  115. package/dist/dev/storefront-api-constants.cjs.map +1 -1
  116. package/dist/dev/storefront-api-constants.js +1 -1
  117. package/dist/dev/storefront-api-constants.js.map +1 -1
  118. package/dist/dev/useCartAPIStateMachine.cjs +326 -0
  119. package/dist/dev/useCartAPIStateMachine.cjs.map +1 -0
  120. package/dist/dev/useCartAPIStateMachine.js +326 -0
  121. package/dist/dev/useCartAPIStateMachine.js.map +1 -0
  122. package/dist/dev/useCartActions.cjs +123 -0
  123. package/dist/dev/useCartActions.cjs.map +1 -0
  124. package/dist/dev/useCartActions.js +123 -0
  125. package/dist/dev/useCartActions.js.map +1 -0
  126. package/dist/dev/useMoney.cjs +5 -5
  127. package/dist/dev/useMoney.cjs.map +1 -1
  128. package/dist/prod/CartProvider.cjs +485 -0
  129. package/dist/prod/CartProvider.cjs.map +1 -0
  130. package/dist/prod/CartProvider.js +485 -0
  131. package/dist/prod/CartProvider.js.map +1 -0
  132. package/dist/prod/MediaFile.cjs.map +1 -1
  133. package/dist/prod/MediaFile.js.map +1 -1
  134. package/dist/prod/Metafield.cjs +2 -2
  135. package/dist/prod/Metafield.cjs.map +1 -1
  136. package/dist/prod/Metafield.js.map +1 -1
  137. package/dist/prod/ModelViewer.cjs +4 -4
  138. package/dist/prod/ModelViewer.cjs.map +1 -1
  139. package/dist/prod/Money.cjs.map +1 -1
  140. package/dist/prod/Money.js.map +1 -1
  141. package/dist/prod/ProductPrice.cjs.map +1 -1
  142. package/dist/prod/ProductPrice.js.map +1 -1
  143. package/dist/prod/ProductProvider.cjs +14 -14
  144. package/dist/prod/ProductProvider.cjs.map +1 -1
  145. package/dist/prod/ShopPayButton.cjs.map +1 -1
  146. package/dist/prod/ShopPayButton.js.map +1 -1
  147. package/dist/prod/ShopifyProvider.cjs +4 -4
  148. package/dist/prod/ShopifyProvider.cjs.map +1 -1
  149. package/dist/prod/Video.cjs +2 -2
  150. package/dist/prod/_virtual/index.cjs +5 -0
  151. package/dist/prod/_virtual/index.cjs.map +1 -0
  152. package/dist/prod/_virtual/index.js +5 -0
  153. package/dist/prod/_virtual/index.js.map +1 -0
  154. package/dist/prod/_virtual/use-sync-external-store-shim.development.cjs +5 -0
  155. package/dist/prod/_virtual/use-sync-external-store-shim.development.cjs.map +1 -0
  156. package/dist/prod/_virtual/use-sync-external-store-shim.development.js +5 -0
  157. package/dist/prod/_virtual/use-sync-external-store-shim.development.js.map +1 -0
  158. package/dist/prod/_virtual/use-sync-external-store-shim.production.min.cjs +5 -0
  159. package/dist/prod/_virtual/use-sync-external-store-shim.production.min.cjs.map +1 -0
  160. package/dist/prod/_virtual/use-sync-external-store-shim.production.min.js +5 -0
  161. package/dist/prod/_virtual/use-sync-external-store-shim.production.min.js.map +1 -0
  162. package/dist/prod/_virtual/with-selector.cjs +5 -0
  163. package/dist/prod/_virtual/with-selector.cjs.map +1 -0
  164. package/dist/prod/_virtual/with-selector.development.cjs +5 -0
  165. package/dist/prod/_virtual/with-selector.development.cjs.map +1 -0
  166. package/dist/prod/_virtual/with-selector.development.js +5 -0
  167. package/dist/prod/_virtual/with-selector.development.js.map +1 -0
  168. package/dist/prod/_virtual/with-selector.js +5 -0
  169. package/dist/prod/_virtual/with-selector.js.map +1 -0
  170. package/dist/prod/_virtual/with-selector.production.min.cjs +5 -0
  171. package/dist/prod/_virtual/with-selector.production.min.cjs.map +1 -0
  172. package/dist/prod/_virtual/with-selector.production.min.js +5 -0
  173. package/dist/prod/_virtual/with-selector.production.min.js.map +1 -0
  174. package/dist/prod/cart-constants.cjs +17 -0
  175. package/dist/prod/cart-constants.cjs.map +1 -0
  176. package/dist/prod/cart-constants.js +17 -0
  177. package/dist/prod/cart-constants.js.map +1 -0
  178. package/dist/prod/cart-hooks.cjs +48 -0
  179. package/dist/prod/cart-hooks.cjs.map +1 -0
  180. package/dist/prod/cart-hooks.js +48 -0
  181. package/dist/prod/cart-hooks.js.map +1 -0
  182. package/dist/prod/cart-queries.cjs +114 -0
  183. package/dist/prod/cart-queries.cjs.map +1 -0
  184. package/dist/prod/cart-queries.js +114 -0
  185. package/dist/prod/cart-queries.js.map +1 -0
  186. package/dist/prod/flatten-connection.cjs +7 -0
  187. package/dist/prod/flatten-connection.cjs.map +1 -1
  188. package/dist/prod/flatten-connection.js +7 -0
  189. package/dist/prod/flatten-connection.js.map +1 -1
  190. package/dist/prod/index.cjs +3 -0
  191. package/dist/prod/index.cjs.map +1 -1
  192. package/dist/prod/index.js +3 -0
  193. package/dist/prod/index.js.map +1 -1
  194. package/dist/prod/load-script.cjs +3 -3
  195. package/dist/prod/load-script.cjs.map +1 -1
  196. package/dist/prod/node_modules/@xstate/fsm/es/index.cjs +158 -0
  197. package/dist/prod/node_modules/@xstate/fsm/es/index.cjs.map +1 -0
  198. package/dist/prod/node_modules/@xstate/fsm/es/index.js +159 -0
  199. package/dist/prod/node_modules/@xstate/fsm/es/index.js.map +1 -0
  200. package/dist/prod/node_modules/@xstate/react/es/fsm.cjs +94 -0
  201. package/dist/prod/node_modules/@xstate/react/es/fsm.cjs.map +1 -0
  202. package/dist/prod/node_modules/@xstate/react/es/fsm.js +94 -0
  203. package/dist/prod/node_modules/@xstate/react/es/fsm.js.map +1 -0
  204. package/dist/prod/node_modules/@xstate/react/es/useConstant.cjs +30 -0
  205. package/dist/prod/node_modules/@xstate/react/es/useConstant.cjs.map +1 -0
  206. package/dist/prod/node_modules/@xstate/react/es/useConstant.js +12 -0
  207. package/dist/prod/node_modules/@xstate/react/es/useConstant.js.map +1 -0
  208. package/dist/prod/node_modules/use-isomorphic-layout-effect/dist/use-isomorphic-layout-effect.browser.esm.cjs +5 -0
  209. package/dist/prod/node_modules/use-isomorphic-layout-effect/dist/use-isomorphic-layout-effect.browser.esm.cjs.map +1 -0
  210. package/dist/prod/node_modules/use-isomorphic-layout-effect/dist/use-isomorphic-layout-effect.browser.esm.js +6 -0
  211. package/dist/prod/node_modules/use-isomorphic-layout-effect/dist/use-isomorphic-layout-effect.browser.esm.js.map +1 -0
  212. package/dist/prod/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim/with-selector.development.cjs +107 -0
  213. package/dist/prod/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim/with-selector.development.cjs.map +1 -0
  214. package/dist/prod/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim/with-selector.development.js +105 -0
  215. package/dist/prod/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim/with-selector.development.js.map +1 -0
  216. package/dist/prod/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim/with-selector.production.min.cjs +74 -0
  217. package/dist/prod/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim/with-selector.production.min.cjs.map +1 -0
  218. package/dist/prod/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim/with-selector.production.min.js +72 -0
  219. package/dist/prod/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim/with-selector.production.min.js.map +1 -0
  220. package/dist/prod/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim.development.cjs +138 -0
  221. package/dist/prod/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim.development.cjs.map +1 -0
  222. package/dist/prod/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim.development.js +136 -0
  223. package/dist/prod/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim.development.js.map +1 -0
  224. package/dist/prod/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim.production.min.cjs +60 -0
  225. package/dist/prod/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim.production.min.cjs.map +1 -0
  226. package/dist/prod/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim.production.min.js +58 -0
  227. package/dist/prod/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim.production.min.js.map +1 -0
  228. package/dist/prod/node_modules/use-sync-external-store/shim/index.cjs +21 -0
  229. package/dist/prod/node_modules/use-sync-external-store/shim/index.cjs.map +1 -0
  230. package/dist/prod/node_modules/use-sync-external-store/shim/index.js +21 -0
  231. package/dist/prod/node_modules/use-sync-external-store/shim/index.js.map +1 -0
  232. package/dist/prod/node_modules/use-sync-external-store/shim/with-selector.cjs +12 -0
  233. package/dist/prod/node_modules/use-sync-external-store/shim/with-selector.cjs.map +1 -0
  234. package/dist/prod/node_modules/use-sync-external-store/shim/with-selector.js +11 -0
  235. package/dist/prod/node_modules/use-sync-external-store/shim/with-selector.js.map +1 -0
  236. package/dist/prod/node_modules/worktop/cookie/index.cjs +27 -0
  237. package/dist/prod/node_modules/worktop/cookie/index.cjs.map +1 -0
  238. package/dist/prod/node_modules/worktop/cookie/index.js +27 -0
  239. package/dist/prod/node_modules/worktop/cookie/index.js.map +1 -0
  240. package/dist/prod/storefront-api-constants.cjs +1 -1
  241. package/dist/prod/storefront-api-constants.cjs.map +1 -1
  242. package/dist/prod/storefront-api-constants.js +1 -1
  243. package/dist/prod/storefront-api-constants.js.map +1 -1
  244. package/dist/prod/useCartAPIStateMachine.cjs +326 -0
  245. package/dist/prod/useCartAPIStateMachine.cjs.map +1 -0
  246. package/dist/prod/useCartAPIStateMachine.js +326 -0
  247. package/dist/prod/useCartAPIStateMachine.js.map +1 -0
  248. package/dist/prod/useCartActions.cjs +123 -0
  249. package/dist/prod/useCartActions.cjs.map +1 -0
  250. package/dist/prod/useCartActions.js +123 -0
  251. package/dist/prod/useCartActions.js.map +1 -0
  252. package/dist/prod/useMoney.cjs +5 -5
  253. package/dist/prod/useMoney.cjs.map +1 -1
  254. package/dist/types/CartProvider.d.ts +61 -0
  255. package/dist/types/MediaFile.d.ts +0 -1
  256. package/dist/types/Money.d.ts +1 -1
  257. package/dist/types/ProductPrice.d.ts +0 -1
  258. package/dist/types/cart-constants.d.ts +8 -0
  259. package/dist/types/cart-hooks.d.ts +24 -0
  260. package/dist/types/cart-queries.d.ts +10 -0
  261. package/dist/types/cart-types.d.ts +289 -0
  262. package/dist/types/flatten-connection.d.ts +3 -1
  263. package/dist/types/index.d.cts +2 -0
  264. package/dist/types/index.d.ts +2 -0
  265. package/dist/types/storefront-api-constants.d.ts +1 -1
  266. package/dist/types/storefront-api-types.d.ts +278 -73
  267. package/dist/types/useCartAPIStateMachine.d.ts +25 -0
  268. package/dist/types/useCartActions.d.ts +64 -0
  269. package/dist/umd/hydrogen-react.dev.js +1815 -109
  270. package/dist/umd/hydrogen-react.dev.js.map +1 -1
  271. package/dist/umd/hydrogen-react.prod.js +239 -2
  272. package/dist/umd/hydrogen-react.prod.js.map +1 -1
  273. package/package.json +24 -12
  274. package/storefront.schema.json +1 -1
@@ -0,0 +1,123 @@
1
+ import { useCallback, useMemo } from "react";
2
+ import { CartQuery, CartCreate, CartLineAdd, CartLineUpdate, CartLineRemove, CartNoteUpdate, CartBuyerIdentityUpdate, CartAttributesUpdate, CartDiscountCodesUpdate } from "./cart-queries.js";
3
+ import { useCartFetch } from "./cart-hooks.js";
4
+ function useCartActions({
5
+ numCartLines,
6
+ cartFragment,
7
+ countryCode = "US"
8
+ }) {
9
+ const fetchCart = useCartFetch();
10
+ const cartFetch = useCallback((cartId) => {
11
+ return fetchCart({
12
+ query: CartQuery(cartFragment),
13
+ variables: {
14
+ id: cartId,
15
+ numCartLines,
16
+ country: countryCode
17
+ }
18
+ });
19
+ }, [fetchCart, cartFragment, numCartLines, countryCode]);
20
+ const cartCreate = useCallback((cart) => {
21
+ return fetchCart({
22
+ query: CartCreate(cartFragment),
23
+ variables: {
24
+ input: cart,
25
+ numCartLines,
26
+ country: countryCode
27
+ }
28
+ });
29
+ }, [cartFragment, countryCode, fetchCart, numCartLines]);
30
+ const cartLineAdd = useCallback((cartId, lines) => {
31
+ return fetchCart({
32
+ query: CartLineAdd(cartFragment),
33
+ variables: {
34
+ cartId,
35
+ lines,
36
+ numCartLines,
37
+ country: countryCode
38
+ }
39
+ });
40
+ }, [cartFragment, countryCode, fetchCart, numCartLines]);
41
+ const cartLineUpdate = useCallback((cartId, lines) => {
42
+ return fetchCart({
43
+ query: CartLineUpdate(cartFragment),
44
+ variables: {
45
+ cartId,
46
+ lines,
47
+ numCartLines,
48
+ country: countryCode
49
+ }
50
+ });
51
+ }, [cartFragment, countryCode, fetchCart, numCartLines]);
52
+ const cartLineRemove = useCallback((cartId, lines) => {
53
+ return fetchCart({
54
+ query: CartLineRemove(cartFragment),
55
+ variables: {
56
+ cartId,
57
+ lines,
58
+ numCartLines,
59
+ country: countryCode
60
+ }
61
+ });
62
+ }, [cartFragment, countryCode, fetchCart, numCartLines]);
63
+ const noteUpdate = useCallback((cartId, note) => {
64
+ return fetchCart({
65
+ query: CartNoteUpdate(cartFragment),
66
+ variables: {
67
+ cartId,
68
+ note,
69
+ numCartLines,
70
+ country: countryCode
71
+ }
72
+ });
73
+ }, [fetchCart, cartFragment, numCartLines, countryCode]);
74
+ const buyerIdentityUpdate = useCallback((cartId, buyerIdentity) => {
75
+ return fetchCart({
76
+ query: CartBuyerIdentityUpdate(cartFragment),
77
+ variables: {
78
+ cartId,
79
+ buyerIdentity,
80
+ numCartLines,
81
+ country: countryCode
82
+ }
83
+ });
84
+ }, [cartFragment, countryCode, fetchCart, numCartLines]);
85
+ const cartAttributesUpdate = useCallback((cartId, attributes) => {
86
+ return fetchCart({
87
+ query: CartAttributesUpdate(cartFragment),
88
+ variables: {
89
+ cartId,
90
+ attributes,
91
+ numCartLines,
92
+ country: countryCode
93
+ }
94
+ });
95
+ }, [cartFragment, countryCode, fetchCart, numCartLines]);
96
+ const discountCodesUpdate = useCallback((cartId, discountCodes) => {
97
+ return fetchCart({
98
+ query: CartDiscountCodesUpdate(cartFragment),
99
+ variables: {
100
+ cartId,
101
+ discountCodes,
102
+ numCartLines,
103
+ country: countryCode
104
+ }
105
+ });
106
+ }, [cartFragment, countryCode, fetchCart, numCartLines]);
107
+ return useMemo(() => ({
108
+ cartFetch,
109
+ cartCreate,
110
+ cartLineAdd,
111
+ cartLineUpdate,
112
+ cartLineRemove,
113
+ noteUpdate,
114
+ buyerIdentityUpdate,
115
+ cartAttributesUpdate,
116
+ discountCodesUpdate,
117
+ cartFragment
118
+ }), [cartFetch, cartCreate, cartLineAdd, cartLineUpdate, cartLineRemove, noteUpdate, buyerIdentityUpdate, cartAttributesUpdate, discountCodesUpdate, cartFragment]);
119
+ }
120
+ export {
121
+ useCartActions
122
+ };
123
+ //# sourceMappingURL=useCartActions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useCartActions.js","sources":["../../src/useCartActions.tsx"],"sourcesContent":["import {useCallback, useMemo} from 'react';\nimport {\n AttributeInput,\n CartBuyerIdentityInput,\n CartInput,\n CartLineInput,\n CartLineUpdateInput,\n CountryCode,\n Cart as CartType,\n MutationCartDiscountCodesUpdateArgs,\n MutationCartNoteUpdateArgs,\n} from './storefront-api-types.js';\nimport {\n CartAttributesUpdate,\n CartBuyerIdentityUpdate,\n CartCreate,\n CartDiscountCodesUpdate,\n CartLineAdd,\n CartLineRemove,\n CartLineUpdate,\n CartNoteUpdate,\n CartQuery,\n} from './cart-queries.js';\nimport {useCartFetch} from './cart-hooks.js';\nimport {PartialDeep} from 'type-fest';\n\ntype CartResponse = PartialDeep<CartType, {recurseIntoArrays: true}>;\n\n/**\n * The `useCartActions` hook returns helper graphql functions for Storefront Cart API\n *\n * See [cart API graphql mutations](https://shopify.dev/api/storefront/2022-10/objects/Cart)\n */\nexport function useCartActions({\n numCartLines,\n cartFragment,\n countryCode = 'US',\n}: {\n /** Maximum number of cart lines to fetch. Defaults to 250 cart lines. */\n numCartLines?: number;\n /** A fragment used to query the Storefront API's [Cart object](https://shopify.dev/api/storefront/latest/objects/cart) for all queries and mutations. A default value is used if no argument is provided. */\n cartFragment: string;\n /** The ISO country code for i18n. */\n countryCode?: CountryCode;\n}) {\n const fetchCart = useCartFetch();\n\n const cartFetch = useCallback(\n (cartId: string) => {\n return fetchCart<{cart: CartResponse}>({\n query: CartQuery(cartFragment),\n variables: {\n id: cartId,\n numCartLines,\n country: countryCode,\n },\n });\n },\n [fetchCart, cartFragment, numCartLines, countryCode]\n );\n\n const cartCreate = useCallback(\n (cart: CartInput) => {\n return fetchCart<{cartCreate: {cart: CartResponse}}>({\n query: CartCreate(cartFragment),\n variables: {\n input: cart,\n numCartLines,\n country: countryCode,\n },\n });\n },\n [cartFragment, countryCode, fetchCart, numCartLines]\n );\n\n const cartLineAdd = useCallback(\n (cartId: string, lines: CartLineInput[]) => {\n return fetchCart<{cartLinesAdd: {cart: CartResponse}}>({\n query: CartLineAdd(cartFragment),\n variables: {\n cartId,\n lines,\n numCartLines,\n country: countryCode,\n },\n });\n },\n [cartFragment, countryCode, fetchCart, numCartLines]\n );\n\n const cartLineUpdate = useCallback(\n (cartId: string, lines: CartLineUpdateInput[]) => {\n return fetchCart<{cartLinesUpdate: {cart: CartResponse}}>({\n query: CartLineUpdate(cartFragment),\n variables: {\n cartId,\n lines,\n numCartLines,\n country: countryCode,\n },\n });\n },\n [cartFragment, countryCode, fetchCart, numCartLines]\n );\n\n const cartLineRemove = useCallback(\n (cartId: string, lines: string[]) => {\n return fetchCart<{cartLinesRemove: {cart: CartResponse}}>({\n query: CartLineRemove(cartFragment),\n variables: {\n cartId,\n lines,\n numCartLines,\n country: countryCode,\n },\n });\n },\n [cartFragment, countryCode, fetchCart, numCartLines]\n );\n\n const noteUpdate = useCallback(\n (cartId: string, note: MutationCartNoteUpdateArgs['note']) => {\n return fetchCart<{cartNoteUpdate: {cart: CartResponse}}>({\n query: CartNoteUpdate(cartFragment),\n variables: {\n cartId,\n note,\n numCartLines,\n country: countryCode,\n },\n });\n },\n [fetchCart, cartFragment, numCartLines, countryCode]\n );\n\n const buyerIdentityUpdate = useCallback(\n (cartId: string, buyerIdentity: CartBuyerIdentityInput) => {\n return fetchCart<{cartBuyerIdentityUpdate: {cart: CartResponse}}>({\n query: CartBuyerIdentityUpdate(cartFragment),\n variables: {\n cartId,\n buyerIdentity,\n numCartLines,\n country: countryCode,\n },\n });\n },\n [cartFragment, countryCode, fetchCart, numCartLines]\n );\n\n const cartAttributesUpdate = useCallback(\n (cartId: string, attributes: AttributeInput[]) => {\n return fetchCart<{cartAttributesUpdate: {cart: CartResponse}}>({\n query: CartAttributesUpdate(cartFragment),\n variables: {\n cartId,\n attributes,\n numCartLines,\n country: countryCode,\n },\n });\n },\n [cartFragment, countryCode, fetchCart, numCartLines]\n );\n\n const discountCodesUpdate = useCallback(\n (\n cartId: string,\n discountCodes: MutationCartDiscountCodesUpdateArgs['discountCodes']\n ) => {\n return fetchCart<{cartDiscountCodesUpdate: {cart: CartResponse}}>({\n query: CartDiscountCodesUpdate(cartFragment),\n variables: {\n cartId,\n discountCodes,\n numCartLines,\n country: countryCode,\n },\n });\n },\n [cartFragment, countryCode, fetchCart, numCartLines]\n );\n\n return useMemo(\n () => ({\n cartFetch,\n cartCreate,\n cartLineAdd,\n cartLineUpdate,\n cartLineRemove,\n noteUpdate,\n buyerIdentityUpdate,\n cartAttributesUpdate,\n discountCodesUpdate,\n cartFragment,\n }),\n [\n cartFetch,\n cartCreate,\n cartLineAdd,\n cartLineUpdate,\n cartLineRemove,\n noteUpdate,\n buyerIdentityUpdate,\n cartAttributesUpdate,\n discountCodesUpdate,\n cartFragment,\n ]\n );\n}\n"],"names":["useCartActions","numCartLines","cartFragment","countryCode","fetchCart","useCartFetch","cartFetch","useCallback","cartId","query","CartQuery","variables","id","country","cartCreate","cart","CartCreate","input","cartLineAdd","lines","CartLineAdd","cartLineUpdate","CartLineUpdate","cartLineRemove","CartLineRemove","noteUpdate","note","CartNoteUpdate","buyerIdentityUpdate","buyerIdentity","CartBuyerIdentityUpdate","cartAttributesUpdate","attributes","CartAttributesUpdate","discountCodesUpdate","discountCodes","CartDiscountCodesUpdate","useMemo"],"mappings":";;;AAiCO,SAASA,eAAe;AAAA,EAC7BC;AAAAA,EACAC;AAAAA,EACAC,cAAc;AAHe,GAW5B;AACD,QAAMC,YAAYC;AAEZC,QAAAA,YAAYC,YAChB,CAACC,WAAmB;AAClB,WAAOJ,UAAgC;AAAA,MACrCK,OA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AAAAA,IACAE;AAAAA,IACAG;AAAAA,IACAG;AAAAA,IACAG;AAAAA,IACAhC;AAAAA,EAEF,IAAA,CACEI,WACAQ,YACAI,aACAG,gBACAE,gBACAE,YACAG,qBACAG,sBACAG,qBACAhC,YAVF,CAbY;AA0Bf;"}
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
3
- const react = require("react");
3
+ const require$$0 = require("react");
4
4
  const ShopifyProvider = require("./ShopifyProvider.cjs");
5
5
  function useMoney(money) {
6
6
  const {
@@ -10,7 +10,7 @@ function useMoney(money) {
10
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
11
  }
12
12
  const amount = parseFloat(money.amount);
13
- const options = react.useMemo(() => ({
13
+ const options = require$$0.useMemo(() => ({
14
14
  style: "currency",
15
15
  currency: money.currencyCode
16
16
  }), [money.currencyCode]);
@@ -34,7 +34,7 @@ function useMoney(money) {
34
34
  maximumFractionDigits: 0
35
35
  });
36
36
  const isPartCurrency = (part) => part.type === "currency";
37
- const lazyFormatters = react.useMemo(() => ({
37
+ const lazyFormatters = require$$0.useMemo(() => ({
38
38
  original: () => money,
39
39
  currencyCode: () => money.currencyCode,
40
40
  localizedString: () => defaultFormatter().format(amount),
@@ -55,7 +55,7 @@ function useMoney(money) {
55
55
  },
56
56
  amount: () => defaultFormatter().formatToParts(amount).filter((part) => ["decimal", "fraction", "group", "integer", "literal"].includes(part.type)).map((part) => part.value).join("")
57
57
  }), [money, amount, nameFormatter, defaultFormatter, narrowSymbolFormatter, withoutCurrencyFormatter, withoutTrailingZerosFormatter, withoutTrailingZerosOrCurrencyFormatter]);
58
- return react.useMemo(() => new Proxy(lazyFormatters, {
58
+ return require$$0.useMemo(() => new Proxy(lazyFormatters, {
59
59
  get: (target, key) => {
60
60
  var _a;
61
61
  return (_a = Reflect.get(target, key)) == null ? void 0 : _a.call(null);
@@ -63,7 +63,7 @@ function useMoney(money) {
63
63
  }), [lazyFormatters]);
64
64
  }
65
65
  function useLazyFormatter(locale, options) {
66
- return react.useMemo(() => {
66
+ return require$$0.useMemo(() => {
67
67
  let memoized;
68
68
  return () => memoized != null ? memoized : memoized = new Intl.NumberFormat(locale, options);
69
69
  }, [locale, options]);
@@ -1 +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,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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,WAAAA,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,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