All files web.js

100% Statements 31/31
63.64% Branches 7/11
100% Functions 7/7
100% Lines 31/31
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  2x 2x 2x 2x 2x     2x                       40x 40x 198x   40x       16x 16x 16x 16x 2x   16x   16x 16x 16x 16x 8x   16x     16x 14x 14x                               14x   14x           9x     8x     8x         2x  
// Dependencies
const debug = require('debug')('node-cba-netbank');
const fs = require('mz/fs');
const isString = require('lodash/isString');
const request = require('request-promise');
const truncate = require('lodash/truncate');
 
// Initialisation
const 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',
    'Cache-Control': 'no-cache',
  },
};
 
// Utilities
function shorten(form) {
  const shortenForm = {};
  Object.keys(form).forEach((key) => {
    shortenForm[key] = truncate(form[key], { length: 200 });
  });
  return shortenForm;
}
 
function doRequest(req, params = {}) {
  const sequence = Date.now();
  const { partial } = params;
  const headers = Object.assign({}, params.headers);
  if (partial) {
    headers['X-MicrosoftAjax'] = 'Delta=true';
  }
  const myParams = Object.assign({ method: 'GET' }, params, { 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))}`);
    }
    fs.writeFile(`log/${sequence}-1-request.json`, JSON.stringify(shorten(myParams), null, 2));
  }
 
  return req(myParams).then((response) => {
    Eif (debug.enabled) {
      fs.writeFile(
        `log/${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,
        ),
      );
      fs.writeFile(`log/${sequence}-3-response-body.html`, response.body);
    }
    return { url: response.request.href, headers: response.headers, body: response.body };
  });
}
 
class WebClient {
  constructor() {
    this.request = request.defaults(DEFAULT_OPTION);
  }
  get(params) {
    return isString(params) ? doRequest(this.request, { url: params }) : doRequest(this.request, params);
  }
  post(params) {
    return doRequest(this.request, Object.assign({ method: 'POST' }, params));
  }
}
 
//  Export
module.exports = WebClient;