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

GetAvailableShippingMethods

Implements GetAvailableShippingMethods Unified Method.

Source

import { defineApi, getCartVersion } from "@vsf-enterprise/unified-api-commercetools";
import { ShippingMethod } from "@vsf-enterprise/commercetools-types";
import { getNormalizers } from "@vue-storefront/unified-data-model";

export const getAvailableShippingMethods = defineApi.getAvailableShippingMethods(
  async (context) => {
    const { id: cartId } = await getCartVersion(context);
    const shippingMethods = await context.api.getShippingMethods(cartId);

    const { normalizeShippingMethod } = getNormalizers(context);

    return {
      methods:
        shippingMethods.data?.shippingMethods
          .map((method: ShippingMethod) => normalizeShippingMethod(method))
          .filter(Boolean) || [],
    };
  },
);