Last active
August 11, 2020 21:19
-
-
Save klzns/64917026b7673df329c87dda2d3c6d36 to your computer and use it in GitHub Desktop.
Search endpoint that is being called by IO GraphQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const searchEncodeURI = (str) => { | |
return str.replace(/[%"'.()]/g, (c) => { | |
switch (c) { | |
case '%': | |
return "@perc@" | |
case '"': | |
return "@quo@" | |
case '\'': | |
return "@squo@" | |
case '.': | |
return "@dot@" | |
case '(': | |
return "@lpar@" | |
case ')': | |
return "@rpar@" | |
default: { | |
return c | |
} | |
} | |
}) | |
} | |
const productSearchUrl = ({ | |
query = '', | |
category = '', | |
specificationFilters, | |
priceRange = '', | |
collection = '', | |
salesChannel = '', | |
orderBy = '', | |
from = 0, | |
to = 9, | |
map = '', | |
hideUnavailableItems = false, | |
simulationBehavior = 'default', | |
}) => { | |
const sanitizedQuery = encodeURIComponent( | |
searchEncodeURI(decodeURIComponent(query || '').trim()) | |
) | |
if (hideUnavailableItems) { | |
const segmentData = { channel: '1' } // (this.context).segment | |
salesChannel = (segmentData && segmentData.channel.toString()) || '' | |
} | |
let url = `/pub/products/search/${sanitizedQuery}?` | |
if (category && !query) { | |
url += `&fq=C:/${category}/` | |
} | |
if (specificationFilters && specificationFilters.length > 0) { | |
url += specificationFilters.map(filter => `&fq=${filter}`) | |
} | |
if (priceRange) { | |
url += `&fq=P:[${priceRange}]` | |
} | |
if (collection) { | |
url += `&fq=productClusterIds:${collection}` | |
} | |
if (salesChannel) { | |
url += `&fq=isAvailablePerSalesChannel_${salesChannel}:1` | |
} | |
if (orderBy) { | |
url += `&O=${orderBy}` | |
} | |
if (map) { | |
url += `&map=${map}` | |
} | |
if (from != null && from > -1) { | |
url += `&_from=${from}` | |
} | |
if (to != null && to > -1) { | |
url += `&_to=${to}` | |
} | |
if (simulationBehavior === 'skip') { | |
url += `&simulation=false` | |
} | |
return 'https://' + __RUNTIME__.account + '.vtexcommercestable.com.br/api/catalog_system' + url | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment