Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: add ESM code examples in url.md #38651

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions doc/api/url.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
The `url` module provides utilities for URL resolution and parsing. It can be
accessed using:

```js
```mjs
import url from 'url';
```
```cjs
const url = require('url');
```

Expand Down Expand Up @@ -61,7 +64,12 @@ const myURL =

Parsing the URL string using the Legacy API:

```js
```mjs
import url from 'url';
const myURL =
url.parse('https://user:[email protected]:8080/p/a/t/h?query=string#hash');
```
```cjs
const url = require('url');
const myURL =
url.parse('https://user:[email protected]:8080/p/a/t/h?query=string#hash');
Expand Down Expand Up @@ -135,7 +143,11 @@ const myURL = new URL('/foo', 'https://example.org/');
The URL constructor is accessible as a property on the global object.
It can also be imported from the built-in url module:

```js
```mjs
import { URL } from 'url';
console.log(URL === globalThis.URL); // Prints 'true'.
```
```cjs
console.log(URL === require('url').URL); // Prints 'true'.
```

Expand Down Expand Up @@ -573,10 +585,6 @@ and [`url.format()`][] methods would produce.
The `toString()` method on the `URL` object returns the serialized URL. The
value returned is equivalent to that of [`url.href`][] and [`url.toJSON()`][].

Because of the need for standard compliance, this method does not allow users
to customize the serialization process of the URL. For more flexibility,
[`require('url').format()`][] method might be of interest.

#### `url.toJSON()`

* Returns: {string}
Expand Down Expand Up @@ -932,7 +940,6 @@ invalid domain, the empty string is returned.
It performs the inverse operation to [`url.domainToUnicode()`][].

```js
const url = require('url');
console.log(url.domainToASCII('español.com'));
// Prints xn--espaol-zwa.com
console.log(url.domainToASCII('中文.com'));
Expand All @@ -957,7 +964,6 @@ domain, the empty string is returned.
It performs the inverse operation to [`url.domainToASCII()`][].

```js
const url = require('url');
console.log(url.domainToUnicode('xn--espaol-zwa.com'));
// Prints español.com
console.log(url.domainToUnicode('xn--fiq228c.com'));
Expand Down Expand Up @@ -1080,7 +1086,6 @@ This utility function converts a URL object into an ordinary options object as
expected by the [`http.request()`][] and [`https.request()`][] APIs.

```js
const { urlToHttpOptions } = require('url');
const myURL = new URL('https://a:b@測試?abc#foo');

console.log(urlToHttpOptions(myUrl));
Expand Down Expand Up @@ -1124,8 +1129,8 @@ changes:

> Stability: 3 - Legacy: Use the WHATWG URL API instead.

The legacy `urlObject` (`require('url').Url`) is created and returned by the
`url.parse()` function.
The legacy `urlObject` (`require('url').Url` or `import { Url } from 'url'`) is
created and returned by the `url.parse()` function.

#### `urlObject.auth`

Expand Down Expand Up @@ -1499,7 +1504,6 @@ console.log(myURL.origin);
[`https.request()`]: https.md#https_https_request_options_callback
[`new URL()`]: #url_new_url_input_base
[`querystring`]: querystring.md
[`require('url').format()`]: #url_url_format_url_options
[`url.domainToASCII()`]: #url_url_domaintoascii_domain
[`url.domainToUnicode()`]: #url_url_domaintounicode_domain
[`url.format()`]: #url_url_format_urlobject
Expand Down