All files / dist web.js

100% Statements 39/39
70% Branches 14/20
100% Functions 9/9
100% Lines 39/39
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92      1x 1x 1x     1x                   1x 1x 5x       33x 33x 33x   33x         28x 28x 28x 177x 9x   168x     28x       11x   11x 11x   11x 11x 2x   11x   11x 11x 11x 11x 6x   11x     11x 11x 11x                 11x   11x         1x   5x     6x    
'use strict';
 
// Dependencies
var request = require('request-promise');
var fs = require('fs');
var debug = require('debug')('node-cba-netbank');
 
// Initialisation
var DEFAULT_OPTION = {
  jar: true,
  followAllRedirects: true,
  resolveWithFullResponse: true,
  headers: {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
  }
};
 
// Utilities
var myRequest = request.defaults(DEFAULT_OPTION);
var isString = function isString(obj) {
  return typeof obj === 'string' || obj instanceof String;
};
 
function writeToFile(filename, content) {
  fs.writeFile('log/' + filename, content, function (err) {
    Eif (err) {
      debug(err);
    }
    debug('Wrote to file => \'log/' + filename + '\'.');
  });
}
 
function shorten(form) {
  var MAX_LEN = 200;
  var shortenForm = {};
  Object.entries(form).forEach(function (entry) {
    if (entry[1].length > MAX_LEN) {
      shortenForm[entry[0]] = entry[1].substring(0, MAX_LEN) + '... (' + (entry[1].length - MAX_LEN) + ' bytes more)';
    } else {
      shortenForm[entry[0]] = entry[1];
    }
  });
  return shortenForm;
}
 
function req() {
  var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
 
  var sequence = Date.now();
  var partial = params.partial;
 
  var headers = Object.assign({}, params.headers);
  if (partial) {
    headers['X-MicrosoftAjax'] = 'Delta=true';
  }
  var myParams = Object.assign({ method: 'GET' }, params, { headers: headers });
 
  Eif (debug.enabled) {
    debug(myParams.method + ' ' + myParams.url.substring(0, 80) + '...');
    debug('headers => ' + JSON.stringify(shorten(myParams.headers)));
    if (myParams.method === 'POST') {
      debug('form => ' + JSON.stringify(shorten(myParams.form)));
    }
    writeToFile(sequence + '-1-request.json', JSON.stringify(shorten(myParams), null, 2));
  }
 
  return myRequest(myParams).then(function (response) {
    Eif (debug.enabled) {
      writeToFile(sequence + '-2-response.json', JSON.stringify({
        request: response.request,
        status: {
          code: response.statusCode,
          message: response.statusMessage
        },
        headers: response.headers,
        body: response.body.substring(0, 1000) + '...'
      }, null, 2));
      writeToFile(sequence + '-3-response-body.html', response.body);
    }
    return { url: response.request.href, headers: response.headers, body: response.body };
  });
}
 
//  Export
module.exports = {
  get: function get(params) {
    return isString(params) ? req({ url: params }) : req(params);
  },
  post: function post(params) {
    return req(Object.assign({ method: 'POST' }, params));
  }
};