GetProducts
Implements GetProducts
Unified Method.
Source
import { defineApi } from "@vsf-enterprise/unified-api-sfcc";
import { getNormalizers } from "@vsf-enterprise/unified-api-sfcc/udl";
import { ProductsGetParams } from "@vsf-enterprise/sfcc-types";
declare module "@vsf-enterprise/unified-api-sfcc" {
interface GetProductsExtendedArgs {
/**
* The additional fields to filter products.
*/
products?: Omit<ProductsGetParams, "perPricebook" | "id" | "currency" | "locale">;
}
}
export const getProducts = defineApi.getProducts(async (context, { ids = [], skus = [] }) => {
try {
// both, ids and skus, may be product ids
const productIds = [...new Set([...ids, ...skus])];
const { normalizeProductCatalogItem } = getNormalizers(context);
const products = await context.api.getProducts({ ids: productIds, perPricebook: true });
return {
products: products.map((product) => normalizeProductCatalogItem(product)),
};
} catch (error) {
console.error({ error });
return {
products: [],
};
}
});