traverson
Version:
Hypermedia API/HATEOAS client for Node.js and the browser
32 lines (28 loc) • 1.19 kB
JavaScript
;
var minilog = require('minilog')
, log = minilog('traverson')
, _s = require('underscore.string')
, url = require('url');
var protocolRegEx = /https?:\/\//i;
module.exports = function resolveNextUrl(t) {
if (t.step.url) {
if (t.step.url.search(protocolRegEx) !== 0) {
log.debug('found non full qualified URL');
if (t.resolveRelative && t.lastStep && t.lastStep.url) {
// edge case: resolve URL relatively (only when requested by client)
log.debug('resolving URL relative');
if (_s.startsWith(t.step.url, '/') &&
_s.endsWith(t.lastStep.url, '/')) {
t.step.url = _s.splice(t.step.url, 0, 1);
}
t.step.url = t.lastStep.url + t.step.url;
} else {
// This is the default case and what happens most likely (not a full
// qualified URL, not resolving relatively) and we simply use Node's url
// module (or the appropriate shim) here.
t.step.url = url.resolve(t.startUrl, t.step.url);
}
} // edge case: full qualified URL -> no URL resolving necessary
} // no t.step.url -> no URL resolving (step might contain an embedded doc)
return true;
};