Skip to content
Merged
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
61 changes: 25 additions & 36 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,9 +414,12 @@ var Client = module.exports = function(config) {
};

function getPage(link, which, headers, callback) {
var self = this;
var url = getPageLinks(link)[which];
if (!url)
return callback(new error.NotFound("No " + which + " page found"));
if (!url) {
var urlErr = new error.NotFound("No " + which + " page found");
return self.Promise && !callback ? self.Promise.reject(urlErr) : callback(urlErr);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth throwing an error if neither promise or callback defined, like below?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Up to you. Not a dealbreaker without it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll leave it be 😄 , I'm sure my lazy developer habits will come back to haunt me.

}

var parsedUrl = Url.parse(url, true);

Expand All @@ -430,38 +433,24 @@ var Client = module.exports = function(config) {
params: parsedUrl.query
};

var self = this;
this.httpSend(msg, block, function(err, res) {
if (err)
return self.sendError(err, null, parsedUrl.query, callback);

var ret;
try {
ret = res.data;
var contentType = res.headers["content-type"];
if (contentType && contentType.indexOf("application/json") !== -1)
ret = JSON.parse(ret);
}
catch (ex) {
if (callback)
callback(new error.InternalServerError(ex.message), res);
return;
}

if (!ret)
ret = {};
if (typeof ret == "object") {
if (!ret.meta)
ret.meta = {};
self.responseHeaders.forEach(function(header) {
if (res.headers[header])
ret.meta[header] = res.headers[header];
if (!callback) {
if (self.Promise) {
return new self.Promise(function(resolve,reject) {
var cb = function(err, obj) {
if (err) {
reject(err);
} else {
resolve(obj);
}
};
self.handler(msg, JSON.parse(JSON.stringify(block)), cb);
});
} else {
throw new Error('neither a callback or global promise implementation was provided');
}

if (callback)
callback(null, ret);
});
} else {
self.handler(msg, JSON.parse(JSON.stringify(block)), callback);
}
}

/**
Expand All @@ -477,7 +466,7 @@ var Client = module.exports = function(config) {
callback = headers;
headers = null;
}
getPage.call(this, link, "next", headers, callback);
return getPage.call(this, link, "next", headers, callback);
};

/**
Expand All @@ -493,7 +482,7 @@ var Client = module.exports = function(config) {
callback = headers;
headers = null;
}
getPage.call(this, link, "prev", headers, callback);
return getPage.call(this, link, "prev", headers, callback);
};

/**
Expand All @@ -509,7 +498,7 @@ var Client = module.exports = function(config) {
callback = headers;
headers = null;
}
getPage.call(this, link, "last", headers, callback);
return getPage.call(this, link, "last", headers, callback);
};

/**
Expand All @@ -525,7 +514,7 @@ var Client = module.exports = function(config) {
callback = headers;
headers = null;
}
getPage.call(this, link, "first", headers, callback);
return getPage.call(this, link, "first", headers, callback);
};

function getRequestFormat(hasBody, block) {
Expand Down