@scayle/storefront-core
Version:
Collection of essential utilities to work with the Storefront API
212 lines (211 loc) • 7.89 kB
TypeScript
import type { AppliedReduction, BasketItemPrice, Product, ProductCategory, Value, Variant, VariantPrice } from '@scayle/storefront-api';
import type { ProductSibling } from '../types/sapi/product';
interface Route {
path?: string;
}
/**
* @deprecated This function will be removed in the next major version. This function has known compatibility issues with Nuxt 4. Please use `slugify()` from the `slugify` package directly in your Storefront Application.
*
* Generates a slug from a given URL string.
*
* @param url The URL string to slugify.
*
* @returns The slugified string.
*/
export declare const slugify: (url: string | undefined) => string;
/**
* Retrieves the price of a variant.
*
* @param variant The variant object.
*
* @returns The variant price.
*/
export declare const getPrice: (variant: Variant) => VariantPrice;
/**
* Retrieves the original price of a variant price.
*
* @param price The variant price object.
*
* @returns The original price.
*/
export declare const getOriginalPrice: (price: VariantPrice) => number;
/**
* Calculates the total applied reductions from a price object.
*
* @param price The price object containing applied reductions.
* @param price.appliedReductions Array of applied reductions with absolute and relative amounts.
*
* @returns The total applied reductions.
*/
export declare const getTotalAppliedReductions: (price: {
appliedReductions: {
amount: {
absoluteWithTax: number;
relative: number;
};
}[];
}) => {
absoluteWithTax: number;
relative: number;
};
/**
* Gets the lowest price from a list of variants.
*
* @param variants An array of variants.
*
* @returns The lowest variant price.
*/
export declare const getLowestPrice: (variants: Variant[]) => VariantPrice;
/**
* Filters applied reductions by a specific category.
*
* @param price The price object.
* @param category The category to filter by.
*
* @returns An array of applied reductions matching the category.
*/
export declare const getAppliedReductionsByCategory: (price: VariantPrice | BasketItemPrice, category: AppliedReduction["category"]) => AppliedReduction[];
/**
* Retrieves the size from a variant.
*
* @param variant The variant object.
* @param attributeName The name of the size attribute. Defaults to 'shopSize'.
*
* @returns The size value.
*/
export declare const getSizeFromVariant: (variant: Variant, attributeName?: string) => Value | undefined;
/**
* Retrieves the size from a specific variant within a product.
*
* @param product The product object.
* @param variantId The ID of the variant.
*
* @returns The size value, or null if not found.
*/
export declare const getSizeFromSpecificVariant: (product: Product, variantId?: number) => Value | null | undefined;
/**
* Retrieves a specific variant from a list of variants.
*
* @param variants An array of variants.
* @param id The ID of the variant to retrieve.
*
* @returns The variant object if found, otherwise undefined.
*/
export declare const getVariant: (variants: Variant[], id: number) => Variant | undefined;
/**
* Retrieves all unique sizes from a list of variant attributes.
*
* @param variantsAttributes An array of variants.
* @param attributeName The name of the size attribute. Defaults to 'shopSize'.
*
* @returns An array of unique size values.
*/
export declare const getAllSizesFromVariants: (variantsAttributes: Variant[], attributeName?: string) => Value[];
/**
* Retrieves product siblings including the product itself.
* Filters out inactive siblings.
*
* @param product The product object. If null or undefined, returns an empty array.
* @param colorAttributeName The name of the attribute representing the product's color.
* Defaults to 'colorDetail'. This attribute is used to extract color information
* for each sibling using `getAttributeValueTuples`.
*
* @returns An array of simplified product sibling objects. Each object has the following structure:
* - `id`: The ID of the product sibling.
* - `image`: The URL of the product's bust image, front view. Retrieved using `getImageFromList`.
* If no suitable image is found, this field may be null.
* - `colors`: An array of color values extracted from the attribute specified by `colorAttributeName`.
* This is the result of calling `getAttributeValueTuples` with the product's attributes and `colorAttributeName`.
* If the attribute is not found, this will be an empty array.
*/
export declare const getProductSiblings: (product?: Product | null, colorAttributeName?: string) => ProductSibling[];
/**
* Retrieves the colors of a product.
*
* @param product The product object.
* @param colorAttributeName The name of the color attribute. Defaults to 'colorDetail'.
*
* @returns An array of product color labels.
*/
export declare const getProductColors: (product: Product, colorAttributeName?: string) => string[];
/**
* Retrieves a variant by its size.
*
* @param variants An array of variants.
* @param size The size value to search for.
* @param sizeAttributeName The name of the size attribute. Defaults to 'shopSize'.
*
* @returns The variant object if found, otherwise undefined.
*/
export declare const getVariantBySize: (variants: Variant[], size: Value, sizeAttributeName?: string) => Variant | undefined;
/**
* Checks if a variant is in stock.
*
* @param variant The variant object.
*
* @returns True if the variant is in stock or sellable when out-of-stock, false otherwise.
*/
export declare const isInStock: (variant: Variant) => boolean;
/**
* Checks if a variant with a specific size is in stock.
*
* @param variants An array of variants.
* @param size The size value to check.
* @param sizeAttributeName The name of the size attribute. Defaults to 'shopSize'.
*
* @returns True if the variant is in stock, false otherwise.
*/
export declare const isVariantInStock: (variants: Variant[], size: Value, sizeAttributeName?: string) => boolean;
/**
* Retrieves an attribute value from a product.
*
* @param product The product object.
* @param key The attribute key.
*
* @returns The attribute value.
*/
export declare const getAttribute: (product: Product, key: string) => string | undefined;
/**
* @deprecated Will be removed in the next major version. Please use `useRouteHelpers().getProductDetailRoute()` instead.
*
* Generates the product path based on product name and id
*
* @param product The product object
*
* @returns The product path as string
*/
export declare const getProductPath: (product: Product) => string;
/**
* Retrieves categories matching a given route from a product.
*
* @param product The product object
* @param route The route object containing path information.
*
* @returns An array of categories.
*/
export declare const getCategoriesByRoute: (product: Product | null, route: Route | null) => ProductCategory[];
/**
* Retrieves the latest/deepest category from product categories
*
* @param categories List of product categories
*
* @returns The latest category object or undefined
*/
export declare const getLatestCategory: (categories?: Product["categories"]) => ProductCategory | undefined;
/**
* Retrieves colors from a product and its siblings
*
* @param product The product data.
* @param colorAttributeName The name of the color attribute (default: 'colorDetail').
*
* @returns An array of color values.
*/
export declare const getProductAndSiblingsColors: (product: Product, colorAttributeName?: string) => Value[];
/**
* Extracts unique crossselling values from variants' advanced attributes.
*
* @param variants An array of variants.
* @returns An array of unique crossselling values (strings or numbers).
*/
export declare const getVariantCrosssellingValues: (variants: Variant[] | undefined) => (string | number)[];
export {};