Skip to content

Instantly share code, notes, and snippets.

@klzns
Last active August 11, 2020 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klzns/64917026b7673df329c87dda2d3c6d36 to your computer and use it in GitHub Desktop.
Save klzns/64917026b7673df329c87dda2d3c6d36 to your computer and use it in GitHub Desktop.
Search endpoint that is being called by IO GraphQL
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