Vue Storefront is now Alokai! Learn More
GetAvailableShippingMethods

GetAvailableShippingMethods

Implements GetAvailableShippingMethods Unified Method.

Source

import { defineApi, getCartVersion } from "@vsf-enterprise/unified-api-commercetools";
import type { 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, me] = await Promise.all([
      context.api.getShippingMethods(cartId),
      context.api.getMe(),
    ]);
    const activeCart = me.data!.me.activeCart!;
    const { normalizeShippingMethod } = getNormalizers(context);

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