Files
swagger-ui/src/core/utils/url.js
geraldglynn d9f5691f65 fix(OAS3): relative urls (#5341)
* Added tooling for appending OAS3 relative URLs to selected Server

Info
* Terms of service URL
* Contact URL
* License URL
* External Docs URL

Tag
* Tag External Docs URL

Operation
* Operation External Docs
** Operation Tag


Co-authored-by: Tim Lai <timothy.lai@gmail.com>
2020-08-04 13:21:19 -07:00

24 lines
757 B
JavaScript

export function isAbsoluteUrl(url) {
return url.match(/^(?:[a-z]+:)?\/\//i) // Matches http://, HTTP://, https://, ftp://, //example.com,
}
export function addProtocol(url) {
if(!url.match(/^\/\//i)) return url // Checks if protocol is missing e.g. //example.com
return `${window.location.protocol}${url}`
}
export function buildBaseUrl(selectedServer, specUrl) {
if(!selectedServer) return specUrl
if(isAbsoluteUrl(selectedServer)) return addProtocol(selectedServer)
return new URL(selectedServer, specUrl).href
}
export function buildUrl(url, specUrl, { selectedServer="" } = {}) {
if(!url) return
if(isAbsoluteUrl(url)) return url
const baseUrl = buildBaseUrl(selectedServer, specUrl)
return new URL(url, baseUrl).href
}