GetProducts
Implements GetProducts
Unified Method.
Source
import { defineApi } from "@vsf-enterprise/unified-api-magento";
import type { ProductWithTypeName } from "@vsf-enterprise/unified-api-magento/ecommerceTypes";
import { getNormalizers } from "@vsf-enterprise/unified-api-magento/udl";
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,
};
});