Vue Storefront is now Alokai! Learn More
Normalizers

Normalizers

In our integration, we use so-called normalizers to map data received from the Coveo to match Unified Data Model. It is possible to overwrite these normalizers with functions respecting the contract.

Passing own normalizerri:link

Firstly, we need to select one kind of normalizer - let's say it's normalizeCoveoIndex. Then we prepare a custom normalizer implementing the NormalizeCoveoIndex type:

import type { NormalizeCoveoIndex } from "@sf-modules-middleware/coveo";
// type NormalizeCoveoIndex = (params: NormalizeCoveoIndexParams) => SfProductCatalogItem;

export const myNormalizeCoveoIndex: NormalizeCoveoIndex = ({
  product,
  currency,
  normalizeDiscountablePrice,
  normalizeCoveoImage,
  normalizeCoveoImageUrl,
}) => {
  return {
    id: product.raw?.ec_code ?? '',
    sku: product.raw?.ec_code ?? '',
    name: product.raw?.ec_name ?? '',
    slug: product.raw?.ec_slug ?? '',
    price: normalizeDiscountablePrice(+product.raw.ec_price, currency),
    primaryImage: normalizeCoveoImage(product.raw?.ec_images ?? '', normalizeCoveoImageUrl),
    rating: null,
    quantityLimit: null,
    myCustomProperty: 123
  };
};

To use it, add it to the Coveo's configuration file:

// integrations/coveo/index.ts
import { myNormalizeCoveoIndex } from '...';

export const config = {
  location: "@vsf-enterprise/coveo-api/server",
  configuration: {},
  extensions: (extensions: ApiClientExtension[]) => [
    ...extensions,
    coveoExtensionFactory({
      defaultCurrency: 'USD',
      commerce: 'commerce',
      normalizers: {
        normalizeCoveoIndex: myNormalizeCoveoIndex
      }
    }), 
  ],
} satisfies Integration<IntegrationConfig>;

Reusing default normalizerri:link

If you don't want to write normalizer from scratch. We expose default normalizer, reuse it and add on top of that anything.

import type { NormalizeCoveoIndex } from "@sf-modules-middleware/coveo";
import { normalizers } from "@sf-modules-middleware/coveo";
// type NormalizeCoveoIndex = (params: NormalizeCoveoIndexParams) => SfProductCatalogItem;

export const myNormalizeCoveoIndex: NormalizeCoveoIndex = (params) => {
  const baseNormalized = normalizers.normalizeCoveoIndex(params);
  return {
    ...baseNormalized,
    myCustomProperty: 123
  };
};

Available normalizersri:link

A list of normalizers possible to overwrite with their responsibilities.

normalizeCoveoIndexri:link

Normalizer responsible for mapping single entry from Coveo to SfProductCatalogItem type from Unified Data Model.

export type NormalizeCoveoIndex = (params: NormalizeCoveoIndexParams) => SfProductCatalogItem;

normalizeCoveoFacetri:link

Normalizer responsible for mapping single facet from Coveo to SfFacet type from Unified Data Model.

export type NormalizeCoveoFacet = (facet: CoveoResultFacet) => SfFacet;

normalizeCoveoImageri:link

Normalizer responsible for mapping single image from Coveo (used inside normalizeCoveoIndex) to SfImage type from Unified Data Model.

export type NormalizeCoveoImage = (images: string | string[], transformImageUrl?: NormalizeCoveoImageUrl) => SfImage;

normalizeCoveoImageUrlri:link

Normalizer responsible for mapping a URL to image.

export type NormalizeCoveoImageUrl = (url: string) => string;

normalizeCoveoDiscountablePriceri:link

Normalizer responsible for mapping raw price and currency from Coveo (used inside normalizeCoveoIndex) to SfDiscountablePrice type from Unified Data Model.

export type NormalizeDiscountablePriceType = (
  rawPrice: number,
  currency: string
) => SfDiscountablePrice;