Spaces:
Running
Running
File size: 802 Bytes
9ada4bc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import { RequestOptions } from 'http'
/**
* Converts a URL instance into the RequestOptions object expected by
* the `ClientRequest` class.
* @see https://github.com/nodejs/node/blob/908292cf1f551c614a733d858528ffb13fb3a524/lib/internal/url.js#L1257
*/
export function getRequestOptionsByUrl(url: URL): RequestOptions {
const options: RequestOptions = {
method: 'GET',
protocol: url.protocol,
hostname:
typeof url.hostname === 'string' && url.hostname.startsWith('[')
? url.hostname.slice(1, -1)
: url.hostname,
host: url.host,
path: `${url.pathname}${url.search || ''}`,
}
if (!!url.port) {
options.port = Number(url.port)
}
if (url.username || url.password) {
options.auth = `${url.username}:${url.password}`
}
return options
}
|