Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 55 additions & 14 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ const { version } = require('../package.json');
*/
function parseResponse(res, body, resolve, reject) {
try {
if (!/application\/json/.test(res.headers['content-type']) || body.trim() === '') {
if (
!/application\/json/.test(res.headers['content-type']) ||
body.trim() === ''
) {
return resolve(body);
}

Expand All @@ -35,8 +38,15 @@ function parseResponse(res, body, resolve, reject) {
}

class Request {
constructor(hostname, { headers = { }, failOnLimitReached = false, agent = null } = { }) {
if (!hostname) throw new Error('The hostname is required to make the call to the server.');
constructor(
hostname,
{ headers = {}, failOnLimitReached = false, agent = null } = {}
) {
if (!hostname) {
throw new Error(
'The hostname is required to make the call to the server.'
);
}

this.hostname = hostname;
this.headers = headers;
Expand All @@ -45,7 +55,9 @@ class Request {
}

run(method, path, data) {
logger(`Requesting Data from: https://${this.hostname}${path} Using the ${method} method`);
logger(
`Requesting Data from: https://${this.hostname}${path} Using the ${method} method`
);

const dataString = JSON.stringify(data);

Expand All @@ -54,11 +66,14 @@ class Request {
hostname: this.hostname,
method: method.toUpperCase(),
port: 443,
headers: Object.assign({
'User-Agent': 'node-bigcommerce/' + version,
'Content-Type': 'application/json',
'Accept-Encoding': 'gzip, deflate'
}, this.headers)
headers: Object.assign(
{
'User-Agent': 'node-bigcommerce/' + version,
'Content-Type': 'application/json',
'Accept-Encoding': 'gzip, deflate'
},
this.headers
)
};

if (this.agent) options.agent = this.agent;
Expand Down Expand Up @@ -86,13 +101,17 @@ class Request {
const timeToWait = res.headers['x-retry-after'];

if (this.failOnLimitReached) {
const err = new Error(`You have reached the rate limit for the BigCommerce API. Please retry in ${timeToWait} seconds.`);
const err = new Error(
`You have reached the rate limit for the BigCommerce API. Please retry in ${timeToWait} seconds.`
);
err.retryAfter = Number(timeToWait);

return reject(err);
}

logger(`You have reached the rate limit for the BigCommerce API, we will retry again in ${timeToWait} seconds.`);
logger(
`You have reached the rate limit for the BigCommerce API, we will retry again in ${timeToWait} seconds.`
);

return setTimeout(() => {
logger('Restarting request call after suggested time');
Expand All @@ -103,13 +122,34 @@ class Request {
}, timeToWait * 1000);
}

res.on('data', chunk => body += chunk);
res.on('data', chunk => (body += chunk));

res.on('end', () => {
logger('Request complete');

if (res.statusCode >= 400 && res.statusCode <= 600) {
const error = new Error(`Request returned error code: ${res.statusCode} and body: ${body}`);
if (shouldUnzip) {
const unzip =
contentEncoding === 'deflate' ? zlib.deflate : zlib.gunzip;
return unzip(Buffer.from(body, encoding), (err, data) => {
if (err) {
return reject(err);
}

const error = new Error(
`Request returned error code: ${
res.statusCode
} and body: ${data.toString('utf8')}`
);
error.code = res.statusCode;
error.responseBody = data.toString('utf8');
return reject(error);
});
}

const error = new Error(
`Request returned error code: ${res.statusCode} and body: ${body}`
);
error.code = res.statusCode;
error.responseBody = body;

Expand All @@ -118,7 +158,8 @@ class Request {

// Use GZIP decompression if required
if (shouldUnzip) {
const unzip = contentEncoding === 'deflate' ? zlib.deflate : zlib.gunzip;
const unzip =
contentEncoding === 'deflate' ? zlib.deflate : zlib.gunzip;
return unzip(Buffer.from(body, encoding), (err, data) => {
if (err) {
return reject(err);
Expand Down