All files / src/utils/search-query/translators translateAstToWixFilter.ts

100% Statements 6/6
100% Branches 2/2
100% Functions 1/1
100% Lines 6/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 331x     1x                                                 1x 1x 3x 3x  
import { translateAstToContainsFilter } from './translateAstToContainsFilter';
import type { SearchAst } from '../types';
 
/**
 * Translates a {@link SearchAst} into a Wix catalog filter tree.
 *
 * Leaf terms become a `CONTAINS` match on the product `name` field (wrapped in
 * `*…*` wildcards, mirroring the single-term default in `SupplierBaseWix`).
 * Boolean nodes map to Wix's `and`/`or`/`not` combinators, which the catalog
 * `productsWithMetaData(filters: …)` endpoint accepts. Thin wrapper over the
 * shared {@link translateAstToContainsFilter} (its output is structurally a
 * `WixFilterNode`).
 *
 * @category Utils
 * @group Converters
 * @param ast - The parsed query tree.
 * @returns A Wix filter node suitable for `GraphQLQueryVariables.filters`.
 * @example
 * ```ts
 * translateAstToWixFilter({ type: "or",
 *   left: { type: "term", value: "Sodium", phrase: false },
 *   right: { type: "term", value: "Potassium", phrase: false } });
 * // { or: [
 * //   { term: { field: "name", op: "CONTAINS", values: ["*Sodium*"] } },
 * //   { term: { field: "name", op: "CONTAINS", values: ["*Potassium*"] } },
 * // ] }
 * ```
 * @source
 */
export function translateAstToWixFilter(ast: SearchAst): WixFilterNode {
  return translateAstToContainsFilter(ast, (value) => `*${value}*`);
}