'use strict';

const http = require('http');
const https = require('https');

const utils = require('./utils.js');

module.exports = class Client {
  constructor(hostname, port, base, ssl) {
    base = base || '';
    ssl = ssl || false;
    this.hostname = hostname;
    this.port = port;
    this.base = base;
    this.protocol = ssl ? https : http;
    this.cookies = {};
  }

  request(opts, data) {
    return new Promise((resolve, reject) => {
      data = data || null;
      opts.hostname = this.hostname;
      if (this.port > 0) { opts.port = this.port; }
      opts.path = this.base + opts.path;
      opts.headers = opts.headers || {};
      this.writeCookies(opts.headers);
      const req = this.protocol.request(opts, (resp) => {
        this.readCookies(resp);
        resolve(resp);
      });
      req.on('error', (err) => reject(err))
      if (data) { req.write(data); }
      req.end();
    })
  }

  readCookies(resp) {
    utils.parseSetCookies(resp.headers['set-cookie']).forEach(c => {
      this.cookies[c.Name] = c;
    })
  }

  writeCookies(hdrs) {
    var cookies = [];
    for (var k in this.cookies) {
      cookies.push(this.cookies[k]);
    }
    if (cookies.length) {
      hdrs.Cookies = utils.dumpCookies(cookies);
    }
  }
};

'use strict';

const http = require('http');
const https = require('https');

const utils = require('./utils.js');

module.exports = class Client {
  constructor(hostname, port, base, ssl) {
    base = base || '';
    ssl = ssl || false;
    this.hostname = hostname;
    this.port = port;
    this.base = base;
    this.protocol = ssl ? https : http;
    this.cookies = {};
  }

  request(opts, data) {
    return new Promise((resolve, reject) => {
      data = data || null;
      opts.hostname = this.hostname;
      if (this.port > 0) { opts.port = this.port; }
      opts.path = this.base + opts.path;
      opts.headers = opts.headers || {};
      this.writeCookies(opts.headers);
      const req = this.protocol.request(opts, resp => {
        this.readCookies(resp);
        resolve(resp);
      });
      req.on('error', err => reject(err))
      if (data) { req.write(data); }
      req.end();
    })
  }

  readCookies(resp) {
    utils.parseSetCookies(resp.headers['set-cookie']).forEach(c => {
      this.cookies[c.Name] = c;
    })
  }

  writeCookies(hdrs) {
    var cookies = [];
    for (var k in this.cookies) {
      cookies.push(this.cookies[k]);
    }
    if (cookies.length) {
      hdrs.Cookies = utils.dumpCookies(cookies);
    }
  }
};

Use single param form