|
| 1 | +import {StorefrontApiErrors, formatAPIResult} from '../../storefront'; |
| 2 | +import type {CartSelectableAddressInput} from '@shopify/hydrogen-react/storefront-api-types'; |
| 3 | +import { |
| 4 | + CART_WARNING_FRAGMENT, |
| 5 | + MINIMAL_CART_FRAGMENT, |
| 6 | + USER_ERROR_FRAGMENT, |
| 7 | +} from './cart-fragments'; |
| 8 | +import type { |
| 9 | + CartOptionalInput, |
| 10 | + CartQueryData, |
| 11 | + CartQueryDataReturn, |
| 12 | + CartQueryOptions, |
| 13 | +} from './cart-types'; |
| 14 | + |
| 15 | +export type CartDeliveryAddressesReplaceFunction = ( |
| 16 | + addresses: Array<CartSelectableAddressInput>, |
| 17 | + optionalParams?: CartOptionalInput, |
| 18 | +) => Promise<CartQueryDataReturn>; |
| 19 | + |
| 20 | +/** |
| 21 | + * Replaces all delivery addresses on the cart. |
| 22 | + * |
| 23 | + * This function sends a mutation to the storefront API to replace all delivery addresses on the cart |
| 24 | + * with the provided addresses. It returns the result of the mutation, including any errors that occurred. |
| 25 | + * |
| 26 | + * @param {CartQueryOptions} options - The options for the cart query, including the storefront API client and cart fragment. |
| 27 | + * @returns {CartDeliveryAddressesReplaceFunction} - A function that takes an array of addresses and optional parameters, and returns the result of the API call. |
| 28 | + * |
| 29 | + * @example |
| 30 | + * const replaceDeliveryAddresses = cartDeliveryAddressesReplaceDefault({ storefront, getCartId }); |
| 31 | + * const result = await replaceDeliveryAddresses([ |
| 32 | + * { |
| 33 | + * address: { |
| 34 | + * deliveryAddress: { |
| 35 | + * address1: '123 Main St', |
| 36 | + * city: 'Anytown', |
| 37 | + * countryCode: 'US' |
| 38 | + * } |
| 39 | + * }, |
| 40 | + * selected: true |
| 41 | + * } |
| 42 | + * ], { someOptionalParam: 'value' } |
| 43 | + * ); |
| 44 | + */ |
| 45 | +export function cartDeliveryAddressesReplaceDefault( |
| 46 | + options: CartQueryOptions, |
| 47 | +): CartDeliveryAddressesReplaceFunction { |
| 48 | + return async ( |
| 49 | + addresses: Array<CartSelectableAddressInput>, |
| 50 | + optionalParams, |
| 51 | + ) => { |
| 52 | + const {cartDeliveryAddressesReplace, errors} = |
| 53 | + await options.storefront.mutate<{ |
| 54 | + cartDeliveryAddressesReplace: CartQueryData; |
| 55 | + errors: StorefrontApiErrors; |
| 56 | + }>(CART_DELIVERY_ADDRESSES_REPLACE_MUTATION(options.cartFragment), { |
| 57 | + variables: { |
| 58 | + cartId: options.getCartId(), |
| 59 | + addresses, |
| 60 | + ...optionalParams, |
| 61 | + }, |
| 62 | + }); |
| 63 | + |
| 64 | + return formatAPIResult(cartDeliveryAddressesReplace, errors); |
| 65 | + }; |
| 66 | +} |
| 67 | + |
| 68 | +//! @see: https://shopify.dev/docs/api/storefront/2025-10/mutations/cartDeliveryAddressesReplace |
| 69 | +export const CART_DELIVERY_ADDRESSES_REPLACE_MUTATION = ( |
| 70 | + cartFragment = MINIMAL_CART_FRAGMENT, |
| 71 | +) => `#graphql |
| 72 | + mutation cartDeliveryAddressesReplace( |
| 73 | + $cartId: ID! |
| 74 | + $addresses: [CartSelectableAddressInput!]!, |
| 75 | + $country: CountryCode = ZZ |
| 76 | + $language: LanguageCode |
| 77 | + ) @inContext(country: $country, language: $language) { |
| 78 | + cartDeliveryAddressesReplace(addresses: $addresses, cartId: $cartId) { |
| 79 | + cart { |
| 80 | + ...CartApiMutation |
| 81 | + } |
| 82 | + userErrors { |
| 83 | + ...CartApiError |
| 84 | + } |
| 85 | + warnings { |
| 86 | + ...CartApiWarning |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + ${cartFragment} |
| 91 | + ${USER_ERROR_FRAGMENT} |
| 92 | + ${CART_WARNING_FRAGMENT} |
| 93 | +`; |
0 commit comments