ketting
Version:
Opiniated HATEAOS / Rest client.
46 lines • 1.47 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.parse = exports.resolve = void 0;
function resolve(base, relative) {
var _a;
if (typeof base !== 'string') {
relative = base.href;
base = base.context;
}
// If the URL object is supported, we prefer that.
if (typeof URL !== 'undefined' && ((_a = /https?:\/\//.exec(base)) === null || _a === void 0 ? void 0 : _a.index) === 0) {
return (new URL(relative, base).toString());
}
// Code taken from this gist:;
// https://gist.github.com/johan/3915545#file-resolveurl-js
const doc = document;
const oldBase = doc.getElementsByTagName('base')[0];
const oldHref = oldBase && oldBase.href;
const docHead = doc.head || doc.getElementsByTagName('head')[0];
const ourBase = oldBase || docHead.appendChild(doc.createElement('base'));
const resolver = doc.createElement('a');
ourBase.href = base;
resolver.href = relative;
const resolvedUrl = resolver.href; // browser magic at work here
if (oldBase) {
oldBase.href = oldHref;
}
else {
docHead.removeChild(ourBase);
}
return resolvedUrl;
}
exports.resolve = resolve;
/**
* Parses a url in multiple components.
*
* This is the browser-based version.
*/
function parse(url) {
const urlObj = new URL(url);
return {
host: urlObj.host,
};
}
exports.parse = parse;
//# sourceMappingURL=uri.web.js.map