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:
23
src/core/utils/url.js
Normal file
23
src/core/utils/url.js
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user