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>
This commit is contained in:
geraldglynn
2020-08-04 21:21:19 +01:00
committed by GitHub
parent 225a915cf8
commit d9f5691f65
11 changed files with 174 additions and 31 deletions

23
src/core/utils/url.js Normal file
View File

@@ -0,0 +1,23 @@
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
}