bv-ui-core
Advanced tools
Comparing version 2.2.0 to 2.3.0
@@ -10,6 +10,6 @@ /* global fetch */ | ||
production: 'api' | ||
} | ||
}; | ||
// Default configuration | ||
const API_VERSION = 5.4 | ||
const API_VERSION = 5.4; | ||
const MAX_REQUESTED_IDS = 100; | ||
@@ -52,81 +52,99 @@ | ||
*/ | ||
function getStatistics ({ productIds, environment, key, type, filters = {} }) { | ||
function getStatistics ({ | ||
productIds, | ||
environment, | ||
key, | ||
type, | ||
incentivized = false, | ||
filters = {} | ||
}) { | ||
if (!productIds || !Array.isArray(productIds)) { | ||
throw new TypeError('productIds must be an array') | ||
throw new TypeError('productIds must be an array'); | ||
} | ||
const envPrefix = envPrefixMap[environment] | ||
const envPrefix = envPrefixMap[environment]; | ||
if (!envPrefix) { | ||
throw new TypeError( | ||
'environment must be \'qa\', \'staging\', or \'production\'' | ||
) | ||
throw new TypeError('environment must be "qa", "staging", or "production"'); | ||
} | ||
if (!key) { | ||
throw new TypeError('key must be provided') | ||
throw new TypeError('key must be provided'); | ||
} | ||
if (!type || !(type === 'Reviews' || type === 'NativeReviews')) { | ||
throw new TypeError('type must be \'Reviews\' or \'NativeReviews\'') | ||
throw new TypeError('type must be "Reviews" or "NativeReviews"'); | ||
} | ||
const uri = `https://${envPrefix}.bazaarvoice.com/data/statistics.json` + | ||
`?apiversion=${API_VERSION}` + | ||
`&passkey=${key}` + | ||
`&stats=${type}` + | ||
Object.keys(filters) | ||
.map(filter => `&filter=${filter}:${filters[filter]}`) | ||
.join() | ||
let uri = | ||
`https://${envPrefix}.bazaarvoice.com/data/statistics.json` + | ||
`?apiversion=${API_VERSION}` + | ||
`&passkey=${key}` + | ||
`&stats=${type}` + | ||
Object.keys(filters) | ||
.map(filter => `&filter=${filter}:${filters[filter]}`) | ||
.join(); | ||
// We should add `incentivizedStats` request param in case | ||
// if `incentivized` flag is enabled that means such reviews | ||
// have to be loaded in response | ||
if (incentivized) { | ||
uri = uri + `&incentivizedStats=${incentivized}`; | ||
} | ||
// Clone the productIds so we can manipulate it. | ||
productIds = [...productIds] | ||
const productIdChunks = [] | ||
productIds = [...productIds]; | ||
const productIdChunks = []; | ||
if (productIds.length > 100) { | ||
console.warn('Requesting more than 100 products is not recommended!') | ||
console.warn('Requesting more than 100 products is not recommended!'); | ||
} | ||
while (productIds.length > 0) { | ||
productIdChunks.push(productIds.splice(0, MAX_REQUESTED_IDS)) | ||
productIdChunks.push(productIds.splice(0, MAX_REQUESTED_IDS)); | ||
} | ||
return Promise.all( | ||
productIdChunks.map(products => new Promise((resolve, reject) => { | ||
const requestUri = `${uri}&filter=ProductId:${products.join(',')}` | ||
return fetch(requestUri) | ||
.then(response => response.json()) | ||
.then(json => { | ||
// If there are errors in the actual response body (from API) | ||
// we need to handle them here. | ||
if (json.HasErrors) { | ||
let errors = json.Errors | ||
// If for some reason errors is empty or doesn't exist, | ||
if (!errors || !Array.isArray(errors) || errors.length <= 0) { | ||
reject({ | ||
Message: 'An unknown error occurred.', | ||
Code: 'ERROR_UNKNOWN' | ||
}) | ||
} | ||
else { | ||
// We can reasonably assume that if an error occurred for this | ||
// request it should only have a single error response | ||
// unlike submission api errors, which have multiples. | ||
reject(json.Errors[0]) | ||
} | ||
} | ||
else { | ||
resolve(json.Results) | ||
} | ||
productIdChunks.map( | ||
products => | ||
new Promise((resolve, reject) => { | ||
const requestUri = `${uri}&filter=ProductId:${products.join(',')}`; | ||
return fetch(requestUri) | ||
.then(response => response.json()) | ||
.then(json => { | ||
// If there are errors in the actual response body (from API) | ||
// we need to handle them here. | ||
if (json.HasErrors) { | ||
let errors = json.Errors; | ||
// If for some reason errors is empty or doesn't exist, | ||
if (!errors || !Array.isArray(errors) || errors.length <= 0) { | ||
reject({ | ||
Message: 'An unknown error occurred.', | ||
Code: 'ERROR_UNKNOWN', | ||
}); | ||
} | ||
else { | ||
// We can reasonably assume that if an error occurred for this | ||
// request it should only have a single error response | ||
// unlike submission api errors, which have multiples. | ||
reject(json.Errors[0]); | ||
} | ||
} | ||
else { | ||
resolve(json.Results); | ||
} | ||
}); | ||
}) | ||
})) | ||
) | ||
).then(results => { | ||
if (results.length === 0) { | ||
return results | ||
} | ||
return results; | ||
} | ||
else { | ||
return results.reduce((a,b) => { return a.concat(b) }) | ||
return results.reduce((a, b) => { | ||
return a.concat(b); | ||
}); | ||
} | ||
}) | ||
}); | ||
} | ||
module.exports = getStatistics | ||
module.exports = getStatistics; |
{ | ||
"name": "bv-ui-core", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"license": "Apache 2.0", | ||
@@ -11,3 +11,3 @@ "description": "Bazaarvoice UI-related JavaScript", | ||
"engines": { | ||
"node": ">=6.10" | ||
"node": ">=10.*" | ||
}, | ||
@@ -14,0 +14,0 @@ "scripts": { |
@@ -106,3 +106,3 @@ /** | ||
} | ||
})).to.throw('environment must be \'qa\', \'staging\', or \'production\'') | ||
})).to.throw('environment must be "qa", "staging", or "production"') | ||
}) | ||
@@ -122,3 +122,3 @@ | ||
} | ||
})).to.throw('environment must be \'qa\', \'staging\', or \'production\'') | ||
})).to.throw('environment must be "qa", "staging", or "production"') | ||
} | ||
@@ -146,3 +146,3 @@ }) | ||
} | ||
})).to.throw('type must be \'Reviews\' or \'NativeReviews\'') | ||
})).to.throw('type must be "Reviews" or "NativeReviews"') | ||
}) | ||
@@ -159,3 +159,3 @@ | ||
type: 'reviews' | ||
})).to.throw('type must be \'Reviews\' or \'NativeReviews\'') | ||
})).to.throw('type must be "Reviews" or "NativeReviews"') | ||
}) | ||
@@ -162,0 +162,0 @@ }) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
219707
5196
6