Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions vtex/loaders/legacy/productList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ const isFQProps = (p: any): p is FQProps => isValidArrayProp(p.fq);
// deno-lint-ignore no-explicit-any
const isTermProps = (p: any): p is TermProps => typeof p.term === "string";

const sanitizeNumericIds = (ids: string[] | undefined): string[] => {
return (ids || [])
.filter((id) => id && /^\d+$/.test(id.trim()))
.map((id) => id.trim());
};

const preferredSKU = (items: LegacyItem[], { props }: Props) => {
const fetchedSkus = new Set((props as SkuIDProps).ids ?? []);
return items.find((item) => fetchedSkus.has(item.itemId)) || items[0];
Expand All @@ -133,7 +139,7 @@ const fromProps = ({ props }: Props) => {
};

if (isSKUIDProps(props)) {
const skuIds = props.ids || [];
const skuIds = sanitizeNumericIds(props.ids);

skuIds.forEach((skuId) => params.fq.push(`skuId:${skuId}`));
params._from = 0;
Expand All @@ -143,7 +149,7 @@ const fromProps = ({ props }: Props) => {
}

if (isProductIDProps(props)) {
const productIds = props.productIds || [];
const productIds = sanitizeNumericIds(props.productIds);

productIds.forEach((productId) => params.fq.push(`productId:${productId}`));
params._from = 0;
Expand Down Expand Up @@ -303,14 +309,12 @@ export const cacheKey = (
]);

if (isSKUIDProps(props)) {
const skuIds = [...props.ids ?? []]?.sort();
const skuIds = sanitizeNumericIds(props.ids).sort();
params.append("skuids", skuIds.join(","));
}

if (
isProductIDProps(props)
) {
const productIds = [props.productIds ?? []].sort();
if (isProductIDProps(props)) {
const productIds = sanitizeNumericIds(props.productIds).sort();
params.append("productids", productIds.join(","));
}

Expand Down
Loading