@scayle/storefront-nuxt
Version:
Nuxt integration for the SCAYLE Commerce Engine and Storefront API
72 lines (71 loc) • 3.15 kB
TypeScript
import type { Product, WishlistItem, RpcMethodParameters } from '@scayle/storefront-core';
import type { UseRpcReturn, UseRpcCacheKey } from '../core/useRpc.js';
import type { MaybeRefOrGetter, ComputedRef } from 'vue';
/**
* Representation of options for configuring the `useWishlist` composable.
*/
type Options = Partial<{
params: MaybeRefOrGetter<RpcMethodParameters<'getWishlist'>>;
}>;
type UseWishlistBaseReturn = Awaited<UseRpcReturn<'getWishlist'>> & {
addItem: (item: {
variantId?: number;
productId?: number;
}) => Promise<void>;
removeItem: (item: {
variantId?: number;
productId?: number;
}) => Promise<void>;
replaceItem: (item: {
variantId?: number;
productId?: number;
}, newItem: {
variantId?: number;
productId?: number;
}) => Promise<void>;
clear: () => Promise<void>;
findItem: (item: {
variantId: number;
} | {
productId: number;
}) => WishlistItem | undefined;
contains: (item: {
variantId: number;
} | {
productId: number;
}) => boolean;
toggleItem: (item: {
variantId?: number;
productId?: number;
}) => Promise<void>;
count: ComputedRef<number | undefined>;
items: ComputedRef<WishlistItem[] | undefined>;
products: ComputedRef<Product[]>;
};
/**
* Manages wishlist interactions. Fetches wishlist data, provides methods for adding, removing,
* replacing, clearing, finding, and toggling items. It leverages RPC calls for data manipulation
* and provides reactive properties for count, items, and products.
*
* This function acts as a wrapper around the `useRpc` composable for wishlist 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 wishlist 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`.
*
* @see https://scayle.dev/en/api-guides/storefront-api/resources/wishlists/get-a-wishlist
* @see https://scayle.dev/en/core-documentation/storefront-guide/storefront-application/technical-foundation/data-and-state#shared-state-and-caching
*
* @param params An object containing parameters for wishlist-related RPC calls.
* @param params.params The parameters for the `useWishlist` RPC method.
* @param key A unique key for this RPC call. Used internally by `useRpc` for caching and state management.
*
* @returns A promise that resolves to an object containing wishlist data, methods, and reactive properties.
*
* @throws {Error} If an item cannot be found during removal or replacement.
*/
export declare function useWishlist({ params }?: Options, key?: UseRpcCacheKey): UseWishlistBaseReturn & Promise<UseWishlistBaseReturn>;
export {};