UNPKG

@scayle/storefront-core

Version:

Collection of essential utilities to work with the Storefront API

84 lines (83 loc) 3.77 kB
import type { Attributes, Value } from '@scayle/storefront-api'; /** * Returns the first value of an attribute. * This function is useful when you need to get the first value of an attribute. * * This returns undefined, if the attribute doesn't exist or if it doesn't have any values. * * @param attributes - The product attributes object * @param attributeName - The name of the attribute to look up * * @returns The first value of the attribute, or undefined if the attribute doesn't exist or has no values * * @example * ```typescript * const firstColor = getFirstAttributeValue(product.attributes, 'color') * // Returns: { id: 1, label: 'Red', value: 'red' } * ``` */ export declare const getFirstAttributeValue: (attributes: Attributes | undefined, attributeName: string) => Value | undefined; /** * Returns all values of an attribute group by its name key. * This function always returns an array, making it safe to use in iterations. * For single-select attributes, the value is wrapped in an array. * For multi-select attributes, the array is returned directly. * * @param attributes - The product attributes object * @param name - The name key of the attribute group to look up (e.g., 'color', 'size') * * @returns An array of values, or an empty array if the attribute doesn't exist or has no values * * @example * ```typescript * // Single-select attribute * const sizes = getAttributeValuesByName(product.attributes, 'size') * // Returns: [{ id: 1, label: 'M', value: 'medium' }] * * // Multi-select attribute * const colors = getAttributeValuesByName(product.attributes, 'color') * // Returns: [{ id: 1, label: 'Red' }, { id: 2, label: 'Blue' }] * ``` */ export declare const getAttributeValuesByName: (attributes: Attributes | undefined, name: string) => Value[]; /** * Returns all values of an attribute group by its numeric ID. * * This function searches through all attribute groups to find one matching the given ID. * This is useful when you need to look up attributes by their SCAYLE API identifier rather than by name. * Like {@link getAttributeValuesByName}, this always returns an array for consistency. * * @param attributes - The product attributes object * @param groupId - The numeric ID of the attribute group to look up * @returns An array of values, or an empty array if the attribute doesn't exist or has no values * * @example * ```typescript * // Look up by attribute group ID instead of name * const promotionValues = getAttributeValuesByGroupId(product.attributes, 42) * // Returns: [{ id: 123, label: 'Summer Sale', value: 'summer-2024' }] * ``` */ export declare const getAttributeValuesByGroupId: (attributes: Attributes | undefined, groupId: number) => Value[]; /** * Retrieves the value or label of the first attribute value for a given attribute name. * * @param attributes The set of attributes. * @param attributeName The name of the attribute to retrieve. * * @returns The value or label of the first attribute value, or null if not found. */ export declare const getAttributeValue: (attributes: Attributes | undefined, attributeName: string) => string | undefined; /** * Retrieves the first attribute value for multiple attribute names. * * @param attributes The set of attributes. * @param attributeNames An array of attribute names to retrieve. * * @returns An array of attribute values, filtered to remove any null or undefined values. */ export declare const getManyAttributeValueTuples: (attributes: Attributes | undefined, attributeNames: string[]) => Value[]; /** * @deprecated This function will be removed in the next major version. Use {@link getAttributeValuesByName} instead. */ export { getAttributeValuesByName as getAttributeValueTuples };