All files / src web.js

94.44% Statements 34/36
52.94% Branches 9/17
100% Functions 9/9
94.29% Lines 33/35
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  1x 1x 1x     1x                   1x 3x     11x 9x     9x         12x 12x 12x 21x       12x       5x 5x 5x 5x     5x   5x 5x 5x 5x 2x   5x     5x 3x 3x                               3x   3x         1x 3x 2x    
// Dependencies
const request = require('request-promise');
const fs = require('fs');
const debug = require('debug')('node-cba-netbank');
 
// 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',
  },
};
 
// Utilities
const myRequest = request.defaults(DEFAULT_OPTION);
const isString = obj => typeof obj === 'string' || obj instanceof String;
 
function writeToFile(filename, content) {
  fs.writeFile(`log/${filename}`, content, (err) => {
    Iif (err) {
      debug(err);
    }
    debug(`Wrote to file => 'log/${filename}'.`);
  });
}
 
function shorten(form) {
  const MAX_LEN = 200;
  const shortenForm = {};
  Object.keys(form).forEach((key) => {
    shortenForm[key] = form[key].length > MAX_LEN
      ? `${form[key].substring(0, MAX_LEN)}... (${form[key].length - MAX_LEN} bytes more)`
      : form[key];
  });
  return shortenForm;
}
 
function req(params = {}) {
  const sequence = Date.now();
  const { partial } = params;
  const headers = Object.assign({}, params.headers);
  Iif (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))}`);
    }
    writeToFile(`${sequence}-1-request.json`, JSON.stringify(shorten(myParams), null, 2));
  }
 
  return myRequest(myParams).then((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: params => (isString(params) ? req({ url: params }) : req(params)),
  post: params => req(Object.assign({ method: 'POST' }, params)),
};