Vue Storefront is now Alokai! Learn More
Product normalizers

Product normalizers

Product includes two normalizers:

  • normalizeProduct: This function is used to map SFCC Product into SfProduct, which includes a full product details
  • normalizeProductCatalogItem: This function is used to map SFCC Product into SfProductCatalogItem, which includes only basic product details, needed to display a product in a product catalog

Parameters

normalizeProduct

NameTypeDefault valueDescription
productProductSFCC Product
ctxNormalizerContextContext needed for the normalizer. Context contain a currency field that contains a currency code

normalizeProductCatalogItem

NameTypeDefault valueDescription
productProductSearchHit or ProductSFCC ProductSearchHit or Product
ctxNormalizerContextContext needed for the normalizer. Context contain a currency field that contains a currency code

Extending

The SfProduct model is returned from GetProductDetails method. The SfProductCatalogItem model is returned from GetProducts method. If any of these models don't contain the information you need for your Storefront, you can extend its logic using the defineNormalizers function. In the following example we extend the normalizeProduct with manufacturerSku field which is available on SFCC Product and normalizeProductCatalogItem with productType field.

import { normalizers as normalizersSFC, defineNormalizers } from "@vsf-enterprise/unified-api-sfcc";
import { defineNormalizers } from "@vue-storefront/unified-data-model";

const normalizers = defineNormalizers<typeof normalizersSFCC>()({
  ...normalizersSFCC,
  normalizeProduct: (product, context) => ({
    ...normalizersSFCC.normalizeProduct(product, context),
    manufacturerSku: product.manufacturerSku,
  }),
  normalizeProductCatalogItem: (product, context) => ({
    ...normalizersSFCC.normalizeProductCatalogItem(product, context),
    productType: product.productType,
  }),
});

Since both normalizers accepts SFCC Product as a first argument, you may also use the same approach to use same representation for both SfProduct and SfProductCatalogItem models.

import { normalizers as normalizersSFC, defineNormalizers } from "@vsf-enterprise/unified-api-sfcc";

const normalizers = defineNormalizers<typeof normalizersSFCC>()({
  ...normalizersSFCC,
  normalizeProduct: (product, context) => ({
    ...normalizersSFCC.normalizeProduct(product, context),
  }),
  normalizeProductCatalogItem: (product, context) => ({
    ...normalizersSFCC.normalizeProduct(product, context),
  }),
});

However in this case you should be aware that SfProductCatalogItem will contain all the fields from SfProduct model and it will affect the performance of the catalog page.

Source

The normalizeProduct and normalizeProductCatalogItem function consists of several smaller normalizers such as normalizeImage, normalizeDiscountablePrice, and more, which you can override as well.

product.ts
import { getImageGroupByViewType } from "@/normalizers/__internal__";
import type { ImageGroup, Product } from "@internal";
import type { NormalizerContext } from "../types";
import type { SfAttribute, SfImage } from "@vue-storefront/unified-data-model";
import sanitizeHtml from "sanitize-html";
import { defineNormalizer } from "../defineNormalizer";
import { normalizeProductCatalogItemFromProduct } from "./productCatalog";
import { normalizeProductVariant } from "./variant";

// eslint-disable-next-line complexity
export const normalizeProduct = defineNormalizer.normalizeProduct((product, ctx) => {
  const baseFields = normalizeProductCatalogItemFromProduct(product, ctx);
  const description = product.longDescription
    ? sanitizeHtml(product.longDescription)
    : product.shortDescription
      ? sanitizeHtml(product.shortDescription)
      : null;

  let attributes: SfAttribute[] = [];
  // variationValues exists only for type.variant or type.item
  if (product.variationValues && product.variationAttributes) {
    attributes = getAttributes(product, ctx);
  }

  return {
    ...baseFields,
    attributes,
    description,
    gallery: getGallery(product.imageGroups ?? [], ctx),
    variants:
      product.variants?.map((variant) =>
        normalizeProductVariant(
          { variant, variationAttributes: product.variationAttributes ?? [] },
          ctx,
        ),
      ) ?? [],
  };
});

function getGallery(images: ImageGroup[], ctx: NormalizerContext): SfImage[] {
  if (!images?.length) {
    return [];
  }
  const imageGroup = getImageGroupByViewType(images, "large") ?? images[0];
  return imageGroup?.images.map((image) => ctx.normalizers.normalizeImage(image)) ?? [];
}

function getAttributes(product: Product, ctx: NormalizerContext): SfAttribute[] {
  const { variationValues, variationAttributes } = product;
  if (!variationValues || !variationAttributes) {
    return [];
  }
  return Object.entries(variationValues)
    .map(([key, value]) => {
      const attribute = { key, value };
      return ctx.normalizers.normalizeAttribute({ attribute, variationAttributes });
    })
    .filter(Boolean);
}