url-to-options.js 588 B

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