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 "@vsf-enterprise/unified-api-commercetools/udl";

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.me.activeCart!;
    const { normalizeShippingMethod } = getNormalizers(context);

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