cart
Version:
Headless cart management library
107 lines (104 loc) • 3.74 kB
TypeScript
import * as zustand_middleware from 'zustand/middleware';
import { StateStorage } from 'zustand/middleware';
import * as zustand from 'zustand';
interface CartItems {
cartQuantity?: number;
imagesrc?: string;
inStock?: boolean;
name: string;
price?: number;
productId: number | string | undefined;
quantity: number;
}
interface CartState {
/**
* Adds an item to the shopping cart or updates its quantity if already in the cart.
*
* Example:
* ```js
* addToCart({ productId: 'product1', name: 'Product 1', price: 20 });
* ```
* @param {CartItems} item - The item to be added or updated in the cart.
*/
addToCart?: (item: CartItems) => void;
/**
* Array of Items in the Cart.
*/
cartItems?: CartItems[];
/**
* Clears all items from the shopping cart.
*/
clearCart?: () => void;
/**
* Set cart open state to `false`.
*/
closeCart?: () => void;
/**
* Decreases the quantity of an item in the shopping cart or removes it if the quantity becomes zero.
*
* Example:
* ```js
* decreaseItem('product1', 1);
* ```
* @param {string} productId - The ID of the product to decrease the quantity of.
* @param {number} [quantity=1] - The quantity to decrease. Defaults to 1.
*/
decreaseItem?: (productId: string, quantity?: number) => void;
/**
* Indicates whether the cart is open or not.
*/
isCartOpen?: boolean;
/**
* Set cart open state to `true`.
*/
openCart?: () => void;
/**
* Removes an item from the shopping cart.
*
* Example:
* ```js
* removeFromCart('product1');
* ```
* @param {string} productId - The ID of the product to remove from the cart.
*/
removeFromCart?: (productId: string) => void;
/**
* Toggles the visibility of the shopping cart.
*/
toggleCart?: () => void;
}
/**
* Sets the name of the cart store for persistence.
* @param name - The name to be used for persistence.
*/
declare const setCartStoreName: (name: string) => void;
/**
* A higher-order function for server-side rendering (SSR) with Zustand state management.
* @template T - The type of the state.
* @template F - The type of the result returned by the callback.
* @param store - A function that performs server-side state setup.
* @param callback - A function that computes a result based on the state.
* @returns The result of the callback function, or undefined if not available.
*/
declare const withSSR: <T, F>(store: (callback: (state: T) => unknown) => unknown, callback: (state: T) => F) => F | undefined;
/**
* Sets the type of storage for the cart store's persistence.
* @param [storage] - The type of storage to be used for persistence (e.g., localStorage or sessionStorage). Defaults to localStorage if not provided.
*/
declare const setCartStoreType: (storage?: StateStorage) => void;
/**
* Custom hook for managing the shopping cart state.
* @returns {CartState} The cart state object.
*/
declare const useCart: zustand.UseBoundStore<Omit<zustand.StoreApi<CartState>, "persist"> & {
persist: {
setOptions: (options: Partial<zustand_middleware.PersistOptions<CartState, unknown>>) => void;
clearStorage: () => void;
rehydrate: () => void | Promise<void>;
hasHydrated: () => boolean;
onHydrate: (fn: (state: CartState) => void) => () => void;
onFinishHydration: (fn: (state: CartState) => void) => () => void;
getOptions: () => Partial<zustand_middleware.PersistOptions<CartState, unknown>>;
};
}>;
export { CartItems, CartState, setCartStoreName, setCartStoreType, useCart, withSSR };