Vue Storefront is now Alokai! Learn More
GetProductReviews

GetProductReviews

Implements GetProductReviews Unified Method.

Source

import { defineApi } from "@vsf-enterprise/unified-api-commercetools";
import type { getReview } from "@vsf-enterprise/commercetools-api";
import type { Review } from "@vsf-enterprise/commercetools-types";
import type { GetProductReviewsArgs } from "@vsf-enterprise/unified-api-commercetools/udl";
import { getNormalizers } from "@vue-storefront/unified-data-model";

type GetReviewParams = Parameters<typeof getReview>[1];

export const getProductReviews = defineApi.getProductReviews(async (context, args) => {
  const response = await context.api.getReview(toGetReviewParams(args));
  const { normalizeProductReview, normalizePagination } = getNormalizers(context);

  return {
    reviews: response.reviews.results.map((review: Review) => normalizeProductReview(review)),
    pagination: normalizePagination(response.reviews),
  };
});

function toGetReviewParams(args: GetProductReviewsArgs): GetReviewParams {
  const offset =
    args.pageSize && args.currentPage ? (args.currentPage - 1) * args.pageSize : undefined;

  return {
    productId: args.productId,
    limit: args.pageSize,
    offset,
  };
}