The Vue Storefront Essentials Course is now available! Learn More
GetAvailableShippingMethods

GetAvailableShippingMethods

Implements GetAvailableShippingMethods Unified Method.

Source

import { getCartId, defineApi } from "@vsf-enterprise/unified-api-sapcc";
import { getNormalizers } from "@vue-storefront/unified-data-model";

export const getAvailableShippingMethods = defineApi.getAvailableShippingMethods(
  async (context) => {
    const cartId = getCartId(context);
    const { normalizeShippingMethod } = getNormalizers(context);

    try {
      const data = await context.api.getCartDeliveryModes({ cartId });
      return {
        methods:
          data.deliveryModes
            ?.map((deliveryMode) => normalizeShippingMethod(deliveryMode))
            .filter(Boolean) ?? [],
      };
    } catch {
      throw new Error("Error getting available shipping methods");
    }
  },
);