Vue Storefront is now Alokai! Learn More
Money normalizer

Money normalizer

The normalizeMoney function maps Commercetools BaseMoney into Unified SfMoney.

Parameters

NameTypeDefault valueDescription
moneyBaseMoneyCommercetools money

Extending

The SfMoney is returned as a part of multiple models, as for example SfProduct, SfProductCatalogItem, and SfCart. If you want to extend the SfMoney with custom fields, you should use the defineNormalizers utility.

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

const normalizers = defineNormalizers<typeof normalizersCT>()({
  ...normalizersCT,
  normalizeMoney: (money, ctx) => ({
    ...normalizersCT.normalizeMoney(money, ctx),
    type: money.type,
  }),
});

Source

money.ts
import { defineNormalizer } from "../defineNormalizer";

export const normalizeMoney = defineNormalizer.normalizeMoney((money) => {
  // Invalid types for BaseMoney - fractionDigits are optional
  const fractionDigits = money.fractionDigits ?? 2;
  const divider = Math.pow(10, fractionDigits);
  const amount = money.centAmount / divider;

  return {
    currency: money.currencyCode,
    amount,
    precisionAmount: amount.toFixed(fractionDigits),
  };
});