swagger-js
swagger-js copied to clipboard
replace node:url with native url?
https://github.com/swagger-api/swagger-js/blob/ba0db610cfbd6b716206e2eefe8b46760606b30c/src/index.js#L86-L102
Yeah, don't see a reason why not.
I was triaging this issue a bit and came into conclusion that replacing the url Node.js module with WHATWG API will not be that easy.
url.resolve can be replaced with following function, that's no brainer.
// WHATWG URL API replacement for url.resolve function
export function urlResolve(from, to) {
const resolvedUrl = new URL(to, new URL(from, 'resolve://'));
if (resolvedUrl.protocol === 'resolve:') {
// `from` is a relative URL.
const { pathname, search, hash } = resolvedUrl;
return pathname + search + hash;
}
return resolvedUrl.toString();
}
But url.parse(str) cannot be easily replaced with new URL(str) as the URL constructor expects valid URL as the first parameter and throws error when provided argument is not valid URL.
Found an inconsistency in WHATWG URL parsing. Before this inconsistency is addressed, let's put this issue on hold.
Inconsistency described here: https://github.com/whatwg/url/issues/677
url package have been removed in https://github.com/swagger-api/swagger-js/pull/3137
Codebase now contains helper function for parsing URI References that looks like this:
/**
* `parseURIReference` function simulates the behavior of `node:url` parse function.
* New WHATWG URL API is not capable of parsing relative references natively,
* but can be adapter by utilizing the `base` parameter.
*/
const parseURIReference = (uriReference) => {
try {
return new URL(uriReference);
} catch {
const parsedURL = new URL(uriReference, 'https://swagger.io');
const pathname = String(uriReference).startsWith('/')
? parsedURL.pathname
: parsedURL.pathname.substring(1);
return {
hash: parsedURL.hash,
host: '',
hostname: '',
href: '',
origin: '',
password: '',
pathname,
port: '',
protocol: '',
search: parsedURL.search,
searchParams: parsedURL.searchParams,
};
}
};
:tada: This issue has been resolved in version 3.21.0 :tada:
The release is available on:
Your semantic-release bot :package::rocket: