UNPKG

@betterstore/react

Version:

61 lines (60 loc) 2.21 kB
import { CreateCheckoutParams, InferProduct } from '@betterstore/sdk'; type SDKLineItemCreate = CreateCheckoutParams["lineItems"][number]; /** * Utility type to pick required arguments from a type while making others optional */ type PickRequiredArguments<RequiredArgumentKeys extends string[], T extends object> = { [K in keyof T as K extends RequiredArgumentKeys[number] ? K : never]: T[K]; } & { [K in keyof T as K extends RequiredArgumentKeys[number] ? never : K]?: T[K]; }; /** * Default product type for line items - requires id, makes productVariants optional */ type DefaultLineItemCreateProductType = PickRequiredArguments<[ "id" ], InferProduct> & { productVariants?: PickRequiredArguments<[ "id" ], InferProduct<{ with: { productVariants: true; }; }>["productVariants"][number]>[]; }; /** * Optional parameters that can be passed when adding an item to the cart */ type LineItemOptionalParams = { quantity?: number; variantOptions?: { name: string; value: string; }[]; metadata?: Record<string, any>; }; /** * Represents a single line item in the cart */ export interface CartLineItem<ProductType extends DefaultLineItemCreateProductType = DefaultLineItemCreateProductType> extends Pick<SDKLineItemCreate & { productData: ProductType; }, "metadata" | "quantity" | "variantOptions" | "productId" | "productData"> { id: string; selectedVariant: NonNullable<NonNullable<ProductType["productVariants"]>[number]> | null; } /** * Cart interface with all cart operations */ export interface Cart<ProductType extends DefaultLineItemCreateProductType = DefaultLineItemCreateProductType> { lineItems: CartLineItem<ProductType>[]; addItem: (product: ProductType, additionalParams?: LineItemOptionalParams) => void; removeItem: (id: string) => void; updateQuantity: (id: string, quantity: number) => void; getProductQuantity: (productId: string) => number; clearCart: () => void; } /** * Hook to access the cart store */ export declare const useCart: <ProductType extends DefaultLineItemCreateProductType = DefaultLineItemCreateProductType>() => Cart<ProductType>; export {};