UNPKG

@stackend/api

Version:

JS bindings to api.stackend.com

65 lines 2.35 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.addOrQuery = exports.createProductQuery = void 0; var reducedProductQuery_1 = __importDefault(require("./reducedProductQuery")); var shopify_clientside_1 = require("../shopify-clientside"); var parseSearchString_1 = require("../../search/parseSearchString"); function productListingQuery(req) { var productQuery = createProductQuery(req.q, req.productTypes, req.tags, true); return "products (query: ".concat((0, shopify_clientside_1.escapeQueryTerm)(productQuery), ", sortKey: ").concat(req.sort || 'RELEVANCE', ", ").concat((0, shopify_clientside_1.paginationArgs)(req), ") {\n edges {\n node {\n ").concat((0, reducedProductQuery_1.default)(req.imageMaxWidth), "\n },\n cursor\n },\n pageInfo {\n hasNextPage,\n hasPreviousPage,\n }\n }"); } exports.default = productListingQuery; function createProductQuery(text, productTypes, tags, availableForSale) { var r = { q: '', and: false }; if (text) { var v = (0, parseSearchString_1.parseSearchString)(text); if (v) { v.forEach(function (x) { r.q += x + ' '; }); r.and = true; } } if (availableForSale) { appendToQuery(r, 'available_for_sale:' + availableForSale); } addOrQuery(r, 'product_type', productTypes); addOrQuery(r, 'tag', tags); return r.q || 'available_for_sale:true'; } exports.createProductQuery = createProductQuery; function appendToQuery(r, v) { if (v) { if (r.and) { r.q += ' AND '; } r.q += v; r.and = true; } } function addOrQuery(r, name, values) { if (!values || values.length === 0) { return; } var v = '('; var first = true; values.forEach(function (x) { if (first) { first = false; } else { v += ' OR '; } v += name + ':' + (0, shopify_clientside_1.escapeQueryTerm)(x); }); v += ')'; appendToQuery(r, v); } exports.addOrQuery = addOrQuery; //# sourceMappingURL=productListingQuery.js.map