@redotech/redo-hydrogen 1.1.1 → 1.1.3

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/dist/types.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { CartReturn } from '@shopify/hydrogen';
2
2
  import { ReactNode, DependencyList } from 'react';
3
+ import { CartWithActionsDocs } from '@shopify/hydrogen-react/dist/types/cart-types';
3
4
  import { ProductVariant } from '@shopify/hydrogen-react/storefront-api-types';
4
5
  import * as react_jsx_runtime from 'react/jsx-runtime';
5
6
 
@@ -13,7 +14,7 @@ interface RedoCoverageClient {
13
14
  get eligible(): boolean;
14
15
  get price(): number | undefined;
15
16
  get storeId(): string | undefined;
16
- get cart(): CartReturn | undefined;
17
+ get cart(): CartReturn | CartWithActionsDocs | undefined;
17
18
  get cartProduct(): CartProductVariantFragment | undefined;
18
19
  get cartAttribute(): CartAttributeKey | undefined;
19
20
  get errors(): RedoError[] | undefined;
@@ -29,7 +30,7 @@ type RedoContextValue = {
29
30
  loading: boolean;
30
31
  storeId?: string;
31
32
  cartInfoToEnable?: CartInfoToEnable;
32
- cart?: CartReturn;
33
+ cart?: CartReturn | CartWithActionsDocs;
33
34
  errors?: RedoError[];
34
35
  };
35
36
  declare enum RedoErrorType {
@@ -44,14 +45,14 @@ type RedoError = {
44
45
  };
45
46
 
46
47
  declare const RedoProvider: ({ cart, storeId, children }: {
47
- cart: CartReturn;
48
+ cart: CartReturn | CartWithActionsDocs;
48
49
  storeId: string;
49
50
  children: ReactNode;
50
51
  }) => ReactNode;
51
52
  declare const useRedoCoverageClient: () => RedoCoverageClient;
52
53
 
53
54
  declare const RedoCheckoutButtons: (props: {
54
- cart: CartReturn;
55
+ cart: CartReturn | CartWithActionsDocs;
55
56
  children?: ReactNode;
56
57
  onClick?: (enabled: boolean) => void;
57
58
  }) => react_jsx_runtime.JSX.Element;
@@ -68,4 +69,4 @@ interface LoadState<T> {
68
69
  }
69
70
  declare function useLoad<T>(fn: Loader<T>, deps: DependencyList): LoadState<T>;
70
71
 
71
- export { type CartAttributeKey, type CartInfoToEnable, type CartProductVariantFragment, type LoadState, type Loader, REDO_REQUIRED_HOSTNAMES, RedoCheckoutButtons, type RedoContextValue, type RedoCoverageClient, RedoProvider, useLoad, useRedoCoverageClient };
72
+ export { type CartAttributeKey, type CartInfoToEnable, type CartProductVariantFragment, type LoadState, type Loader, REDO_REQUIRED_HOSTNAMES, RedoCheckoutButtons, type RedoContextValue, type RedoCoverageClient, type RedoError, RedoErrorType, RedoProvider, useLoad, useRedoCoverageClient };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@redotech/redo-hydrogen",
3
- "version": "1.1.1",
3
+ "version": "1.1.3",
4
4
  "description": "Utilities to enable and disable Redo coverage on Hydrogen stores",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -8,6 +8,8 @@ import { useRedoCoverageClient } from "../providers/redo-coverage-client";
8
8
  import { CartInfoToEnable, RedoCoverageClient } from "../types";
9
9
  import { REDO_PUBLIC_API_HOSTNAME } from "../utils/security";
10
10
  import { CurrencyCode } from "@shopify/hydrogen-react/storefront-api-types";
11
+ import { CartWithActionsDocs } from "@shopify/hydrogen-react/dist/types/cart-types";
12
+ import { getCartLines, isCartWithActionsDocs } from "../utils/cart";
11
13
 
12
14
  type CheckoutButtonUIResponse = {
13
15
  html: string;
@@ -20,7 +22,7 @@ const getButtonsToShow = ({
20
22
  storeId
21
23
  }: {
22
24
  redoCoverageClient: RedoCoverageClient,
23
- cart: CartReturn,
25
+ cart: CartReturn | CartWithActionsDocs,
24
26
  storeId: string;
25
27
  }): Promise<CheckoutButtonUIResponse | null> => {
26
28
  return new Promise<CheckoutButtonUIResponse | null>((resolve, reject) => {
@@ -60,10 +62,10 @@ const applyButtonVariables = ({
60
62
  ui
61
63
  }: {
62
64
  redoCoverageClient: RedoCoverageClient,
63
- cart: CartReturn,
65
+ cart: CartReturn | CartWithActionsDocs,
64
66
  ui: CheckoutButtonUIResponse
65
67
  }): CheckoutButtonUIResponse | null => {
66
- if(!redoCoverageClient.eligible || !redoCoverageClient.price) {
68
+ if(!redoCoverageClient.eligible || !redoCoverageClient.price || !cart?.cost) {
67
69
  return null;
68
70
  }
69
71
 
@@ -72,7 +74,7 @@ const applyButtonVariables = ({
72
74
  currencyCode = 'USD';
73
75
  }
74
76
 
75
- const cartContainsRedo = !!(cart.lines.nodes.some((cartItem) => cartItem.merchandise?.product?.vendor === 're:do'));
77
+ const cartContainsRedo = !!(getCartLines(cart).some((cartItem) => cartItem.merchandise?.product?.vendor === 're:do'));
76
78
  const combinedPrice = new Intl.NumberFormat('en-US', {
77
79
  style: 'currency',
78
80
  currency: currencyCode
@@ -101,7 +103,7 @@ const findAncestor = (
101
103
  };
102
104
 
103
105
  const RedoCheckoutButtons = (props: {
104
- cart: CartReturn;
106
+ cart: CartReturn | CartWithActionsDocs;
105
107
  children?: ReactNode;
106
108
  onClick?: (enabled: boolean) => void;
107
109
  }) => {
package/src/index.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { RedoProvider, useRedoCoverageClient } from "./providers/redo-coverage-client";
2
2
  import { RedoCheckoutButtons } from "./components/redo-checkout-buttons";
3
3
  import { REDO_REQUIRED_HOSTNAMES } from "./utils/security";
4
- import { CartProductVariantFragment, CartAttributeKey, CartInfoToEnable, RedoContextValue, RedoCoverageClient } from "./types";
4
+ import { CartProductVariantFragment, CartAttributeKey, CartInfoToEnable, RedoContextValue, RedoCoverageClient, RedoError, RedoErrorType } from "./types";
5
5
  import { LoadState, Loader, useLoad } from './utils/react-utils'
6
6
 
7
7
  export {
@@ -9,7 +9,8 @@ export {
9
9
  RedoProvider,
10
10
  useRedoCoverageClient,
11
11
  useLoad,
12
- REDO_REQUIRED_HOSTNAMES
12
+ REDO_REQUIRED_HOSTNAMES,
13
+ RedoErrorType
13
14
  };
14
15
 
15
16
  export type {
@@ -20,4 +21,5 @@ export type {
20
21
  RedoCoverageClient,
21
22
  LoadState,
22
23
  Loader,
24
+ RedoError
23
25
  };
@@ -3,7 +3,8 @@ import { CartReturn } from "@shopify/hydrogen";
3
3
  import { createContext, ReactNode, useContext, useEffect, useState } from "react";
4
4
  import { CartProductVariantFragment, CartAttributeKey, CartInfoToEnable, RedoContextValue, RedoCoverageClient, RedoError, RedoErrorType } from "../types";
5
5
  import { REDO_PUBLIC_API_HOSTNAME } from "../utils/security";
6
- import { addProductToCartIfNeeded, removeProductFromCartIfNeeded, setCartRedoEnabledAttribute, useFetcherWithPromise } from "../utils/cart";
6
+ import { addProductToCartIfNeeded, removeProductFromCartIfNeeded, setCartRedoEnabledAttribute, useFetcherWithPromise, isCartWithActionsDocs, getCartLines } from "../utils/cart";
7
+ import { CartWithActionsDocs } from "@shopify/hydrogen-react/dist/types/cart-types";
7
8
 
8
9
  const DEFAULT_REDO_CONTEXT_VALUE: RedoContextValue = {
9
10
  enabled: false,
@@ -17,7 +18,7 @@ const RedoProvider = ({
17
18
  storeId,
18
19
  children
19
20
  }: {
20
- cart: CartReturn,
21
+ cart: CartReturn | CartWithActionsDocs,
21
22
  storeId: string,
22
23
  children: ReactNode,
23
24
  }): ReactNode => {
@@ -40,6 +41,8 @@ const RedoProvider = ({
40
41
  return;
41
42
  }
42
43
 
44
+ let cartLines = getCartLines(cart);
45
+
43
46
  fetch(`https://${REDO_PUBLIC_API_HOSTNAME}/v2.2/stores/${storeId}/coverage-products`, {
44
47
  method: 'POST',
45
48
  headers: {
@@ -47,7 +50,7 @@ const RedoProvider = ({
47
50
  },
48
51
  body: JSON.stringify({
49
52
  cart: {
50
- lineItems: cart.lines.nodes.map((cartLine) => ({
53
+ lineItems: cartLines.map((cartLine) => ({
51
54
  id: cartLine.id,
52
55
  originalPrice: {
53
56
  amount: cartLine.merchandise?.price?.amount,
@@ -187,7 +190,7 @@ const useRedoCoverageClient = (): RedoCoverageClient => {
187
190
  return redoContext.loading;
188
191
  },
189
192
  get eligible() {
190
- return !this.loading && !!this.price && !!this.cartProduct;
193
+ return !this.loading && !!this.price && !!this.cartProduct && !!this.cart?.cost;
191
194
  },
192
195
  get enabled() {
193
196
  return redoContext.enabled;
package/src/types.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { CartReturn } from "@shopify/hydrogen";
2
+ import { CartWithActionsDocs } from "@shopify/hydrogen-react/dist/types/cart-types";
2
3
  import { ProductVariant } from "@shopify/hydrogen-react/storefront-api-types";
3
4
 
4
5
  type CartProductVariantFragment = Omit<ProductVariant,
@@ -15,7 +16,7 @@ interface RedoCoverageClient {
15
16
  get eligible(): boolean;
16
17
  get price(): number | undefined;
17
18
  get storeId(): string | undefined;
18
- get cart(): CartReturn | undefined;
19
+ get cart(): CartReturn | CartWithActionsDocs | undefined;
19
20
  get cartProduct(): CartProductVariantFragment | undefined;
20
21
  get cartAttribute(): CartAttributeKey | undefined;
21
22
  get errors(): RedoError[] | undefined;
@@ -33,7 +34,7 @@ type RedoContextValue = {
33
34
  loading: boolean,
34
35
  storeId?: string,
35
36
  cartInfoToEnable?: CartInfoToEnable,
36
- cart?: CartReturn,
37
+ cart?: CartReturn | CartWithActionsDocs,
37
38
  errors?: RedoError[],
38
39
  };
39
40
 
package/src/utils/cart.ts CHANGED
@@ -3,15 +3,29 @@ import { CartInfoToEnable } from "../types";
3
3
  import { CartForm, CartReturn } from "@shopify/hydrogen";
4
4
  import type { AppData } from '@remix-run/react/dist/data';
5
5
  import React from 'react'
6
+ import { CartWithActionsDocs } from "@shopify/hydrogen-react/dist/types/cart-types";
7
+ import { CartLine, ComponentizableCartLine } from "@shopify/hydrogen-react/storefront-api-types";
6
8
 
7
9
  const DEFAULT_REDO_ENABLED_CART_ATTRIBUTE = 'redo_opted_in_from_cart';
8
10
 
11
+ const isCartWithActionsDocs = (cart: CartReturn | CartWithActionsDocs): cart is CartWithActionsDocs => {
12
+ return (Array.isArray(cart.lines) && 'linesAdd' in cart && typeof cart.linesAdd === 'function');
13
+ }
14
+
15
+ const getCartLines = (cart: CartReturn | CartWithActionsDocs): Array<CartLine | ComponentizableCartLine> => {
16
+ if(isCartWithActionsDocs(cart)) {
17
+ return cart.lines;
18
+ } else {
19
+ return cart.lines.nodes ?? cart.lines.edges.map((edge) => edge.node);
20
+ }
21
+ }
22
+
9
23
  const addProductToCartIfNeeded = async ({
10
24
  cart,
11
25
  fetcher,
12
26
  cartInfoToEnable
13
27
  }: {
14
- cart: CartReturn | undefined,
28
+ cart: CartReturn | CartWithActionsDocs | undefined,
15
29
  fetcher: FetcherWithComponents<unknown>,
16
30
  cartInfoToEnable: CartInfoToEnable
17
31
  }) => {
@@ -19,7 +33,7 @@ const addProductToCartIfNeeded = async ({
19
33
  return await addProductToCart({ fetcher, cartInfoToEnable });
20
34
  }
21
35
 
22
- const redoProductsInCart = cart.lines.nodes.filter((cartLine) => {
36
+ const redoProductsInCart = getCartLines(cart).filter((cartLine) => {
23
37
  return cartLine.merchandise.product.vendor === 're:do';
24
38
  });
25
39
  const correctRedoProductInCart = redoProductsInCart?.filter((cartLine) => {
@@ -67,7 +81,7 @@ const removeProductFromCartIfNeeded = async ({
67
81
  fetcher,
68
82
  cartInfoToEnable
69
83
  }: {
70
- cart: CartReturn | undefined,
84
+ cart: CartReturn | CartWithActionsDocs | undefined,
71
85
  fetcher: FetcherWithComponents<unknown>,
72
86
  cartInfoToEnable: CartInfoToEnable
73
87
  }) => {
@@ -76,7 +90,7 @@ const removeProductFromCartIfNeeded = async ({
76
90
  return;
77
91
  }
78
92
 
79
- const redoProductsInCart = cart.lines.nodes.filter((cartLine) => {
93
+ const redoProductsInCart = getCartLines(cart).filter((cartLine) => {
80
94
  return cartLine.merchandise.product.vendor === 're:do';
81
95
  });
82
96
 
@@ -190,5 +204,7 @@ export {
190
204
  addProductToCartIfNeeded,
191
205
  removeProductFromCartIfNeeded,
192
206
  setCartRedoEnabledAttribute,
193
- useFetcherWithPromise
207
+ useFetcherWithPromise,
208
+ isCartWithActionsDocs,
209
+ getCartLines
194
210
  };