Replies: 16 comments
-
@nodejs/url |
Beta Was this translation helpful? Give feedback.
-
Hi, const url = new URL("http://example.com");
url.protocol = "https";
url.toString(); // --> 'https://example.com/' Using the legacy API it is possible to construct an url with url.format. What is the way to do this going forward? Using a dummy url to be allowed to create a URL object seems like a hack but will probably be easier than taking in an extra npm package to implement it. Or have I completely missed how to do this in the docs? :) Thank you for your time, |
Beta Was this translation helpful? Give feedback.
-
I think something closer to what you're looking for would be Object.assign(new URL("about:blank"), {
protocol: "https",
hostname: "example.com",
pathname: "/path/to/thing",
hash: "somehash"
}); |
Beta Was this translation helpful? Give feedback.
-
@domenic That wouldn’t work because Object.assign(new URL("https://abc.def/"), {
protocol: "https",
hostname: "example.com",
pathname: "/path/to/thing",
hash: "somehash"
}); |
Beta Was this translation helpful? Give feedback.
-
Thank you for the response! Seems like pretty easy solutions but still a bit hackish to have to enter a dummy url. Would it be possible to remove the requirement for giving an initial url or would that break the spec? |
Beta Was this translation helpful? Give feedback.
-
Seems like either:
|
Beta Was this translation helpful? Give feedback.
-
👍 |
Beta Was this translation helpful? Give feedback.
-
As it is related I wanted to mention the suggestion at whatwg/url#421 for URL.relative, which can always be done in Node without a URL spec. If more people show interest now it could be worthwhile to line up though. |
Beta Was this translation helpful? Give feedback.
-
http.md and http2.md still have plenty of references to require('url').parse('/status?name=ryan') Related: #12682 |
Beta Was this translation helpful? Give feedback.
-
I'm hoping we might have just published something that helps with this. @janicklas-ralph put a bunch of time into porting (in spirit) Node's It contains a number of fixes for the cases noted by @l1bbcsg and others elsewhere. |
Beta Was this translation helpful? Give feedback.
-
Hey, this was working with legacy url
This throws a type error
What to do ? |
Beta Was this translation helpful? Give feedback.
-
Similarly to @rpuls, I'm able to use |
Beta Was this translation helpful? Give feedback.
-
The WHATWG URL parser does not work with relative URL without specifying a base. We cannot deviate from that as it's mandated by the standard. The way to address this is: |
Beta Was this translation helpful? Give feedback.
-
Ahh, ok, sorry I misunderstood the significance of that. Thanks for clarifying. |
Beta Was this translation helpful? Give feedback.
-
How would you replace const url = nodeUrl.format({
protocol: 'https',
host: 'example.com',
pathname: 'somepath' // or '/somepath'
}) By this? const buildUrl = (url) => new URL(url.pathname, url.protocol + '://' + url.host) |
Beta Was this translation helpful? Give feedback.
-
A fully adaptable one-liner replacer for const urlFrom = (urlObject) => String(Object.assign(new URL("https://site.example"), urlObject)) Test it here |
Beta Was this translation helpful? Give feedback.
-
The legacy url API (
url.parse()
,url.format()
, andurl.resolve()
) have been docs-only deprecated.Following the Node.js 11.0.0 release, I plan to open a PR that upgrades that to a pending runtime-deprecation (requires
--pending-deprecation
for the warning to appear). That would land in master but would not go into a release until 12.0.0 (April 2019)Following the 12.0.0 release, I plan to on a PR that upgrades that to a full run-time deprecation that would last in master but would not go into a release until 13.0.0 (October 2019).
After the 13.0.0 release, I would like to move the legacy url API to End-of-Life, allowing us to remove it.
To support existing code that is still using the legacy URL, the implementation can be moved into a separate standalone module published on npm (similar to the existing
node-url
module).Beta Was this translation helpful? Give feedback.
All reactions