Vue Storefront is now Alokai! Learn More
GetProductDetails

GetProductDetails

Implements GetProductDetails Unified Method.

Source

import { defineApi } from "@vsf-enterprise/unified-api-sapcc";
import type { CategoryWithParentCode } from "@vsf-enterprise/unified-api-sapcc";
import { getNormalizers } from "@vsf-enterprise/unified-api-sapcc/udl";


declare module "@vsf-enterprise/unified-api-sapcc" {
  interface GetProductDetailsExtendedArgs {
    /**
     * Response configuration. List of fields returned in the response body.
     */
    fields?: "BASIC" | "DEFAULT" | "FULL" | string | string[];
  }
}


export const getProductDetails = defineApi.getProductDetails(async (context, args) => {
  const { id, sku } = args;
  const code = sku || id;
  const { data: product } = await context.api.getProduct({
    productCode: decodeURI(code.replace(/\s/g, "+")),
  });
  const { normalizeProduct, normalizeCategory } = getNormalizers(context);
  const categoryHierarchy: CategoryWithParentCode[] =
    product.categories?.map((category, index, categories) => ({
      ...category,
      parentCategoryCode: categories[index - 1]?.code,
    })) ?? [];

  return {
    product: normalizeProduct(product),
    categoryHierarchy: categoryHierarchy.map((category) => normalizeCategory(category)) ?? [],
  };
});