diff --git a/types/node/v14/test/url.ts b/types/node/v14/test/url.ts index 870112cdf3e195..9c35ac45e403b0 100644 --- a/types/node/v14/test/url.ts +++ b/types/node/v14/test/url.ts @@ -1,4 +1,5 @@ import assert = require('node:assert'); +import { RequestOptions } from 'node:http'; import * as url from 'node:url'; { @@ -155,6 +156,10 @@ import * as url from 'node:url'; const path: url.URL = url.pathToFileURL('file://test'); } +{ + const opts: RequestOptions = url.urlToHttpOptions(new url.URL('test.com')); +} + { const dataUrl1: URL = new url.URL('file://test'); const dataUrl2: url.URL = new URL('file://test'); diff --git a/types/node/v14/ts4.8/test/url.ts b/types/node/v14/ts4.8/test/url.ts index 870112cdf3e195..9c35ac45e403b0 100644 --- a/types/node/v14/ts4.8/test/url.ts +++ b/types/node/v14/ts4.8/test/url.ts @@ -1,4 +1,5 @@ import assert = require('node:assert'); +import { RequestOptions } from 'node:http'; import * as url from 'node:url'; { @@ -155,6 +156,10 @@ import * as url from 'node:url'; const path: url.URL = url.pathToFileURL('file://test'); } +{ + const opts: RequestOptions = url.urlToHttpOptions(new url.URL('test.com')); +} + { const dataUrl1: URL = new url.URL('file://test'); const dataUrl2: url.URL = new URL('file://test'); diff --git a/types/node/v14/ts4.8/url.d.ts b/types/node/v14/ts4.8/url.d.ts index bc1a17a18107dd..507215cec9dec5 100644 --- a/types/node/v14/ts4.8/url.d.ts +++ b/types/node/v14/ts4.8/url.d.ts @@ -1,4 +1,5 @@ declare module 'url' { + import { ClientRequestArgs } from 'node:http'; import { ParsedUrlQuery, ParsedUrlQueryInput } from 'querystring'; // Input to `url.format` @@ -71,6 +72,33 @@ declare module 'url' { * @param url The path to convert to a File URL. */ function pathToFileURL(url: string): URL; + /** + * This utility function converts a URL object into an ordinary options object as + * expected by the `http.request()` and `https.request()` APIs. + * + * ```js + * import { urlToHttpOptions } from 'url'; + * const myURL = new URL('https://a:b@測試?abc#foo'); + * + * console.log(urlToHttpOptions(myURL)); + * + * { + * protocol: 'https:', + * hostname: 'xn--g6w251d', + * hash: '#foo', + * search: '?abc', + * pathname: '/', + * path: '/?abc', + * href: 'https://a:b@xn--g6w251d/?abc#foo', + * auth: 'a:b' + * } + * + * ``` + * @since v14.18.0 + * @param url The `WHATWG URL` object to convert to an options object. + * @return Options object + */ + function urlToHttpOptions(url: URL): ClientRequestArgs; interface URLFormatOptions { auth?: boolean | undefined; diff --git a/types/node/v14/url.d.ts b/types/node/v14/url.d.ts index bc1a17a18107dd..507215cec9dec5 100644 --- a/types/node/v14/url.d.ts +++ b/types/node/v14/url.d.ts @@ -1,4 +1,5 @@ declare module 'url' { + import { ClientRequestArgs } from 'node:http'; import { ParsedUrlQuery, ParsedUrlQueryInput } from 'querystring'; // Input to `url.format` @@ -71,6 +72,33 @@ declare module 'url' { * @param url The path to convert to a File URL. */ function pathToFileURL(url: string): URL; + /** + * This utility function converts a URL object into an ordinary options object as + * expected by the `http.request()` and `https.request()` APIs. + * + * ```js + * import { urlToHttpOptions } from 'url'; + * const myURL = new URL('https://a:b@測試?abc#foo'); + * + * console.log(urlToHttpOptions(myURL)); + * + * { + * protocol: 'https:', + * hostname: 'xn--g6w251d', + * hash: '#foo', + * search: '?abc', + * pathname: '/', + * path: '/?abc', + * href: 'https://a:b@xn--g6w251d/?abc#foo', + * auth: 'a:b' + * } + * + * ``` + * @since v14.18.0 + * @param url The `WHATWG URL` object to convert to an options object. + * @return Options object + */ + function urlToHttpOptions(url: URL): ClientRequestArgs; interface URLFormatOptions { auth?: boolean | undefined;