Vue Storefront is now Alokai! Learn More
GetProducts

GetProducts

Implements GetProducts Unified Method.

Source

import { defineApi } from "@vsf-enterprise/unified-api-magento";
import type { ProductWithTypeName } from "@/normalizers/__internal__";
import { getNormalizers } from "@vue-storefront/unified-data-model";

export const getProducts = defineApi.getProducts(async (context, args) => {
  const { ids, skus = [] } = args;

  if (ids?.length) {
    console.warn("getProducts(): ids argument is not supported");
  }

  if (skus.length === 0) {
    return {
      products: [],
    };
  }

  const responseData = await context.api.products({ filter: { sku: { in: skus } } });

  if (responseData.errors?.[0])
    throw {
      message: responseData.errors[0].message,
    };

  const responseProducts = responseData.data.products?.items ?? [];

  const { normalizeProductCatalogItem } = getNormalizers(context);

  const products = responseProducts
    .filter(Boolean)
    .map((product) => normalizeProductCatalogItem(product as ProductWithTypeName));

  return {
    products,
  };
});