All files client.js

100% Statements 19/19
50% Branches 7/14
100% Functions 10/10
100% Lines 19/19
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 99 100 101 102 103 104 105 106 107 108 109        17x 17x 17x 17x                 10x                   1x                       4x                 2x 1x     1x 1x                   3x 1x     2x 2x                 1x                 2x                   1x                 1x        
import { user, message, event, intent } from './methods';
 
export default class uTu {
  constructor(apikey, config = {}, hasContext = false) {
    this.apikey = apikey;
    this.config = config;
    this.values = {};
    this.hasContext = hasContext;
  }
 
  /**
   * Returns the request object that will be sent
   * @param  {Object} obj an optional object you'd like to merge into the final request object
   * @return {Object}
   */
  getRequestObject(obj = {}) {
    return Object.assign({}, this.config, {
      values: this.values,
    }, obj);
  }
 
  /**
   * Sets the global config so you can short hand some of your requests
   * @param {Object} config the config that will be globally set
   */
  setConfig(config = {}) {
    this.config = config;
  }
 
  /**
   * Returns a new instance of the class so that we can contain context
   * think of context such as platformId. We may want to send multiple requests
   * be we don't want to put the users platformId in each request
   *
   * @param {Object} ctx the context you would like to pass with each request
   * @return {uTu} a new instance of uTu
   */
  withContext(ctx = {}) {
    return new uTu(this.apikey, this.getRequestObject(ctx), true);
  }
 
  /**
   * Sets the values that should be sent with the request
   * @params {Object} values sets the values object within the context
   * @return {uTu}
   */
  setValues(values = {}) {
    if (!this.hasContext) {
      throw new Error('You can only set values when using context, please see withContext()');
    }
 
    this.values = values;
    return this;
  }
 
  /**
   * Sets a single value
   * @param {Object} key the key to set
   * @param {Object} value this can be any value type
   * @return {uTu}
   */
  setValue(key, value) {
    if (!this.hasContext) {
      throw new Error('You can only add values when using context, please see withContext()');
    }
 
    Object.assign(this.values, { [key]: value });
    return this;
  }
 
  /**
   * Creates or updates a new user in the system
   * @param  {Object} data the data that should be sent with the request
   * @return {Promise}
   */
  user(data = {}) {
    return user(this.apikey, this.getRequestObject(data));
  }
 
  /**
   * Logs a message into the system
   * @param  {Object} data the data that should be sent with the request
   * @return {Promise}
   */
  message(data = {}) {
    return message(this.apikey, this.getRequestObject(data));
  }
 
  /**
   * Logs a custom event
   * @param  {String} e    the name of the custom event
   * @param  {Object} data the data that should be sent with the request
   * @return {Promise}
   */
  event(e, data = {}) {
    return event(e, this.apikey, this.getRequestObject(data));
  }
 
  /**
   * Logs an intent into the system
   * @param  {Object} data the data that should be sent with the request
   * @return {Promise}
   */
  intent(i, data = {}) {
    return intent(i, this.apikey, this.getRequestObject(data));
  }
 
}