|
| 1 | +# Node.js URL to WHATWG URL |
| 2 | + |
| 3 | +This recipe converts Node.js `url` module usage to the WHATWG URL API. It modifies code that uses the legacy `url` module to use the modern `URL` class instead. |
| 4 | + |
| 5 | +See [DEP0116](https://nodejs.org/api/deprecations.html#DEP0116). |
| 6 | + |
| 7 | +## Example |
| 8 | + |
| 9 | +### `url.parse` to `new URL()` |
| 10 | + |
| 11 | +**Before:** |
| 12 | +```js |
| 13 | +const url = require('node:url'); |
| 14 | + |
| 15 | +const myURL = url.parse('https://example.com/path?query=string#hash'); |
| 16 | + |
| 17 | +const { auth } = myURL; |
| 18 | +const urlAuth = myURL.auth; |
| 19 | + |
| 20 | +const { path } = myURL; |
| 21 | +const urlPath = myURL.path; |
| 22 | + |
| 23 | +const { hostname } = myURL; |
| 24 | +const urlHostname = myURL.hostname; |
| 25 | +``` |
| 26 | + |
| 27 | +**After:** |
| 28 | +```js |
| 29 | +const myURL = new URL('https://example.com/path?query=string#hash'); |
| 30 | + |
| 31 | +const auth = `${myURL.username}:${myURL.password}`; |
| 32 | +const urlAuth = `${myURL.username}:${myURL.password}`; |
| 33 | + |
| 34 | +const path = `${myURL.pathname}${myURL.search}`; |
| 35 | +const urlPath = `${myURL.pathname}${myURL.search}`; |
| 36 | + |
| 37 | +const hostname = myURL.hostname.replace(/^\[|\]$/, ''); |
| 38 | +const urlHostname = myURL.hostname.replace(/^\[|\]$/, ''); |
| 39 | +``` |
| 40 | + |
| 41 | +### `url.format` to `myUrl.toString() |
| 42 | + |
| 43 | +**Before:** |
| 44 | +```js |
| 45 | +const url = require('node:url'); |
| 46 | + |
| 47 | +url.format({ |
| 48 | + protocol: 'https', |
| 49 | + hostname: 'example.com', |
| 50 | + pathname: '/some/path', |
| 51 | + query: { |
| 52 | + page: 1, |
| 53 | + format: 'json', |
| 54 | + }, |
| 55 | +}); |
| 56 | +``` |
| 57 | + |
| 58 | +**After:** |
| 59 | +```js |
| 60 | +const myUrl = new URL('https://example.com/some/path?page=1&format=json').toString(); |
| 61 | +``` |
| 62 | + |
| 63 | +> **Note:** The migration of `url.format` can also be done as `` `${new URL('https://example.com/some/path?page=1&format=json')}` `` which is little bit more efficient. But it may be less readable for some users. |
| 64 | +
|
| 65 | +## Caveats |
| 66 | + |
| 67 | +The [`url.resolve`](https://nodejs.org/api/url.html#urlresolvefrom-to) method is not directly translatable to the WHATWG URL API. You may need to implement custom logic to handle URL resolution in your application. |
| 68 | + |
| 69 | +```js |
| 70 | +function resolve(from, to) { |
| 71 | + const resolvedUrl = new URL(to, new URL(from, 'resolve://')); |
| 72 | + if (resolvedUrl.protocol === 'resolve:') { |
| 73 | + // `from` is a relative URL. |
| 74 | + const { pathname, search, hash } = resolvedUrl; |
| 75 | + return pathname + search + hash; |
| 76 | + } |
| 77 | + return resolvedUrl.toString(); |
| 78 | +} |
| 79 | + |
| 80 | +resolve('/one/two/three', 'four'); // '/one/two/four' |
| 81 | +resolve('http://example.com/', '/one'); // 'http://example.com/one' |
| 82 | +resolve('http://example.com/one', '/two'); // 'http://example.com/two' |
| 83 | +``` |
| 84 | + |
| 85 | +If you are using `url.parse().auth`, `url.parse().username`, or `url.parse().password`. Will transform to `new URL().auth` which is not valid WHATWG url property. So you have to manually construct the `auth`, `path`, and `hostname` properties as shown in the examples above. |
| 86 | + |
0 commit comments