blob: 32981d133b7d471b1b8a4baff62bf06ef8d5051f [file] [log] [blame]
"use strict";
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
Object.defineProperty(exports, "__esModule", { value: true });
const url = require("url");
const path = require("path");
/**
* creates an url from a request url and optional base url (http://server:8080)
* @param {string} resource - a fully qualified url or relative path
* @param {string} baseUrl - an optional baseUrl (http://server:8080)
* @return {string} - resultant url
*/
function getUrl(resource, baseUrl) {
const pathApi = path.posix || path;
if (!baseUrl) {
return resource;
}
else if (!resource) {
return baseUrl;
}
else {
const base = url.parse(baseUrl);
const resultantUrl = url.parse(resource);
// resource (specific per request) elements take priority
resultantUrl.protocol = resultantUrl.protocol || base.protocol;
resultantUrl.auth = resultantUrl.auth || base.auth;
resultantUrl.host = resultantUrl.host || base.host;
resultantUrl.pathname = pathApi.resolve(base.pathname, resultantUrl.pathname);
if (!resultantUrl.pathname.endsWith('/') && resource.endsWith('/')) {
resultantUrl.pathname += '/';
}
return url.format(resultantUrl);
}
}
exports.getUrl = getUrl;