Skip to content
This repository has been archived by the owner on Oct 21, 2022. It is now read-only.

Commit

Permalink
allow override of configuration.baseUrl by passing full URL with prot…
Browse files Browse the repository at this point in the history
…ocol
  • Loading branch information
taylordowns2000 committed Apr 5, 2022
1 parent a1450a5 commit f7b60a2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/Utils.js
Expand Up @@ -19,10 +19,23 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de

function setUrl(configuration, path) {
const baseUrl = configuration === null || configuration === void 0 ? void 0 : configuration.baseUrl;
if (isValidHttpUrl(path)) return path;
if (baseUrl) return `${baseUrl.replace(/\/$/, '')}/${path.replace(/^\/+/g, '')}`;
return path;
}

function isValidHttpUrl(string) {
let url;

try {
url = new URL(string);
} catch (_) {
return false;
}

return url.protocol === 'http:' || url.protocol === 'https:';
}

function setAuth(configuration, manualAuth) {
if (manualAuth) return manualAuth;else if (configuration && configuration.username) return {
username: configuration.username,
Expand Down
14 changes: 14 additions & 0 deletions src/Utils.js
Expand Up @@ -5,12 +5,26 @@ import safeStringify from 'fast-safe-stringify';
export function setUrl(configuration, path) {
const baseUrl = configuration?.baseUrl;

if (isValidHttpUrl(path)) return path;

if (baseUrl)
return `${baseUrl.replace(/\/$/, '')}/${path.replace(/^\/+/g, '')}`;

return path;
}

function isValidHttpUrl(string) {
let url;

try {
url = new URL(string);
} catch (_) {
return false;
}

return url.protocol === 'http:' || url.protocol === 'https:';
}

export function setAuth(configuration, manualAuth) {
if (manualAuth) return manualAuth;
else if (configuration && configuration.username)
Expand Down

0 comments on commit f7b60a2

Please sign in to comment.