Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
This is a high-level JavaScript / Node.js helper for the Best Buy developer API.
The examples folder contains code that demonstrates how to include the module and initialize it with your Best Buy developer key and then programmatically call the API.
Example of including the NPM module:
Pre-ES6 with explicit initialization
var bby = require('bestbuy')("your_BBY_API_KEY");
In addition to the examples, the package contains a suite of Jasmine tests to further show how to use the helper in more ways.
npm install bestbuy --save
"bestbuy": "1.*"
inside of the dependencies part of your package.json
fileBBY_API_KEY
to your key and invoke the methodvar bby = require('bestbuy')();
var bby = require('bestbuy')('YOURKEY');
var bby = require('bestbuy')({'key': 'YOURKEY'});
In our documentation, we'll use a couple actual examples:
4312001
, which is the Star Wars Episode IV: A New Hope (Blu-ray)611
and 482
, which are respectfully the Best Buy stores by corporate headquarters and in New York where Chloe works.More examples are available in the examples directory
Node.js streams are supported via these endpoints:
availabilityAsStream
openBoxAsStream
(xml format not supported)categoriesAsStream
productsAsStream
storesAsStream
For Streams:
objectMode
, so each data chunk will be an object.<product><sku>123</sku></product>
)
total
event will always be emitted once per stream with the total number of results.data
event will be emitted for each item in the result (one per product/store/etc).error
event will be emitted once maxRetries
is exceeded.availability(sku, array of store ids[, query string object])
This method supports an optional third parameter that represents extra attributes, such as show
, to be added to the query string sent to the API.
var bby = require('bestbuy')('YOURKEY');
bby.availability(4312001, [611, 482], function(err, data) {
if (err) console.warn(err);
else console.log('Stores carrying %s: %d', data.products[0].name, data.products[0].stores.length);
});
var bby = require('bestbuy')('YOURKEY');
bby.availability(4312001, [611, 482])
.then(function(data){
console.log('Stores carrying %s: %d', data.products[0].name, data.products[0].stores.length);
})
.catch(function(err){
console.warn(err);
});
var stream = bby.availabilityAsStream(5670003, [611, 15, 6, 10, 7, 1055, 1000, 281, 245, 11, 8]);
stream.on('total', function (total) { console.log('Total Products: ' + total); });
stream.on('data', function (data) {
console.log(`\nProduct "${data.name}" available at:\n${data.stores.map(store => ` - ${store.longName}`).join('\n')}`);
});
stream.on('error', function (error) {
console.error(`Error status: ${error.status}`);
console.error(`Error headers: ${error.headers}`);
console.error(`Error body: ${error.body}`);
console.error(`Error cause: ${error.cause}`);
})
realTimeAvailability(sku, {postalCode: postalCode}
realTimeAvailability(sku, {storeId: storeId}
This method provides near real-time inventory levels for a sku. This endpoint only support JSON responses.
bby.realTimeAvailability(4312001, {storeId: 611}, function (err, data) {
if (err) console.warn(err);
console.log(`Sku 4312001 in store pickup availability: ${data.ispuEligible}`);
if(data.stores.length === 1) {
console.log(`Sku 4312001 availability in the 611 store: ${JSON.stringify(data.stores[0])}`);
} else {
console.log(`Sku 4312001 is not available in the 611 store`);
}
});
bby.realTimeAvailability(4312001, {postalCode: 55454})
.then(function(data) {
console.log(`Sku 4312001 in store pickup availability: ${data.ispuEligible}`);
if(data.stores.length > 0) {
console.log(`Sku 4312001 availability in the 55454 area: ${JSON.stringify(data.stores)}`);
} else {
console.log(`Sku 4312001 is not available in the 55454 area`);
}
})
.catch(function (err) {
console.warn(err);
});
categories(String of search criteria[, query string object])
This endpoint serves the search criteria for querying the Category API as described in our API documentation.
The below example returns the first category with the word "music" in it.
var bby = require('bestbuy')('YOURKEY');
bby.categories('(name=Music)', {pageSize: 1}, function(err, data) {
if (err) console.warn(err);
else if (data.total === 0) console.log('No categories found');
else console.log('Found %d categories. First category (%s): %s', data.total, data.categories[0].id, data.categories[0].name);
});
var bby = require('bestbuy')('YOURKEY');
bby.categories('(name=Music)', {pageSize: 1})
.then(function(data){
if (data.total === 0) console.log('No categories found');
else console.log('Found %d categories. First category (%s): %s', data.total, data.categories[0].id, data.categories[0].name);
})
.catch(function(err){
console.warn(err);
});
// lets write all categories to a file called categories.json
var JSONStream = require('JSONStream');
var categories = bby.categoriesAsStream('');
// a "total" event is emitted so we know how many total products will be sent
categories.on('total', total => console.log(`Total Categories: ${total}`));
categories
.pipe(JSONStream.stringify())
.pipe(fs.createWriteStream('categories.json'));
// log when its done
categories.on('end', () => {
console.log('Done!');
});
openBox(sku, array of store ids)
This endpoint serves the search criteria for querying the Buying Options API as described in our API documentation.
This example searches all open box products in the video games category, and returns the first result.
var bby = require('bestbuy')('YOURKEY');
bby.openBox('categoryId=abcat0700000', function(err, data) {
if (err) console.warn(err);
else if (data.metadata.resultSet.count === 0) console.log('No Open Box products available');
else {
console.log('Found %d Open Box products', data.metadata.resultSet.count);
console.log('First Open Box product:');
console.log('\tName: %s', data.results[0].names.title);
console.log('\tURL: %s', data.results[0].links.web);
console.log('\tPrice: $%d', data.results[0].prices.current);
}
});
var bby = require('bestbuy')('YOURKEY');
bby.openBox('categoryId=abcat0700000')
.then(function(data){
if (data.metadata.resultSet.count === 0) console.log('No Open Box products available');
else {
console.log('Found %d Open Box products', data.metadata.resultSet.count);
console.log('First Open Box product:');
console.log('\tName: %s', data.results[0].names.title);
console.log('\tURL: %s', data.results[0].links.web);
console.log('\tPrice: $%d', data.results[0].prices.current);
}
})
.catch(function(data){
console.warn(err);
});
var stream = bby.openBoxAsStream('categoryId=abcat0502000');
stream.on('data', data => {});
stream.on('total', (t) => { console.log('Total: ' + total) });
stream.on('data', (t) => { console.log('Open box Item: ', data) });
products(String of search criteria[, query string object])
This endpoint serves the search criteria for querying the Products API as described in our API documentation.
The below example returns the title and price of the first search result with the word "Mario" in it.
var bby = require('bestbuy')('YOURKEY');
bby.products('(search=mario)', {show: 'salePrice,name', pageSize: 1}, function(err, data) {
if (err) console.warn(err);
else if (data.total === 0) console.log('No products found');
else console.log('Found %d products. First match "%s" is $%d', data.total, data.products[0].name, data.products[0].salePrice);
});
var bby = require('bestbuy')('YOURKEY');
bby.products('(search=mario)', {show: 'salePrice,name', pageSize: 1})
.then(function(data){
if (data.total === 0) console.log('No products found');
else console.log('Found %d products. First match "%s" is $%d', data.total, data.products[0].name, data.products[0].salePrice);
})
.catch(function(err){
console.warn(err);
});
var productsStream = bby.productsAsStream('customerReviewAverage=5&name=red*', {
show: 'name,sku'
});
// a "total" event is emitted so we know how many total products will be sent
productsStream.on('total', total => console.log(`Total Products: ${total}`));
// log each product to the console
productsStream.on('data', product => { console.log(`Product: ${JSON.stringify(product, null, 0)}`); });
// log when its done
productsStream.on('end', () => console.log('Done!'));
recommendations('mostViewed' OR 'trendingViewed'[, optional category as a string])
recommendations('alsoViewed', sku)
This endpoint serves the search criteria for querying the Recommendations API as described in our API documentation.
The first parameter expects one of three values: mostViewed, trendingViewed or alsoViewed. If the first parameter is mostViewed or trendingViewed, an optional second parameter of a categoryId may be provided. If the first parameter is alsoViewed, a required second parameter of sku must be provided.
The below examples show how to get the most viewed products on BestBuy.com.
var bby = require('bestbuy')('YOURKEY');
bby.recommendations('mostViewed', function(err, data) {
if (err) console.warn(err);
else if (data.metadata.resultSet.count === 0) console.log('Did not find any products');
else console.log('Found %d products. First product: %s', data.metadata.resultSet.count, data.results[0].names.title);
});
var bby = require('bestbuy')('YOURKEY');
bby.recommendations('mostViewed')
.then(function(data){
if (data.metadata.resultSet.count === 0) console.log('Did not find any products');
else console.log('Found %d products. First product: %s', data.metadata.resultSet.count, data.results[0].names.title);
})
.catch(function(err){
console.warn(err);
});
Streams are not supported for recommendations.
stores(String of search criteria)
This endpoint serves the search criteria for querying the Stores API as described in our API documentation.
The below examples show the number of stores located within 25 miles of 94103 (San Francisco, CA).
var bby = require('bestbuy')('YOURKEY');
bby.stores('area(94103,25)&storeType=BigBox', function(err, data){
if (err) console.warn(err);
else console.log('Number of stores found: ' + data.total);
});
var bby = require('bestbuy')('YOURKEY');
bby.stores('area(94103,25)&storeType=BigBox')
.then(function(data){
console.log('Number of stores found: ' + data.total);
})
.catch(function(err){
console.warn(err);
});
var bby = require('bestbuy')('YOURKEY');
bby.storesAsStream('area(94103,25)&storeType=BigBox');
stream.on('total', function (total) { console.log('Total Stores: ' + total); });
stream.on('data', function (store) {
console.log(`\Store: ${store.name} - Phone: ${store.phone}}`);
});
warranties(sku)
This endpoint serves warranties for a specified product per the Warranties API as described in our API documentation.
The below examples show warranties for an old printer.
var bby = require('bestbuy')('YOURKEY');
bby.warranties(6354884, function(err, data){
if (err) console.warn(err);
else console.log('Warranties found: ' + data.length);
});
var bby = require('bestbuy')('YOURKEY');
bby.warranties(6354884)
.then(function(data){
console.log('Warranties found: ' + data.length);
})
.catch(function(err){
console.warn(err);
});
Streams are not supported for warranties.
version()
This endpoint will return the version of the API and this package version.
var bby = require('bestbuy')('YOURKEY');
bby.version(function(err, data) {
if (err) console.warn(err);
else console.log(data);
// output JSON:
// { packageVersion: '2.0.0', apiVersion: '1.0.844' }
});
var bby = require('bestbuy')('YOURKEY');
bby.version().then(function(data) {
console.log(version);
})
.catch(function(err){
console.warn(err);
});
// output JSON:
// { packageVersion: '2.0.0', apiVersion: '1.0.844' }
Streams are not supported.
Debug can be enabled via the debug
attribute:
var bby = require('bestbuy')({
key: 'YOURKEY',
debug: true
});
// output request and response info to console.log
If console.log
is not desired, a custom debug function can be passed:
var bby = require('bestbuy')({
key: 'YOURKEY',
debug: function (debugObject) { myCustomLogging.info(debugObject) }
});
// The debug function will be called for each request, response, and error thrown by the internal HTTP client.
In order to avoid 429 Too Many Requests
errors, all API calls are automatically throttled to 5 requests per second. This value aligns with the default limit for approved API keys. Setting a lower value can be useful if a key needs to be shared among multiple application instances.
If you need to adjust this value, it can be done via the requestsPerSecond
option:
var bby = require('bestbuy')({
key: 'YOURKEY',
requestsPerSecond: 1 // just one request per second
});
By default, requests will be attempted only once before erroring. The maximum number of retries and the time taken between retries can be configured with the maxRetries
and retryInterval
options. retryInterval
is set to 2000 by default.
var bby = require('bestbuy')({
key: 'YOURKEY',
maxRetries: 4, // make five requests at most
retryInterval: 5000, // wait 5 seconds before retrying
});
Request timeout can be configured with the timeout
option. This value is set to 5000ms by default.
var bby = require('bestbuy')({
key: 'YOURKEY',
timeout: 15000 // wait for 15 seconds before erroring due to timeout
});
Run the existing tests with:
npm test
Note that this uses nock fixtures to mock all network calls.
Run tests against the live API (BBY_API_KEY env var needs to be set):
npm run test-live
If tests are added, re-record the mock fixtures:
npm run record
# tests will run against the live API and their output will be saved in test/fixtures
FAQs
High level node.js client for the Best Buy API.
The npm package bestbuy receives a total of 17 weekly downloads. As such, bestbuy popularity was classified as not popular.
We found that bestbuy demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.