Vue Storefront is now Alokai! Learn More
Image normalizer

Image normalizer

The normalizeImage function maps Commercetools Image into Unified SfImage.

Parameters

NameTypeDefault valueDescription
contextNormalizerContextNormalization context
imageImageCommercetools image

Extending

The SfImage is returned as a part of multiple models, as for example SfProduct, SfProductCatalogItem, and SfCart. If the SfImage structure doesn't contain the information you need for your Storefront, you can extend its logic using the addCustomFields API.

export const unifiedApiExtension = createUnifiedExtension({
  normalizers: {
    addCustomFields: [
      {
        normalizeImage: (context, image) => ({
          dimensions: image.dimensions,
        }),
      },
    ],
  },
  config: {
    ...
  },
});

Source

image.ts
import { maybe } from "@/helpers";
import { defineNormalizer } from "../defineNormalizer";

export const normalizeImage = defineNormalizer.normalizeImage((_context, image) => {
  return {
    alt: maybe(image.label),
    url: image.url,
  };
});