Vue Storefront is now Alokai! Learn More
GetCart

GetCart

Implements GetCart Unified Method.

Source

import "./extended";
import type { Basket } from "@vsf-enterprise/sfcc-types";

import { assertAuthorized, defineApi, getNormalizedCart } from "@vsf-enterprise/unified-api-sfcc";

export const getCart = defineApi.getCart(async (context, args) => {
  const { api } = await context.getApiClient();
  let cart: Basket | undefined = await api.getBasket({ basketId: args?.cartId }).catch((err) => {
    if (typeof err === "string" && err.includes("Basket Not Found")) {
      return undefined;
    }
    throw err;
  });

  if (!cart) {
    const customer = await assertAuthorized(context, { silent: true });
    cart = await api.createBasket({
      ...(customer?.email && { customerInfo: { email: customer.email } }),
    });
  }

  return await getNormalizedCart(context, cart);
});