@anker-in/lib 1.1.0 → 1.1.2-beta.1

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 (371) hide show
  1. package/.turbo/turbo-build.log +10 -6
  2. package/.turbo/turbo-test.log +1 -1
  3. package/dist/cart/context/index.d.ts +47 -0
  4. package/dist/cart/context/index.d.ts.map +1 -0
  5. package/dist/cart/hooks/basic/useAddToCart.d.ts +30 -0
  6. package/dist/cart/hooks/basic/useAddToCart.d.ts.map +1 -0
  7. package/dist/cart/hooks/basic/useApplyCartCodes.d.ts +11 -0
  8. package/dist/cart/hooks/basic/useApplyCartCodes.d.ts.map +1 -0
  9. package/dist/cart/hooks/basic/useCreateCart.d.ts +7 -0
  10. package/dist/cart/hooks/basic/useCreateCart.d.ts.map +1 -0
  11. package/dist/cart/hooks/basic/useRemoveCartCodes.d.ts +11 -0
  12. package/dist/cart/hooks/basic/useRemoveCartCodes.d.ts.map +1 -0
  13. package/dist/cart/hooks/basic/useRemoveCartLines.d.ts +11 -0
  14. package/dist/cart/hooks/basic/useRemoveCartLines.d.ts.map +1 -0
  15. package/dist/cart/hooks/basic/useUpdateCartAttributes.d.ts +8 -0
  16. package/dist/cart/hooks/basic/useUpdateCartAttributes.d.ts.map +1 -0
  17. package/dist/cart/hooks/basic/useUpdateCartBuyerIdentity.d.ts +8 -0
  18. package/dist/cart/hooks/basic/useUpdateCartBuyerIdentity.d.ts.map +1 -0
  19. package/dist/cart/hooks/basic/useUpdateCartLines.d.ts +8 -0
  20. package/dist/cart/hooks/basic/useUpdateCartLines.d.ts.map +1 -0
  21. package/dist/cart/hooks/feature/useCalcAutoFreeGift.d.ts +10 -0
  22. package/dist/cart/hooks/feature/useCalcAutoFreeGift.d.ts.map +1 -0
  23. package/dist/cart/hooks/feature/useCalcOrderDiscount.d.ts +21 -0
  24. package/dist/cart/hooks/feature/useCalcOrderDiscount.d.ts.map +1 -0
  25. package/dist/cart/hooks/feature/useCartAttributes.d.ts +10 -0
  26. package/dist/cart/hooks/feature/useCartAttributes.d.ts.map +1 -0
  27. package/dist/cart/hooks/feature/useCartItemQuantityLimit.d.ts +19 -0
  28. package/dist/cart/hooks/feature/useCartItemQuantityLimit.d.ts.map +1 -0
  29. package/dist/cart/hooks/feature/useScriptAutoFreeGift.d.ts +37 -0
  30. package/dist/cart/hooks/feature/useScriptAutoFreeGift.d.ts.map +1 -0
  31. package/dist/cart/hooks/feature/useUpdateLineCodeAmountAttributes.d.ts +9 -0
  32. package/dist/cart/hooks/feature/useUpdateLineCodeAmountAttributes.d.ts.map +1 -0
  33. package/dist/cart/track/gtm.d.ts +26 -0
  34. package/dist/cart/track/gtm.d.ts.map +1 -0
  35. package/dist/cart/types/autoFreeGift.d.ts +135 -0
  36. package/dist/cart/types/autoFreeGift.d.ts.map +1 -0
  37. package/dist/cart/types/orderDiscount.d.ts +35 -0
  38. package/dist/cart/types/orderDiscount.d.ts.map +1 -0
  39. package/dist/cart/types/priceDiscount.d.ts +20 -0
  40. package/dist/cart/types/priceDiscount.d.ts.map +1 -0
  41. package/dist/cart/utils/autoFreeGift.d.ts +21 -0
  42. package/dist/cart/utils/autoFreeGift.d.ts.map +1 -0
  43. package/dist/cjs/cart/context/index.d.ts +47 -0
  44. package/dist/cjs/cart/context/index.d.ts.map +1 -0
  45. package/dist/cjs/cart/context/index.js +2 -0
  46. package/dist/cjs/cart/context/index.js.map +7 -0
  47. package/dist/cjs/cart/hooks/basic/useAddToCart.d.ts +30 -0
  48. package/dist/cjs/cart/hooks/basic/useAddToCart.d.ts.map +1 -0
  49. package/dist/cjs/cart/hooks/basic/useAddToCart.js +2 -0
  50. package/dist/cjs/cart/hooks/basic/useAddToCart.js.map +7 -0
  51. package/dist/cjs/cart/hooks/basic/useApplyCartCodes.d.ts +11 -0
  52. package/dist/cjs/cart/hooks/basic/useApplyCartCodes.d.ts.map +1 -0
  53. package/dist/cjs/cart/hooks/basic/useApplyCartCodes.js +2 -0
  54. package/dist/cjs/cart/hooks/basic/useApplyCartCodes.js.map +7 -0
  55. package/dist/cjs/cart/hooks/basic/useBuyNow.d.ts +1 -1
  56. package/dist/cjs/cart/hooks/basic/useBuyNow.d.ts.map +1 -1
  57. package/dist/cjs/cart/hooks/basic/useBuyNow.js +1 -1
  58. package/dist/cjs/cart/hooks/basic/useBuyNow.js.map +3 -3
  59. package/dist/cjs/cart/hooks/basic/useCreateCart.d.ts +7 -0
  60. package/dist/cjs/cart/hooks/basic/useCreateCart.d.ts.map +1 -0
  61. package/dist/cjs/cart/hooks/basic/useCreateCart.js +2 -0
  62. package/dist/cjs/cart/hooks/basic/useCreateCart.js.map +7 -0
  63. package/dist/cjs/cart/hooks/basic/useRemoveCartCodes.d.ts +11 -0
  64. package/dist/cjs/cart/hooks/basic/useRemoveCartCodes.d.ts.map +1 -0
  65. package/dist/cjs/cart/hooks/basic/useRemoveCartCodes.js +2 -0
  66. package/dist/cjs/cart/hooks/basic/useRemoveCartCodes.js.map +7 -0
  67. package/dist/cjs/cart/hooks/basic/useRemoveCartLines.d.ts +11 -0
  68. package/dist/cjs/cart/hooks/basic/useRemoveCartLines.d.ts.map +1 -0
  69. package/dist/cjs/cart/hooks/basic/useRemoveCartLines.js +2 -0
  70. package/dist/cjs/cart/hooks/basic/useRemoveCartLines.js.map +7 -0
  71. package/dist/cjs/cart/hooks/basic/useUpdateCartAttributes.d.ts +8 -0
  72. package/dist/cjs/cart/hooks/basic/useUpdateCartAttributes.d.ts.map +1 -0
  73. package/dist/cjs/cart/hooks/basic/useUpdateCartAttributes.js +2 -0
  74. package/dist/cjs/cart/hooks/basic/useUpdateCartAttributes.js.map +7 -0
  75. package/dist/cjs/cart/hooks/basic/useUpdateCartBuyerIdentity.d.ts +8 -0
  76. package/dist/cjs/cart/hooks/basic/useUpdateCartBuyerIdentity.d.ts.map +1 -0
  77. package/dist/cjs/cart/hooks/basic/useUpdateCartBuyerIdentity.js +2 -0
  78. package/dist/cjs/cart/hooks/basic/useUpdateCartBuyerIdentity.js.map +7 -0
  79. package/dist/cjs/cart/hooks/basic/useUpdateCartLines.d.ts +8 -0
  80. package/dist/cjs/cart/hooks/basic/useUpdateCartLines.d.ts.map +1 -0
  81. package/dist/cjs/cart/hooks/basic/useUpdateCartLines.js +2 -0
  82. package/dist/cjs/cart/hooks/basic/useUpdateCartLines.js.map +7 -0
  83. package/dist/cjs/cart/hooks/feature/useCalcAutoFreeGift.d.ts +10 -0
  84. package/dist/cjs/cart/hooks/feature/useCalcAutoFreeGift.d.ts.map +1 -0
  85. package/dist/cjs/cart/hooks/feature/useCalcAutoFreeGift.js +2 -0
  86. package/dist/cjs/cart/hooks/feature/useCalcAutoFreeGift.js.map +7 -0
  87. package/dist/cjs/cart/hooks/feature/useCalcOrderDiscount.d.ts +21 -0
  88. package/dist/cjs/cart/hooks/feature/useCalcOrderDiscount.d.ts.map +1 -0
  89. package/dist/cjs/cart/hooks/feature/useCalcOrderDiscount.js +2 -0
  90. package/dist/cjs/cart/hooks/feature/useCalcOrderDiscount.js.map +7 -0
  91. package/dist/cjs/cart/hooks/feature/useCartAttributes.d.ts +10 -0
  92. package/dist/cjs/cart/hooks/feature/useCartAttributes.d.ts.map +1 -0
  93. package/dist/cjs/cart/hooks/feature/useCartAttributes.js +2 -0
  94. package/dist/cjs/cart/hooks/feature/useCartAttributes.js.map +7 -0
  95. package/dist/cjs/cart/hooks/feature/useCartItemQuantityLimit.d.ts +19 -0
  96. package/dist/cjs/cart/hooks/feature/useCartItemQuantityLimit.d.ts.map +1 -0
  97. package/dist/cjs/cart/hooks/feature/useCartItemQuantityLimit.js +2 -0
  98. package/dist/cjs/cart/hooks/feature/useCartItemQuantityLimit.js.map +7 -0
  99. package/dist/cjs/cart/hooks/feature/useScriptAutoFreeGift.d.ts +37 -0
  100. package/dist/cjs/cart/hooks/feature/useScriptAutoFreeGift.d.ts.map +1 -0
  101. package/dist/cjs/cart/hooks/feature/useScriptAutoFreeGift.js +2 -0
  102. package/dist/cjs/cart/hooks/feature/useScriptAutoFreeGift.js.map +7 -0
  103. package/dist/cjs/cart/hooks/feature/useUpdateLineCodeAmountAttributes.d.ts +9 -0
  104. package/dist/cjs/cart/hooks/feature/useUpdateLineCodeAmountAttributes.d.ts.map +1 -0
  105. package/dist/cjs/cart/hooks/feature/useUpdateLineCodeAmountAttributes.js +2 -0
  106. package/dist/cjs/cart/hooks/feature/useUpdateLineCodeAmountAttributes.js.map +7 -0
  107. package/dist/cjs/cart/track/fbq.d.ts +2 -9
  108. package/dist/cjs/cart/track/fbq.d.ts.map +1 -1
  109. package/dist/cjs/cart/track/fbq.js +1 -1
  110. package/dist/cjs/cart/track/fbq.js.map +3 -3
  111. package/dist/cjs/cart/track/gtm.d.ts +26 -0
  112. package/dist/cjs/cart/track/gtm.d.ts.map +1 -0
  113. package/dist/cjs/cart/track/gtm.js +2 -0
  114. package/dist/cjs/cart/track/gtm.js.map +7 -0
  115. package/dist/cjs/cart/types/autoFreeGift.d.ts +135 -0
  116. package/dist/cjs/cart/types/autoFreeGift.d.ts.map +1 -0
  117. package/dist/cjs/cart/types/autoFreeGift.js +2 -0
  118. package/dist/cjs/cart/types/autoFreeGift.js.map +7 -0
  119. package/dist/cjs/cart/types/index.d.ts +1 -0
  120. package/dist/cjs/cart/types/index.d.ts.map +1 -1
  121. package/dist/cjs/cart/types/index.js +1 -1
  122. package/dist/cjs/cart/types/index.js.map +1 -1
  123. package/dist/cjs/cart/types/orderDiscount.d.ts +35 -0
  124. package/dist/cjs/cart/types/orderDiscount.d.ts.map +1 -0
  125. package/dist/cjs/cart/types/orderDiscount.js +2 -0
  126. package/dist/cjs/cart/types/orderDiscount.js.map +7 -0
  127. package/dist/cjs/cart/types/priceDiscount.d.ts +20 -0
  128. package/dist/cjs/cart/types/priceDiscount.d.ts.map +1 -0
  129. package/dist/cjs/cart/types/priceDiscount.js +2 -0
  130. package/dist/cjs/cart/types/priceDiscount.js.map +7 -0
  131. package/dist/cjs/cart/utils/autoFreeGift.d.ts +21 -0
  132. package/dist/cjs/cart/utils/autoFreeGift.d.ts.map +1 -0
  133. package/dist/cjs/cart/utils/autoFreeGift.js +2 -0
  134. package/dist/cjs/cart/utils/autoFreeGift.js.map +7 -0
  135. package/dist/cjs/cart/utils/index.d.ts +1 -0
  136. package/dist/cjs/cart/utils/index.d.ts.map +1 -1
  137. package/dist/cjs/cart/utils/index.js +1 -1
  138. package/dist/cjs/cart/utils/index.js.map +3 -3
  139. package/dist/cjs/constants.d.ts +2 -0
  140. package/dist/cjs/constants.d.ts.map +1 -0
  141. package/dist/cjs/constants.js +2 -0
  142. package/dist/cjs/constants.js.map +7 -0
  143. package/dist/cjs/context/index.js.map +1 -1
  144. package/dist/cjs/index.d.ts +1 -0
  145. package/dist/cjs/index.d.ts.map +1 -1
  146. package/dist/cjs/index.js +1 -1
  147. package/dist/cjs/index.js.map +2 -2
  148. package/dist/cjs/registration/hooks/index.d.ts +1 -0
  149. package/dist/cjs/registration/hooks/index.d.ts.map +1 -1
  150. package/dist/cjs/registration/hooks/index.js +1 -1
  151. package/dist/cjs/registration/hooks/index.js.map +2 -2
  152. package/dist/cjs/registration/hooks/useEmailValidation.d.ts.map +1 -1
  153. package/dist/cjs/registration/hooks/useEmailValidation.js +1 -1
  154. package/dist/cjs/registration/hooks/useEmailValidation.js.map +3 -3
  155. package/dist/cjs/registration/hooks/useSendEmailValidation.d.ts.map +1 -1
  156. package/dist/cjs/registration/hooks/useSendEmailValidation.js +1 -1
  157. package/dist/cjs/registration/hooks/useSendEmailValidation.js.map +3 -3
  158. package/dist/cjs/registration/hooks/useSubscriptions.d.ts +15 -0
  159. package/dist/cjs/registration/hooks/useSubscriptions.d.ts.map +1 -0
  160. package/dist/cjs/registration/hooks/useSubscriptions.js +2 -0
  161. package/dist/cjs/registration/hooks/useSubscriptions.js.map +7 -0
  162. package/dist/cjs/registration/index.d.ts +0 -1
  163. package/dist/cjs/registration/index.d.ts.map +1 -1
  164. package/dist/cjs/registration/index.js +1 -1
  165. package/dist/cjs/registration/index.js.map +2 -2
  166. package/dist/cjs/shopify/fetchers/index.d.ts +2 -0
  167. package/dist/cjs/shopify/fetchers/index.d.ts.map +1 -1
  168. package/dist/cjs/shopify/fetchers/index.js +1 -1
  169. package/dist/cjs/shopify/fetchers/index.js.map +3 -3
  170. package/dist/esm/cart/context/index.d.ts +47 -0
  171. package/dist/esm/cart/context/index.d.ts.map +1 -0
  172. package/dist/esm/cart/context/index.js +2 -0
  173. package/dist/esm/cart/context/index.js.map +7 -0
  174. package/dist/esm/cart/hooks/basic/useAddToCart.d.ts +30 -0
  175. package/dist/esm/cart/hooks/basic/useAddToCart.d.ts.map +1 -0
  176. package/dist/esm/cart/hooks/basic/useAddToCart.js +2 -0
  177. package/dist/esm/cart/hooks/basic/useAddToCart.js.map +7 -0
  178. package/dist/esm/cart/hooks/basic/useApplyCartCodes.d.ts +11 -0
  179. package/dist/esm/cart/hooks/basic/useApplyCartCodes.d.ts.map +1 -0
  180. package/dist/esm/cart/hooks/basic/useApplyCartCodes.js +2 -0
  181. package/dist/esm/cart/hooks/basic/useApplyCartCodes.js.map +7 -0
  182. package/dist/esm/cart/hooks/basic/useBuyNow.d.ts +1 -1
  183. package/dist/esm/cart/hooks/basic/useBuyNow.d.ts.map +1 -1
  184. package/dist/esm/cart/hooks/basic/useBuyNow.js +1 -1
  185. package/dist/esm/cart/hooks/basic/useBuyNow.js.map +3 -3
  186. package/dist/esm/cart/hooks/basic/useCreateCart.d.ts +7 -0
  187. package/dist/esm/cart/hooks/basic/useCreateCart.d.ts.map +1 -0
  188. package/dist/esm/cart/hooks/basic/useCreateCart.js +2 -0
  189. package/dist/esm/cart/hooks/basic/useCreateCart.js.map +7 -0
  190. package/dist/esm/cart/hooks/basic/useRemoveCartCodes.d.ts +11 -0
  191. package/dist/esm/cart/hooks/basic/useRemoveCartCodes.d.ts.map +1 -0
  192. package/dist/esm/cart/hooks/basic/useRemoveCartCodes.js +2 -0
  193. package/dist/esm/cart/hooks/basic/useRemoveCartCodes.js.map +7 -0
  194. package/dist/esm/cart/hooks/basic/useRemoveCartLines.d.ts +11 -0
  195. package/dist/esm/cart/hooks/basic/useRemoveCartLines.d.ts.map +1 -0
  196. package/dist/esm/cart/hooks/basic/useRemoveCartLines.js +2 -0
  197. package/dist/esm/cart/hooks/basic/useRemoveCartLines.js.map +7 -0
  198. package/dist/esm/cart/hooks/basic/useUpdateCartAttributes.d.ts +8 -0
  199. package/dist/esm/cart/hooks/basic/useUpdateCartAttributes.d.ts.map +1 -0
  200. package/dist/esm/cart/hooks/basic/useUpdateCartAttributes.js +2 -0
  201. package/dist/esm/cart/hooks/basic/useUpdateCartAttributes.js.map +7 -0
  202. package/dist/esm/cart/hooks/basic/useUpdateCartBuyerIdentity.d.ts +8 -0
  203. package/dist/esm/cart/hooks/basic/useUpdateCartBuyerIdentity.d.ts.map +1 -0
  204. package/dist/esm/cart/hooks/basic/useUpdateCartBuyerIdentity.js +2 -0
  205. package/dist/esm/cart/hooks/basic/useUpdateCartBuyerIdentity.js.map +7 -0
  206. package/dist/esm/cart/hooks/basic/useUpdateCartLines.d.ts +8 -0
  207. package/dist/esm/cart/hooks/basic/useUpdateCartLines.d.ts.map +1 -0
  208. package/dist/esm/cart/hooks/basic/useUpdateCartLines.js +2 -0
  209. package/dist/esm/cart/hooks/basic/useUpdateCartLines.js.map +7 -0
  210. package/dist/esm/cart/hooks/feature/useCalcAutoFreeGift.d.ts +10 -0
  211. package/dist/esm/cart/hooks/feature/useCalcAutoFreeGift.d.ts.map +1 -0
  212. package/dist/esm/cart/hooks/feature/useCalcAutoFreeGift.js +2 -0
  213. package/dist/esm/cart/hooks/feature/useCalcAutoFreeGift.js.map +7 -0
  214. package/dist/esm/cart/hooks/feature/useCalcOrderDiscount.d.ts +21 -0
  215. package/dist/esm/cart/hooks/feature/useCalcOrderDiscount.d.ts.map +1 -0
  216. package/dist/esm/cart/hooks/feature/useCalcOrderDiscount.js +2 -0
  217. package/dist/esm/cart/hooks/feature/useCalcOrderDiscount.js.map +7 -0
  218. package/dist/esm/cart/hooks/feature/useCartAttributes.d.ts +10 -0
  219. package/dist/esm/cart/hooks/feature/useCartAttributes.d.ts.map +1 -0
  220. package/dist/esm/cart/hooks/feature/useCartAttributes.js +2 -0
  221. package/dist/esm/cart/hooks/feature/useCartAttributes.js.map +7 -0
  222. package/dist/esm/cart/hooks/feature/useCartItemQuantityLimit.d.ts +19 -0
  223. package/dist/esm/cart/hooks/feature/useCartItemQuantityLimit.d.ts.map +1 -0
  224. package/dist/esm/cart/hooks/feature/useCartItemQuantityLimit.js +2 -0
  225. package/dist/esm/cart/hooks/feature/useCartItemQuantityLimit.js.map +7 -0
  226. package/dist/esm/cart/hooks/feature/useScriptAutoFreeGift.d.ts +37 -0
  227. package/dist/esm/cart/hooks/feature/useScriptAutoFreeGift.d.ts.map +1 -0
  228. package/dist/esm/cart/hooks/feature/useScriptAutoFreeGift.js +2 -0
  229. package/dist/esm/cart/hooks/feature/useScriptAutoFreeGift.js.map +7 -0
  230. package/dist/esm/cart/hooks/feature/useUpdateLineCodeAmountAttributes.d.ts +9 -0
  231. package/dist/esm/cart/hooks/feature/useUpdateLineCodeAmountAttributes.d.ts.map +1 -0
  232. package/dist/esm/cart/hooks/feature/useUpdateLineCodeAmountAttributes.js +2 -0
  233. package/dist/esm/cart/hooks/feature/useUpdateLineCodeAmountAttributes.js.map +7 -0
  234. package/dist/esm/cart/track/fbq.d.ts +2 -9
  235. package/dist/esm/cart/track/fbq.d.ts.map +1 -1
  236. package/dist/esm/cart/track/fbq.js +1 -1
  237. package/dist/esm/cart/track/fbq.js.map +3 -3
  238. package/dist/esm/cart/track/gtm.d.ts +26 -0
  239. package/dist/esm/cart/track/gtm.d.ts.map +1 -0
  240. package/dist/esm/cart/track/gtm.js +2 -0
  241. package/dist/esm/cart/track/gtm.js.map +7 -0
  242. package/dist/esm/cart/types/autoFreeGift.d.ts +135 -0
  243. package/dist/esm/cart/types/autoFreeGift.d.ts.map +1 -0
  244. package/dist/esm/cart/types/autoFreeGift.js +2 -0
  245. package/dist/esm/cart/types/autoFreeGift.js.map +7 -0
  246. package/dist/esm/cart/types/index.d.ts +1 -0
  247. package/dist/esm/cart/types/index.d.ts.map +1 -1
  248. package/dist/esm/cart/types/orderDiscount.d.ts +35 -0
  249. package/dist/esm/cart/types/orderDiscount.d.ts.map +1 -0
  250. package/dist/esm/cart/types/orderDiscount.js +2 -0
  251. package/dist/esm/cart/types/orderDiscount.js.map +7 -0
  252. package/dist/esm/cart/types/priceDiscount.d.ts +20 -0
  253. package/dist/esm/cart/types/priceDiscount.d.ts.map +1 -0
  254. package/dist/esm/cart/types/priceDiscount.js +2 -0
  255. package/dist/esm/cart/types/priceDiscount.js.map +7 -0
  256. package/dist/esm/cart/utils/autoFreeGift.d.ts +21 -0
  257. package/dist/esm/cart/utils/autoFreeGift.d.ts.map +1 -0
  258. package/dist/esm/cart/utils/autoFreeGift.js +2 -0
  259. package/dist/esm/cart/utils/autoFreeGift.js.map +7 -0
  260. package/dist/esm/cart/utils/index.d.ts +1 -0
  261. package/dist/esm/cart/utils/index.d.ts.map +1 -1
  262. package/dist/esm/cart/utils/index.js +1 -1
  263. package/dist/esm/cart/utils/index.js.map +3 -3
  264. package/dist/esm/constants.d.ts +2 -0
  265. package/dist/esm/constants.d.ts.map +1 -0
  266. package/dist/esm/constants.js +2 -0
  267. package/dist/esm/constants.js.map +7 -0
  268. package/dist/esm/context/index.js.map +1 -1
  269. package/dist/esm/index.d.ts +1 -0
  270. package/dist/esm/index.d.ts.map +1 -1
  271. package/dist/esm/index.js +1 -1
  272. package/dist/esm/index.js.map +2 -2
  273. package/dist/esm/registration/hooks/index.d.ts +1 -0
  274. package/dist/esm/registration/hooks/index.d.ts.map +1 -1
  275. package/dist/esm/registration/hooks/index.js +1 -1
  276. package/dist/esm/registration/hooks/index.js.map +2 -2
  277. package/dist/esm/registration/hooks/useEmailValidation.d.ts.map +1 -1
  278. package/dist/esm/registration/hooks/useEmailValidation.js +1 -1
  279. package/dist/esm/registration/hooks/useEmailValidation.js.map +3 -3
  280. package/dist/esm/registration/hooks/useSendEmailValidation.d.ts.map +1 -1
  281. package/dist/esm/registration/hooks/useSendEmailValidation.js +1 -1
  282. package/dist/esm/registration/hooks/useSendEmailValidation.js.map +2 -2
  283. package/dist/esm/registration/hooks/useSubscriptions.d.ts +15 -0
  284. package/dist/esm/registration/hooks/useSubscriptions.d.ts.map +1 -0
  285. package/dist/esm/registration/hooks/useSubscriptions.js +2 -0
  286. package/dist/esm/registration/hooks/useSubscriptions.js.map +7 -0
  287. package/dist/esm/registration/index.d.ts +0 -1
  288. package/dist/esm/registration/index.d.ts.map +1 -1
  289. package/dist/esm/registration/index.js +1 -1
  290. package/dist/esm/registration/index.js.map +2 -2
  291. package/dist/esm/shopify/fetchers/index.d.ts +2 -0
  292. package/dist/esm/shopify/fetchers/index.d.ts.map +1 -1
  293. package/dist/esm/shopify/fetchers/index.js +1 -1
  294. package/dist/esm/shopify/fetchers/index.js.map +3 -3
  295. package/dist/registration/hooks/index.d.ts +4 -0
  296. package/dist/registration/hooks/index.d.ts.map +1 -0
  297. package/dist/registration/hooks/useEmailValidation.d.ts +17 -0
  298. package/dist/registration/hooks/useEmailValidation.d.ts.map +1 -0
  299. package/dist/registration/hooks/useSendEmailValidation.d.ts +9 -0
  300. package/dist/registration/hooks/useSendEmailValidation.d.ts.map +1 -0
  301. package/dist/registration/hooks/useSubscriptions.d.ts +15 -0
  302. package/dist/registration/hooks/useSubscriptions.d.ts.map +1 -0
  303. package/dist/registration/index.d.ts +2 -0
  304. package/dist/registration/index.d.ts.map +1 -0
  305. package/dist/utils/classnames.d.ts +3 -0
  306. package/dist/utils/classnames.d.ts.map +1 -0
  307. package/dist/utils/fetcher/fetchResponse.d.ts +14 -0
  308. package/dist/utils/fetcher/fetchResponse.d.ts.map +1 -0
  309. package/dist/utils/fetcher/fetcher.d.ts +2 -0
  310. package/dist/utils/fetcher/fetcher.d.ts.map +1 -0
  311. package/dist/utils/fetcher/index.d.ts +3 -0
  312. package/dist/utils/fetcher/index.d.ts.map +1 -0
  313. package/dist/utils/index.d.ts +5 -0
  314. package/dist/utils/index.d.ts.map +1 -0
  315. package/dist/utils/track.d.ts +16 -0
  316. package/dist/utils/track.d.ts.map +1 -0
  317. package/dist/utils/useCountDown.d.ts +6 -0
  318. package/dist/utils/useCountDown.d.ts.map +1 -0
  319. package/esbuild-esm.mjs +1 -0
  320. package/jest.config.ts +3 -0
  321. package/package.json +4 -5
  322. package/src/cart/context/index.tsx +310 -0
  323. package/src/cart/hooks/basic/useAddToCart.ts +161 -0
  324. package/src/cart/hooks/basic/useApplyCartCodes.ts +72 -0
  325. package/src/cart/hooks/basic/useBuyNow.ts +10 -7
  326. package/src/cart/hooks/basic/useCreateCart.ts +32 -0
  327. package/src/cart/hooks/basic/useRemoveCartCodes.ts +55 -0
  328. package/src/cart/hooks/basic/useRemoveCartLines.ts +51 -0
  329. package/src/cart/hooks/basic/useUpdateCartAttributes.ts +42 -0
  330. package/src/cart/hooks/basic/useUpdateCartBuyerIdentity.ts +34 -0
  331. package/src/cart/hooks/basic/useUpdateCartLines.ts +34 -0
  332. package/src/cart/hooks/feature/useCalcAutoFreeGift.tsx +178 -0
  333. package/src/cart/hooks/feature/useCalcOrderDiscount.tsx +127 -0
  334. package/src/cart/hooks/feature/useCartAttributes.tsx +133 -0
  335. package/src/cart/hooks/feature/useCartItemQuantityLimit.tsx +59 -0
  336. package/src/cart/hooks/feature/useScriptAutoFreeGift.tsx +256 -0
  337. package/src/cart/hooks/feature/useUpdateLineCodeAmountAttributes.tsx +184 -0
  338. package/src/cart/track/fbq.ts +4 -29
  339. package/src/cart/track/gtm.ts +159 -0
  340. package/src/cart/types/autoFreeGift.ts +169 -0
  341. package/src/cart/types/index.ts +1 -0
  342. package/src/cart/types/orderDiscount.ts +38 -0
  343. package/src/cart/types/priceDiscount.ts +21 -0
  344. package/src/cart/utils/autoFreeGift.ts +297 -0
  345. package/src/cart/utils/index.ts +13 -0
  346. package/src/constants.ts +1 -0
  347. package/src/context/index.ts +0 -1
  348. package/src/index.ts +6 -0
  349. package/src/registration/hooks/index.ts +3 -0
  350. package/src/registration/hooks/useEmailValidation.ts +28 -0
  351. package/src/registration/hooks/useSendEmailValidation.ts +21 -0
  352. package/src/registration/hooks/useSubscriptions.ts +36 -0
  353. package/src/registration/index.ts +2 -0
  354. package/src/shopify/fetchers/index.ts +2 -0
  355. package/src/utils/classnames.ts +6 -0
  356. package/src/utils/fetcher/fetchResponse.ts +33 -0
  357. package/src/utils/fetcher/fetcher.ts +38 -0
  358. package/src/utils/fetcher/index.ts +2 -0
  359. package/src/utils/index.ts +4 -0
  360. package/src/utils/track.ts +57 -0
  361. package/src/utils/useCountDown.tsx +41 -0
  362. package/tsconfig.json +4 -0
  363. package/tsup.config.ts +10 -8
  364. package/dist/cjs/registration/context/index.d.ts +0 -24
  365. package/dist/cjs/registration/context/index.d.ts.map +0 -1
  366. package/dist/cjs/registration/context/index.js +0 -2
  367. package/dist/cjs/registration/context/index.js.map +0 -7
  368. package/dist/esm/registration/context/index.d.ts +0 -24
  369. package/dist/esm/registration/context/index.d.ts.map +0 -1
  370. package/dist/esm/registration/context/index.js +0 -2
  371. package/dist/esm/registration/context/index.js.map +0 -7
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fetcher.d.ts","sourceRoot":"","sources":["../../../src/utils/fetcher/fetcher.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,OAAO,GAAU,mEAS3B,GAAG,iBA0BL,CAAA"}
@@ -0,0 +1,3 @@
1
+ export * from './fetcher';
2
+ export * from './fetchResponse';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/utils/fetcher/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAA;AACzB,cAAc,iBAAiB,CAAA"}
@@ -0,0 +1,5 @@
1
+ export * from './classnames';
2
+ export * from './fetcher';
3
+ export * from './track';
4
+ export * from './useCountDown';
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAA;AAC5B,cAAc,WAAW,CAAA;AACzB,cAAc,SAAS,CAAA;AACvB,cAAc,gBAAgB,CAAA"}
@@ -0,0 +1,16 @@
1
+ declare global {
2
+ interface Window {
3
+ dataLayer?: Array<any>;
4
+ fbq?: (...args: any[]) => void;
5
+ klaviyo?: any;
6
+ gtag?: (...args: any[]) => void;
7
+ }
8
+ }
9
+ export declare const gaTrack: (data: any) => "" | undefined;
10
+ export declare function gaNormalClick({ position, label }: {
11
+ position: string;
12
+ label: string;
13
+ }): void;
14
+ export declare const fbq: (...args: any[]) => "" | undefined;
15
+ export declare const gtag: (...args: any[]) => "" | undefined;
16
+ //# sourceMappingURL=track.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"track.d.ts","sourceRoot":"","sources":["../../src/utils/track.ts"],"names":[],"mappings":"AAAA,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,MAAM;QACd,SAAS,CAAC,EAAE,KAAK,CAAC,GAAG,CAAC,CAAA;QACtB,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAA;QAC9B,OAAO,CAAC,EAAE,GAAG,CAAA;QACb,IAAI,CAAC,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAA;KAChC;CACF;AAGD,eAAO,MAAM,OAAO,GAAI,MAAM,GAAG,mBAehC,CAAA;AAED,wBAAgB,aAAa,CAAC,EAAE,QAAQ,EAAE,KAAK,EAAE,EAAE;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,QAUrF;AAGD,eAAO,MAAM,GAAG,GAAI,GAAG,MAAM,GAAG,EAAE,mBAOjC,CAAA;AAED,eAAO,MAAM,IAAI,GAAI,GAAG,MAAM,GAAG,EAAE,mBAOlC,CAAA"}
@@ -0,0 +1,6 @@
1
+ export declare function useCountDown(countDownTime: number, onEnd?: () => void): {
2
+ startCountDown: boolean;
3
+ startAction: () => void;
4
+ countDown: number;
5
+ };
6
+ //# sourceMappingURL=useCountDown.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useCountDown.d.ts","sourceRoot":"","sources":["../../src/utils/useCountDown.tsx"],"names":[],"mappings":"AAEA,wBAAgB,YAAY,CAAC,aAAa,EAAE,MAAM,EAAE,KAAK,aAAW;;;;EAsCnE"}
package/esbuild-esm.mjs CHANGED
@@ -1,4 +1,5 @@
1
1
  import esbuild from 'esbuild'
2
+
2
3
  const dir = 'dist/esm'
3
4
 
4
5
  const options = {
package/jest.config.ts CHANGED
@@ -7,6 +7,9 @@ const config: JestConfigWithTsJest = {
7
7
  coverageDirectory: 'coverage', // 输出覆盖率报告
8
8
  testMatch: ['**/__tests__/**/*.test.ts'], // 匹配测试文件路径
9
9
  moduleFileExtensions: ['ts', 'js', 'json'],
10
+ moduleNameMapper: {
11
+ '^src/(.*)$': '<rootDir>/src/$1',
12
+ },
10
13
  }
11
14
 
12
15
  export default config
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anker-in/lib",
3
- "version": "1.1.0",
3
+ "version": "1.1.2-beta.1",
4
4
  "description": "Unified Anker headless library with shared context, Shopify integration, and cart utilities",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "module": "./dist/esm/index.js",
@@ -18,8 +18,6 @@
18
18
  }
19
19
  },
20
20
  "peerDependencies": {
21
- "react": "^18.0.0",
22
- "react-dom": "^18.0.0",
23
21
  "tailwind-merge": "^1.11.0",
24
22
  "clsx": "^2.1.1"
25
23
  },
@@ -54,10 +52,11 @@
54
52
  "license": "MIT",
55
53
  "scripts": {
56
54
  "dev": "tsup --watch",
57
- "build": "pnpm run build:js:esm & pnpm run build:js:cjs & pnpm run build:types",
55
+ "build": "pnpm run build:js:esm & pnpm run build:js:cjs & pnpm run build:types:esm & pnpm run build:types:cjs",
58
56
  "build:js:cjs": "node esbuild-cjs.mjs",
59
57
  "build:js:esm": "node esbuild-esm.mjs",
60
- "build:types": "tsc --outdir dist",
58
+ "build:types:esm": "tsc --outdir dist/esm",
59
+ "build:types:cjs": "tsc --outdir dist/cjs",
61
60
  "test": "jest --passWithNoTests",
62
61
  "test:watch": "jest --watch",
63
62
  "coverage": "jest --coverage",
@@ -0,0 +1,310 @@
1
+ import React, { createContext, useContext, useState, useMemo, useEffect, useCallback } from 'react'
2
+ import { Cart } from '../../shopify/types/cart'
3
+
4
+ import { AutoFreeGiftConfig, FunctionGiftResult, AutoFreeGiftList } from '../types/autoFreeGift'
5
+ import { useCalcAutoFreeGift } from '../hooks/feature/useCalcAutoFreeGift'
6
+ import { getCart } from '../../shopify/fetchers/get-cart'
7
+ import { useRequest } from 'ahooks'
8
+ import { useHeadlessContext } from '../../context'
9
+ import { useUpdateCartAttributes } from '../hooks/basic/useUpdateCartAttributes'
10
+ import { useCartAttributes } from '../hooks/feature/useCartAttributes'
11
+ import { AttributeInput } from '../../shopify/gql'
12
+ import { isAttributesEqual } from '../utils'
13
+ import { useUpdateLineCodeAmountAttributes } from '../hooks/feature/useUpdateLineCodeAmountAttributes'
14
+ import useScriptAutoFreeGift, { UseScriptAutoFreeGiftResult } from '../hooks/feature/useScriptAutoFreeGift'
15
+ import {
16
+ formatScriptAutoFreeGift,
17
+ formatFunctionAutoFreeGift,
18
+ getDiscountEnvAttributeValue,
19
+ } from '../utils/autoFreeGift'
20
+ import { CUSTOMER_ATTRIBUTE_KEY, CUSTOMER_SCRIPT_GIFT_KEY } from '../../shopify/utils/const'
21
+
22
+ export type LoadingState = {
23
+ editLineQuantityLoading: boolean
24
+ editLineCodeAmountLoading: boolean
25
+ listingAutoCodeApplying: boolean
26
+ userCodeApplying: boolean
27
+ }
28
+
29
+ type CartContextType = {
30
+ cart: Cart | undefined
31
+ isCartLoading: boolean
32
+ mutateCart: (cart: Cart | undefined) => void
33
+ triggerFetch: () => void
34
+ calculatedAutoFreeGift?: FunctionGiftResult | undefined
35
+ addCustomAttributes: (attributes: AttributeInput[]) => void
36
+ removeCustomAttributes: (attributes: { key: string }[]) => void
37
+ setCustomAttributes: (attributes: AttributeInput[]) => void
38
+ isCodeChanging: boolean
39
+ setIsCodeChanging: (isCodeChanging: boolean) => void
40
+ autoFreeGiftConfig?: AutoFreeGiftConfig
41
+ setLoadingState: React.Dispatch<React.SetStateAction<LoadingState>>
42
+ loadingState: LoadingState
43
+ scriptAutoFreeGiftResult?: UseScriptAutoFreeGiftResult
44
+ scriptAutoFreeGift?: AutoFreeGiftList
45
+ functionAutoFreeGiftResult?: FunctionGiftResult
46
+ functionAutoFreeGift?: AutoFreeGiftList
47
+ setFunctionAutoFreeGift: (functionAutoFreeGift: AutoFreeGiftList) => void
48
+ setScriptAutoFreeGift: (scriptAutoFreeGift: AutoFreeGiftList) => void
49
+ giftNeedAddToCartLines: AutoFreeGiftList
50
+ }
51
+
52
+ const initialState: CartContextType = {
53
+ cart: undefined,
54
+ isCartLoading: false,
55
+ mutateCart: () => {},
56
+ triggerFetch: () => {},
57
+ calculatedAutoFreeGift: undefined,
58
+ addCustomAttributes: () => {},
59
+ removeCustomAttributes: () => {},
60
+ setCustomAttributes: () => {},
61
+ isCodeChanging: false,
62
+ setIsCodeChanging: () => {},
63
+ setLoadingState: () => {},
64
+ loadingState: {
65
+ editLineQuantityLoading: false,
66
+ editLineCodeAmountLoading: false,
67
+ listingAutoCodeApplying: false,
68
+ userCodeApplying: false,
69
+ },
70
+ scriptAutoFreeGift: undefined,
71
+ functionAutoFreeGift: undefined,
72
+ scriptAutoFreeGiftResult: undefined,
73
+ functionAutoFreeGiftResult: undefined,
74
+ setFunctionAutoFreeGift: () => {},
75
+ setScriptAutoFreeGift: () => {},
76
+ giftNeedAddToCartLines: [],
77
+ }
78
+
79
+ export const CartContext = createContext<CartContextType>(initialState)
80
+
81
+ type CartProviderProps = {
82
+ children: React.ReactNode
83
+ autoFreeGiftConfig?: AutoFreeGiftConfig
84
+ gradientGiftsConfig: any
85
+ profile: any
86
+ customer: any
87
+ }
88
+
89
+ export const CartProvider = ({
90
+ children,
91
+ autoFreeGiftConfig,
92
+ gradientGiftsConfig,
93
+ profile,
94
+ customer,
95
+ }: CartProviderProps) => {
96
+ const { storefrontToken, storeDomain, cartIdCookieName, locale } = useHeadlessContext()
97
+ const [customAttributes, setCustomAttributes] = useState<AttributeInput[]>([])
98
+ const [isCodeChanging, setIsCodeChanging] = useState(false)
99
+ const [loadingState, setLoadingState] = useState<LoadingState>({
100
+ editLineQuantityLoading: false,
101
+ editLineCodeAmountLoading: false,
102
+ listingAutoCodeApplying: false,
103
+ userCodeApplying: false,
104
+ })
105
+ const [scriptAutoFreeGift, setScriptAutoFreeGift] = useState<AutoFreeGiftList>([])
106
+ const [functionAutoFreeGift, setFunctionAutoFreeGift] = useState<AutoFreeGiftList>([])
107
+
108
+ // 获取购物车逻辑
109
+ const {
110
+ run: fetchCart,
111
+ data: cart,
112
+ loading: isCartLoading,
113
+ mutate: mutateCart,
114
+ } = useRequest(
115
+ async () => {
116
+ // 从 cookie 获取 cartId
117
+ const cartId =
118
+ typeof window !== 'undefined'
119
+ ? document.cookie
120
+ .split('; ')
121
+ .find(row => row.startsWith(`${cartIdCookieName}=`))
122
+ ?.split('=')[1]
123
+ : undefined
124
+
125
+ if (!cartId) {
126
+ return undefined
127
+ }
128
+
129
+ return getCart({
130
+ options: { cartId },
131
+ config: {
132
+ storefrontToken,
133
+ storeDomain,
134
+ },
135
+ })
136
+ },
137
+ {
138
+ refreshDeps: [locale, storefrontToken, storeDomain, cartIdCookieName],
139
+ }
140
+ ) as unknown as {
141
+ run: () => Promise<Cart | undefined>
142
+ mutate: (cart: Cart | undefined) => void
143
+ data: Cart | undefined
144
+ loading: boolean
145
+ }
146
+
147
+ // 更新 attributes
148
+ const { trigger: updateAttributes } = useUpdateCartAttributes()
149
+ // 公共 attributes
150
+ const { attributes } = useCartAttributes({ profile, customer, cart })
151
+
152
+ useRequest(
153
+ async () => {
154
+ // 去重逻辑
155
+ const newAttributes = [...attributes, ...customAttributes]
156
+
157
+ const needUpdate = cart && !isAttributesEqual(cart.customAttributes, newAttributes)
158
+
159
+ // 如果 attributes 没有变化,不更新
160
+ if (needUpdate && cart) {
161
+ const updatedCart = await updateAttributes({
162
+ cartId: cart.id,
163
+ attributes: newAttributes,
164
+ })
165
+ if (updatedCart) {
166
+ mutateCart(updatedCart)
167
+ }
168
+ return updatedCart
169
+ } else {
170
+ return Promise.resolve(cart)
171
+ }
172
+ },
173
+ {
174
+ throttleWait: 1000, // 1 秒内只触发最后一次更新
175
+ throttleTrailing: true,
176
+ throttleLeading: false,
177
+ refreshDeps: [attributes, customAttributes, cart?.id],
178
+ }
179
+ )
180
+
181
+ // 更新 code 时,同步更新 line item 中的 code amount attribute
182
+ useUpdateLineCodeAmountAttributes({
183
+ cart,
184
+ mutateCart,
185
+ isCartLoading: isCartLoading || isCodeChanging,
186
+ setLoadingState,
187
+ })
188
+
189
+ // 计算购物车赠品 - 只计算,不执行副作用
190
+ const functionAutoFreeGiftResult = useCalcAutoFreeGift(cart, autoFreeGiftConfig || [], customer)
191
+ const scriptAutoFreeGiftResult = useScriptAutoFreeGift({
192
+ campaign: gradientGiftsConfig,
193
+ _giveaway: CUSTOMER_SCRIPT_GIFT_KEY,
194
+ cart,
195
+ })
196
+
197
+ const formattedScriptGifts = useMemo(() => {
198
+ return formatScriptAutoFreeGift({
199
+ scriptAutoFreeGiftResult,
200
+ gradient_gifts: gradientGiftsConfig,
201
+ locale,
202
+ })
203
+ }, [scriptAutoFreeGiftResult, gradientGiftsConfig, locale])
204
+
205
+ const formattedFunctionGifts = useMemo(() => {
206
+ return formatFunctionAutoFreeGift({
207
+ qualifyingGift: functionAutoFreeGiftResult?.qualifyingGift || null,
208
+ giftProductsResult: (functionAutoFreeGiftResult?.giftProductsResult as any[]) || [],
209
+ locale,
210
+ })
211
+ }, [functionAutoFreeGiftResult, locale])
212
+
213
+ // 当格式化结果变化时更新状态
214
+ useEffect(() => {
215
+ if (
216
+ loadingState.userCodeApplying ||
217
+ loadingState.editLineQuantityLoading ||
218
+ loadingState.editLineCodeAmountLoading ||
219
+ loadingState.listingAutoCodeApplying
220
+ ) {
221
+ return
222
+ }
223
+
224
+ setScriptAutoFreeGift(formattedScriptGifts)
225
+ setFunctionAutoFreeGift(formattedFunctionGifts)
226
+ }, [
227
+ formattedScriptGifts,
228
+ formattedFunctionGifts,
229
+ loadingState.userCodeApplying,
230
+ loadingState.editLineQuantityLoading,
231
+ loadingState.editLineCodeAmountLoading,
232
+ loadingState.listingAutoCodeApplying,
233
+ ])
234
+
235
+ const giftNeedAddToCartLines = useMemo(() => {
236
+ const cartGiftLineItems = cart?.lineItems.filter(line =>
237
+ line.customAttributes?.some((attr: any) => [CUSTOMER_ATTRIBUTE_KEY, CUSTOMER_SCRIPT_GIFT_KEY].includes(attr.key))
238
+ )
239
+ const result = [...scriptAutoFreeGift, ...functionAutoFreeGift].filter(
240
+ item =>
241
+ // 如果购物车中已经存在对应赠品,且标记了 function 满赠的属性,则不添加
242
+ !cartGiftLineItems?.find(line => {
243
+ return (
244
+ line.variantId === item.line.variantId &&
245
+ line.customAttributes?.some((attr: any) => {
246
+ if (attr.key === '_discounts_function_env') {
247
+ return getDiscountEnvAttributeValue(line.customAttributes).is_gift
248
+ }
249
+
250
+ return false
251
+ })
252
+ )
253
+ })
254
+ )
255
+ return result
256
+ }, [cart?.lineItems, scriptAutoFreeGift, functionAutoFreeGift])
257
+
258
+ const removeCustomAttributes = useCallback((attributes: { key: string }[]) => {
259
+ setCustomAttributes(prev => prev.filter((attr: AttributeInput) => !attributes.some(a => a.key === attr.key)))
260
+ }, [])
261
+
262
+ const addCustomAttributes = useCallback(
263
+ (attributes: AttributeInput[]) => {
264
+ const sameAttributes = attributes.filter(attr => customAttributes.some(a => a.key === attr.key))
265
+ if (sameAttributes.length) {
266
+ setCustomAttributes(prev => {
267
+ // 更新相同的属性
268
+ const removedAttributes = prev.filter((attr: AttributeInput) => !sameAttributes.some(a => a.key === attr.key))
269
+ return [...removedAttributes, ...attributes]
270
+ })
271
+ } else {
272
+ // 添加新的属性
273
+ setCustomAttributes(prev => [...prev, ...attributes])
274
+ }
275
+ },
276
+ [customAttributes]
277
+ )
278
+
279
+ return (
280
+ <CartContext.Provider
281
+ value={{
282
+ cart,
283
+ mutateCart,
284
+ triggerFetch: fetchCart,
285
+ isCartLoading,
286
+ setCustomAttributes,
287
+ addCustomAttributes: addCustomAttributes,
288
+ removeCustomAttributes: removeCustomAttributes,
289
+ calculatedAutoFreeGift: functionAutoFreeGiftResult || scriptAutoFreeGiftResult,
290
+ isCodeChanging,
291
+ setIsCodeChanging,
292
+ setLoadingState,
293
+ loadingState,
294
+ // function满赠
295
+ functionAutoFreeGift,
296
+ functionAutoFreeGiftResult,
297
+ setFunctionAutoFreeGift,
298
+ // script满赠
299
+ scriptAutoFreeGift,
300
+ scriptAutoFreeGiftResult,
301
+ setScriptAutoFreeGift,
302
+ giftNeedAddToCartLines,
303
+ }}
304
+ >
305
+ {children}
306
+ </CartContext.Provider>
307
+ )
308
+ }
309
+
310
+ export const useCartContext = () => useContext(CartContext)
@@ -0,0 +1,161 @@
1
+ import { Cart, Attributes } from '../../../shopify'
2
+ import { CartBuyerIdentityInput } from '../../../shopify/gql'
3
+ import { useCallback } from 'react'
4
+ import useSWRMutation, { SWRMutationConfiguration } from 'swr/mutation'
5
+ import { GtmParams } from '../../types'
6
+ // import { startInactiveSpan } from '@sentry/nextjs'
7
+ import { addCartLines as addCartLinesFetcher, createCart as createCartFetcher } from '../../../shopify'
8
+ import { useHeadlessContext } from '../../../context'
9
+ import { defaultSWRMutationConfiguration } from '../../const'
10
+ import { addToCartFbq } from '../../track/fbq'
11
+ import { addToCartGa } from '../../track/gtm'
12
+
13
+ type AddToCartLineItem = {
14
+ variant?: { id: string }
15
+ variantId?: string
16
+ quantity?: number
17
+ attributes?: Attributes
18
+ }
19
+
20
+ export type AddToCartArgs = {
21
+ lineItems: AddToCartLineItem[]
22
+ cartId?: string
23
+ discountCodes?: string[]
24
+ customAttributes?: Attributes
25
+ buyerIdentity?: CartBuyerIdentityInput
26
+ gtmParams?: Exclude<GtmParams, 'brand'>
27
+ needCreateCart?: boolean // 是否强制创建新购物车
28
+ onCodesInvalid?: (updatedCart: Cart, invalidCodes: string[]) => Promise<Cart | undefined> // 折扣码不可用时的回调
29
+ }
30
+
31
+ type UseAddToCartProps = {
32
+ withTrack?: boolean // 是否需要默认埋点(暂时注释,需要时可以启用)
33
+ }
34
+
35
+ export const useAddToCart = (
36
+ { withTrack = true }: UseAddToCartProps = {},
37
+ swrOptions: SWRMutationConfiguration<Cart | undefined, Error, string, AddToCartArgs> & {
38
+ throwOnError?: boolean
39
+ } = defaultSWRMutationConfiguration
40
+ ) => {
41
+ const { storefrontToken, storeDomain, brand } = useHeadlessContext()
42
+
43
+ const trigger = useCallback(
44
+ async (_: any, { arg }: { arg: AddToCartArgs }) => {
45
+ const {
46
+ lineItems,
47
+ cartId,
48
+ discountCodes,
49
+ gtmParams = {},
50
+ buyerIdentity,
51
+ needCreateCart = false,
52
+ onCodesInvalid,
53
+ } = arg
54
+
55
+ if (!lineItems || lineItems.length === 0) {
56
+ return
57
+ }
58
+
59
+ // const addToCartSpan = startInactiveSpan({
60
+ // name: 'addToCart',
61
+ // op: 'business',
62
+ // })
63
+
64
+ // 准备要添加的商品数据
65
+ const linesToAdd = lineItems
66
+ .map(item => ({
67
+ merchandiseId: item.variant?.id || item.variantId || '',
68
+ quantity: item.quantity || 1,
69
+ attributes: item.attributes,
70
+ }))
71
+ .filter(item => item.merchandiseId && item.quantity)
72
+
73
+ let resultCart: Cart | undefined
74
+
75
+ if (linesToAdd.length > 0) {
76
+ // const addToCartAPISpan = startInactiveSpan({
77
+ // name: 'addToCartAPI',
78
+ // op: 'business',
79
+ // parentSpan: addToCartSpan,
80
+ // })
81
+
82
+ if (!cartId || needCreateCart) {
83
+ // 创建新购物车
84
+ // const createCartApiSpan = startInactiveSpan({
85
+ // name: 'createCartAPI',
86
+ // op: 'business',
87
+ // parentSpan: addToCartAPISpan,
88
+ // })
89
+
90
+ resultCart = await createCartFetcher({
91
+ options: {
92
+ lines: linesToAdd,
93
+ buyerIdentity,
94
+ discountCodes,
95
+ },
96
+ config: {
97
+ storefrontToken,
98
+ storeDomain,
99
+ },
100
+ })
101
+
102
+ // createCartApiSpan.end()
103
+ } else {
104
+ // 添加到现有购物车
105
+ // const addCartLinesApiSpan = startInactiveSpan({
106
+ // name: 'addCartLinesAPI',
107
+ // op: 'business',
108
+ // parentSpan: addToCartAPISpan,
109
+ // })
110
+
111
+ resultCart = await addCartLinesFetcher({
112
+ options: {
113
+ lines: linesToAdd,
114
+ cartId,
115
+ },
116
+ config: {
117
+ storefrontToken,
118
+ storeDomain,
119
+ },
120
+ })
121
+
122
+ // addCartLinesApiSpan.end()
123
+ }
124
+
125
+ // addToCartAPISpan.end()
126
+ }
127
+
128
+ // 检查是否有不可用的折扣码
129
+ if (resultCart && onCodesInvalid) {
130
+ const unApplicableCodes = resultCart.discountCodes
131
+ ?.filter((item) => !item.applicable)
132
+ .map((item) => item.code)
133
+
134
+ if (unApplicableCodes?.length) {
135
+ // const removeCartCodesApiSpan = startInactiveSpan({
136
+ // name: 'removeCartCodesAPI',
137
+ // op: 'business',
138
+ // parentSpan: addToCartSpan,
139
+ // })
140
+
141
+ resultCart = (await onCodesInvalid(resultCart, unApplicableCodes)) || resultCart
142
+
143
+ // removeCartCodesApiSpan.end()
144
+ }
145
+ }
146
+
147
+ // 埋点(需要时可以启用)
148
+ if (withTrack) {
149
+ addToCartFbq({ lineItems: resultCart?.lineItems || [] })
150
+ addToCartGa({ lineItems: resultCart?.lineItems || [], gtmParams: { ...gtmParams, brand } })
151
+ }
152
+
153
+ // addToCartSpan.end()
154
+
155
+ return resultCart
156
+ },
157
+ [storefrontToken, storeDomain]
158
+ )
159
+
160
+ return useSWRMutation('add-to-cart', trigger, swrOptions)
161
+ }
@@ -0,0 +1,72 @@
1
+ import { Cart } from '../../../shopify'
2
+ import { useCallback } from 'react'
3
+ import useSWRMutation, { SWRMutationConfiguration } from 'swr/mutation'
4
+ import { MutationCartDiscountCodesUpdateArgs } from '../../../shopify/gql'
5
+ // import { startInactiveSpan } from '@sentry/nextjs'
6
+ import { updateCartCodes as updateCartCodesFetcher } from '../../../shopify'
7
+ import { useHeadlessContext } from '../../../context'
8
+ import { defaultSWRMutationConfiguration } from '../../const'
9
+
10
+ export type ApplyCartCodesArgs = {
11
+ cartId: string
12
+ discountCodes: string[]
13
+ currentCart?: Cart // 当前购物车状态,用于合并已有的折扣码
14
+ }
15
+
16
+ export const useApplyCartCodes = (
17
+ swrOptions: SWRMutationConfiguration<Cart | undefined, Error, string, ApplyCartCodesArgs> & {
18
+ throwOnError?: boolean
19
+ } = defaultSWRMutationConfiguration
20
+ ) => {
21
+ const { storefrontToken, storeDomain } = useHeadlessContext()
22
+
23
+ const trigger = useCallback(
24
+ async (_: any, { arg }: { arg: ApplyCartCodesArgs }) => {
25
+ const { cartId, discountCodes, currentCart } = arg
26
+
27
+ if (!discountCodes?.length) {
28
+ throw new Error('Invalid input used for this operation: Miss discountCode')
29
+ }
30
+
31
+ if (!cartId) {
32
+ throw new Error('cartId is required')
33
+ }
34
+
35
+ // 合并当前已有的可用折扣码
36
+ let existingCodes = currentCart?.discountCodes?.filter(code => code.applicable) || []
37
+ const uniqueCodes = [...new Set([...existingCodes.map(code => code.code), ...discountCodes])]
38
+
39
+ // const applyCartCodesApiSpan = startInactiveSpan({
40
+ // name: 'applyCartCodesAPI',
41
+ // op: 'business',
42
+ // })
43
+
44
+ const updatedCart = await updateCartCodesFetcher({
45
+ options: {
46
+ cartId,
47
+ discountCodes: uniqueCodes,
48
+ },
49
+ config: {
50
+ storefrontToken,
51
+ storeDomain,
52
+ },
53
+ })
54
+
55
+ // applyCartCodesApiSpan.end()
56
+
57
+ // 检查新添加的折扣码是否有不可用的
58
+ const unApplicableCodes = discountCodes.filter((code) =>
59
+ updatedCart?.discountCodes?.find((item) => item.code === code && !item.applicable)
60
+ )
61
+
62
+ if (unApplicableCodes.length) {
63
+ throw new Error(`${unApplicableCodes.join(', ')} is not applicable to the cart`)
64
+ }
65
+
66
+ return updatedCart
67
+ },
68
+ [storefrontToken, storeDomain]
69
+ )
70
+
71
+ return useSWRMutation('apply-cart-codes', trigger, swrOptions)
72
+ }