GetProductReviews
Implements GetProductReviews
Unified Method.
Source
import { defineApi } from "@vsf-enterprise/unified-api-sapcc";
import type { Review } from "@vsf-enterprise/sapcc-types";
import type { SfPagination } from "@vsf-enterprise/unified-api-sapcc/udl";
import { getNormalizers } from "@vue-storefront/unified-data-model";
declare module "@vsf-enterprise/unified-api-sapcc" {
interface GetProductReviewsExtendedArgs {
/**
* Response configuration. List of fields returned in the response body.
*/
fields?: "BASIC" | "DEFAULT" | "FULL" | string | string[];
/**
* Maximum number of results to be returned.
*/
maxCount?: number;
}
}
export const getProductReviews = defineApi.getProductReviews(async (context, args) => {
const { productId, currentPage, pageSize } = args;
let reviewsData: Review[] = [];
const { normalizeProductReview } = getNormalizers(context);
try {
const { data: response } = await context.api.getProductReviews({
productCode: productId,
});
reviewsData = response.reviews ?? [];
} catch {
console.error("Error while fetching product reviews");
}
const { data, pagination } = paginatedResponse(reviewsData, currentPage, pageSize);
return {
reviews: data.map((review) => normalizeProductReview(review)),
pagination,
};
});
type PaginatedResponse<TData> = {
data: TData[];
pagination: SfPagination;
};
function paginatedResponse<TData>(
data: TData[],
currentPage = 0,
pageSize = 0,
): PaginatedResponse<TData> {
const totalPages = pageSize ? Math.ceil(data.length / pageSize) : 0;
const start = currentPage * pageSize;
const end = start + pageSize;
return {
data: data.slice(start, end),
pagination: {
currentPage,
pageSize,
totalResults: data.length,
totalPages,
},
};
}