UNPKG

@scayle/storefront-nuxt

Version:

Nuxt integration for the SCAYLE Commerce Engine and Storefront API

85 lines (84 loc) 5.36 kB
import type { AddOrUpdateItemType, RpcMethodParameters, BasketResponseData, BasketItem, Product, BasketTotalPrice, BasketItemUpdateData } from '@scayle/storefront-core'; import { ExistingItemHandling, generateBasketKey } from '@scayle/storefront-core'; import type { NormalizedRpcResponse, UseRpcReturn, UseRpcCacheKey } from '../core/useRpc.js'; import type { MaybeRefOrGetter, ComputedRef, Ref } from 'vue'; import type { BasketKey, BasketPackageInformation } from '@scayle/storefront-api'; /** * Representation of options for configuring the `useBasket` composable. */ type UseBasketOptions = Partial<{ params: MaybeRefOrGetter<Omit<RpcMethodParameters<'getBasket'>, 'orderCustomData'>>; }>; type UseBasketBaseReturn = Omit<Awaited<UseRpcReturn<'getBasket'>>, 'data'> & { data: Ref<NormalizedRpcResponse<'getBasket'>['basket'] | null>; } & { addItem: (item: AddOrUpdateItemType & { existingItemHandling?: ExistingItemHandling; }) => Promise<void>; addItems: (items: AddOrUpdateItemType[], existingItemHandling?: typeof ExistingItemHandling.ADD_QUANTITY_TO_EXISTING) => Promise<void>; removeItemByKey: (itemKey: string) => Promise<void>; updateItem: (basketItemKey: string, update: BasketItemUpdateData) => Promise<void>; getApplicablePromotionsByCode: (promotionCode: string) => Promise<BasketResponseData>; clear: () => Promise<void>; items: ComputedRef<BasketItem[] | undefined>; cost: ComputedRef<BasketTotalPrice | undefined>; count: ComputedRef<number | undefined>; /** @deprecated In a future release `mergeBaskets` will be removed. Consider using `useRpcCall('mergeBaskets')` instead.*/ mergeBaskets: (args: { fromBasketKey: string; toBasketKey: string; }) => Promise<NormalizedRpcResponse<'mergeBaskets'>>; /** @deprecated In a future release `findItem` will be removed. Consider using `findItem` from `@scayle/storefront-basket` */ findItem: (item: { variantId: number; } | { productId: number; }) => BasketItem | undefined; /** @deprecated In a future release `contains` will be removed. Consider using `contains` from `@scayle/storefront-basket` */ contains: (item: { variantId: number; } | { productId: number; }) => boolean; /** @deprecated In a future release `removeItem` will be removed. Consider using {@link UseBasketBaseReturn.removeItemByKey | removeItemByKey } instead*/ removeItem: (item: { variantId: number; }) => Promise<void>; /** @deprecated In a future release `generateBasketKey` will be removed. Consider using {@link generateBasketKey} instead.*/ generateBasketKey: typeof generateBasketKey; /** @deprecated In a future release `key` will be removed. Consider using {@link UseBasketBaseReturn.items | `items`} instead. */ products: ComputedRef<Product[]>; /** @deprecated In a future removeItem `countWithoutSoldOutItems` will be removed. Consider using `countAvailableBasketItems` from `@scayle/storefront-basket` */ countWithoutSoldOutItems: ComputedRef<number | undefined>; /** @deprecated In a future release `key` will be removed. Consider using `data.value.key` instead. */ key: ComputedRef<BasketKey | undefined>; /** @deprecated In a future release `key` will be removed. Consider checking {@link UseBasketBaseReturn.count | count} instead. */ isEmpty: ComputedRef<boolean>; /** @deprecated In a future release `packages` will be removed. Consider using `data.value.packages` instead. */ packages: ComputedRef<BasketPackageInformation[] | undefined>; /** @deprecated In a future release `shippingDates` will be removed. Consider using {@link getShippingDates} instead. */ shippingDates: ComputedRef<(string | null)[] | undefined>; }; /** * Manages basket interactions. Provides functionalities for adding, removing, updating, and clearing * basket items, as well as accessing basket information like items, cost, and count. * * This function acts as a wrapper around the `useRpc` composable for basket data, * simplifying the process of fetching brand data. It internally calls `useRpc` * with the provided parameters and options. It utilizes the `getCachedData` option * within `useRpc` to retrieve cached basket data during hydration or from async data otherwise. * This helps avoid unnecessary re-fetches and improves performance. Specifically, * it checks `nuxtApp._asyncData` for existing data associated with the provided key. * This approach addresses a [Nuxt typing issue](https://github.com/nuxt/nuxt/issues/26093) * requiring a type assertion to `WishlistResponseData`. * * @param options An object containing parameters and options for all basket-related RPC calls. * @param options.params Parameters for the 'getBasket' RPC call (excluding `orderCustomData`). * @param key A unique key for identifying the basket instance. Defaults to 'useBasket'. * * @returns A promise resolving to an object with basket data, methods, and reactive properties. * * @throws {Error} If an item cannot be found during removal or if an item is added with a reduced quantity. Also throws FetchError if RPC calls fail. */ export declare function useBasket({ params }?: UseBasketOptions, key?: UseRpcCacheKey): UseBasketBaseReturn & Promise<UseBasketBaseReturn>; export {};