Skip to content

Instantly share code, notes, and snippets.

@klzns

klzns/catalog-url.js

Last active Aug 11, 2020
Embed
What would you like to do?
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