Vue Storefront is now Alokai! Learn More
GetCart

GetCart

Implements GetCart Unified Method.

Source

import { getNormalizers } from "@alokai/connect/integration-kit";
import { HttpStatusCode } from "@alokai/connect/middleware";

import { defineApi } from "@vsf-enterprise/unified-api-commercetools";

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

  if (!cart) {
    const response = await api.createCart();
    cart = response.cart;
  }

  const { normalizeCart } = getNormalizers(context);

  return normalizeCart(cart);
});