Vue Storefront is now Alokai! Learn More
GetCart

GetCart

Implements GetCart Unified Method.

Source

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

export const getCart = defineApi.getCart(async (context, args) => {
  if (args?.cartId != null) {
    throw { statusCode: 400, message: "Multiple carts feature is not available." };
  }
  const me = await context.api.getMe();
  let cart = me.data?.me.activeCart;

  if (!cart) {
    const response = await context.api.createCart();
    cart = response.data?.cart as Cart;
  }

  const { normalizeCart } = getNormalizers(context);

  return normalizeCart(cart);
});