Attributes normalizer
Attributes are Unified Data Layer representation of SFCC VariationAttribute
values. The normalizeAttribute
function map a single VariationValue
along with list of VariationAttributes
into an SfAttribute
.
Parameters
Name | Type | Default value | Description |
---|---|---|---|
context | NormalizerContext | Context needed for the normalizer. | |
input | Object containing an array of variationAttributes and key and value of a single variation value |
Extending
The SfAttribute
is returned as a part of SfProduct
, SfOrder
and SfCart
. If you want to extend the SfAttribute
with custom fields, you should use the addCustomFields
API.
import { normalizers } from "@vsf-enterprise/unified-api-sfcc";
export const unifiedApiExtension = createUnifiedExtension({
normalizers: {
addCustomFields: [
{
normalizeAttribute: (context, { attribute, variationAttributes }) => {
const normalizedAttribute = normalizers.normalizeAttribute(context, { attribute, variationAttributes });
if (normalizedAttribute) {
return {
someNewField: "someValue",
};
}
return null;
},
},
],
},
config: {
...
},
});
Source
attributes.ts
import { defineNormalizer } from "../defineNormalizer";
export const normalizeAttribute = defineNormalizer.normalizeAttribute((_context, input) => {
const { attribute, variationAttributes } = input;
const variationAttribute = variationAttributes.find((attr) => attr.id === attribute.key);
const variationAttributeValue = variationAttribute?.values?.find(
(values) => values.value === attribute.value,
);
if (!variationAttribute || !variationAttributeValue) {
return null;
}
return {
name: variationAttribute.id,
label: variationAttribute.name ?? variationAttribute.id,
value: variationAttributeValue.value,
valueLabel: variationAttributeValue.name ?? variationAttributeValue.value,
};
});