All files / src/client index.js

81.58% Statements 31/38
73.91% Branches 17/23
63.64% Functions 7/11
81.08% Lines 30/37
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 93 94 95 96 97 98          2x 2x 2x       1x       1x                               11x 11x 11x 11x 11x       11x 11x 11x   11x                     11x     11x 11x 11x   11x 11x 11x     11x       26x       7x   7x 7x                 7x       7x 7x     7x      
import crypto from 'crypto';
import https from '../libs/https';
 
export default class Client {
  constructor (key, secret, version = 'v1') {
    this.version = version;
    this.key = key;
    this.secret = secret;
  }
 
  get (...args) {
    return this.request(...['get'].concat(...args));
  }
 
  post (...args) {
    return this.request(...['post'].concat(...args));
  }
 
  put (...args) {
    return this.request(...['patch'].concat(...args));
  }
 
  patch (...args) {
    return this.request(...['patch'].concat(...args));
  }
 
  delete (...args) {
    return this.request(...['delete'].concat(...args));
  }
 
  signature (timestamp, method, requestPath, bodyConst) {
    let body = {};
    if (bodyConst) body = bodyConst;
    const hmac = crypto.createHmac('sha256', this.secret);
    hmac.update(`${timestamp}|${method}|/api/${this.version}/${requestPath}` + (method !== 'GET' ? `|${JSON.stringify(body)}` : ''));
    return hmac.digest('hex');
  }
 
  request (...args) {
    const method = String(args[0]).toUpperCase();
    const requestPath = Client.requestHasBody(method) ? args.slice(1, -1).join('/') : Client.appendObjectAsQuery(args[1], args[2]);
    const body = (Client.requestHasBody(method) ? args.slice(-1)[0] : null);
 
    const req = {
      'hostname': 'coinfalcon.com',
      'headers': {
        'Content-Type': 'application/json; charset=utf-8'
      },
      'path': '/api/' + this.version + '/' + requestPath,
      'port': 443,
      'method': method,
      'body': body
    };
 
    Iif (this.version === 'v1' && requestPath.indexOf('markets/') === 0) {
      // public request
    } else {
      let timestamp = Date.now().toString();
      timestamp = parseInt(timestamp.substring(0, timestamp.length - 3), 10);
      const signature = this.signature(timestamp, method, requestPath, body);
 
      req.headers['CF-API-KEY'] = this.key;
      req.headers['CF-API-TIMESTAMP'] = timestamp;
      req.headers['CF-API-SIGNATURE'] = signature;
    }
 
    return https.request(req);
  }
 
  static requestHasBody (method) {
    return method === 'POST' || method === 'PATCH';
  }
 
  static appendObjectAsQuery (base, body) {
    let url = base;
 
    Eif (url.indexOf('?') === -1) {
      url += '?';
    } else if (url.charAt(url.length - 1) !== '&') {
      url += '&';
    }
 
    function addToUrl (entry) {
      url += encodeURIComponent(entry[0]) + '=' + encodeURIComponent(entry[1]) + '&';
    }
 
    Iif (body) {
      Object.entries(body).forEach(addToUrl);
    }
 
    Eif (url.charAt(url.length - 1)) {
      url = url.substr(0, url.length - 1);
    }
 
    return url;
  }
}