GetCart
Implements GetCart
Unified Method.
Source
import type { Basket } from "@vsf-enterprise/sfcc-types";
import { assertAuthorized, defineApi } from "@vsf-enterprise/unified-api-sfcc";
import { getNormalizedCart } from "@vsf-enterprise/unified-api-sfcc";
import { BasketCreateParams } from "@vsf-enterprise/sfcc-types";
import from "@vsf-enterprise/unified-api-sfcc/udl";
declare module "@vsf-enterprise/unified-api-sfcc" {
interface GetCartExtendedArgs {
/**
* The additional cart object fields to be included when a new cart is created.
* {@link https://developer.salesforce.com/docs/commerce/commerce-api/references/shopper-baskets?meta=createBasket SFCC method reference}
*/
createCart?: Omit<BasketCreateParams, "customerInfo">;
}
}
export const getCart = defineApi.getCart(async (context, args) => {
let cart: Basket | undefined = await context.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 context.api.createBasket({
...(customer?.email && { customerInfo: { email: customer.email } }),
});
}
return await getNormalizedCart(context, cart);
});