Vue Storefront is now Alokai! Learn More
Product review normalizer

Product review normalizer

The normalizeProductReview function maps Magento ProductReview into Unified SfProductReview.

Parameters

NameTypeDefault valueDescription
reviewProductReviewMagento ProductReview

Extending

The SfProductReview is returned from GetProductReviews Method. If the SfProductReview structure doesn't contain the information you need for your Storefront, you can extend its logic using the defineNormalizers function. The following example demonstrates how to extend SfProductReview with a ratingsBreakdown field.

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

const normalizers = defineNormalizers<typeof normalizersMagento>()({
  ...normalizersMagento,
  normalizeProductReview: (review) => ({
    ...normalizersMagento.normalizeProductReview(review),
    ratingsBreakdown: review.ratings_breakdown
  }),
});

Source

productReview.ts
import { maybe } from "@shared/utils";
import { randomUUID } from "node:crypto";
import { defineNormalizer } from "../defineNormalizer";

export const normalizeProductReview = defineNormalizer.normalizeProductReview((review) => {
  return {
    id: randomUUID(),
    title: maybe(review.summary),
    text: maybe(review.text),
    rating:
      review.ratings_breakdown?.[0]?.value == null
        ? null
        : Number.parseInt(review.ratings_breakdown?.[0]?.value),
    reviewer: maybe(review.nickname),
    createdAt: new Date(review.created_at).toISOString(),
  };
});