UNPKG

588 BJavaScriptView Raw
1'use strict';
2const is = require('@sindresorhus/is');
3
4module.exports = url => {
5 const options = {
6 protocol: url.protocol,
7 hostname: url.hostname.startsWith('[') ? url.hostname.slice(1, -1) : url.hostname,
8 hash: url.hash,
9 search: url.search,
10 pathname: url.pathname,
11 href: url.href
12 };
13
14 if (is.string(url.port) && url.port.length > 0) {
15 options.port = Number(url.port);
16 }
17
18 if (url.username || url.password) {
19 options.auth = `${url.username}:${url.password}`;
20 }
21
22 options.path = is.null(url.search) ? url.pathname : `${url.pathname}${url.search}`;
23
24 return options;
25};