The product to reference
The ref, or undefined when the product has no stable key or URL
toProductRef({ cacheKey: '42', title: 'Acetone', url: '/acetone', id: 42, … });
// { cacheKey: '42', id: 42, title: 'Acetone', url: '/acetone', variants: [], … }
export function toProductRef(product: Product): ProductRef | undefined {
if (!product.cacheKey || !product.url) return undefined;
return {
...toVariantRef(product),
cacheKey: product.cacheKey,
title: product.title,
url: product.url,
permalink: product.permalink,
quantity: product.quantity,
uom: product.uom,
variants: mapDefined(product.variants ?? [], toVariantRef),
};
}
Captures what is needed to refetch a product later. Pass the parent product: a flattened size row carries its own id and SKU in place of the parent's.