# all files / kayn/dist/lib/Utils/ URLHelper.js

25.76% Statements 17/66
12.5% Branches 4/32
9.09% Functions 1/11
27.12% Lines 16/59

    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
    110
    111
    112
    113
    114
    115
    116

    1×

    1×

    1×

    1×

    1×

    1×
    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

     'use strict';

    Object.defineProperty(exports, "__esModule", {
      value: true
    });

    var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

    var _DateTimeHelper = require('./DateTimeHelper');

    var _DateTimeHelper2 = _interopRequireDefault(_DateTimeHelper);

    function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

    var API_HOST = 'api.riotgames.com';
    var API_VERSION = 'v3';

    var createRequestURL = function createRequestURL(platformID, serviceName, endpoint) {
      return 'https://' + platformID + '.' + API_HOST + '/lol/' + serviceName + '/' + API_VERSION + '/' + endpoint;
    };

    var getURLWithKey = function getURLWithKey(platformID, serviceName, endpoint, key) {
      var requestURL = createRequestURL(platformID, serviceName, endpoint);
      return requestURL + getAPIKey(requestURL, key);
    };

    var getAPIKey = function getAPIKey(url, key) {
      return (url.lastIndexOf('?') === -1 ? '?' : '&') + 'api_key=' + (key ? key : '');
    };

    var appendKey = function appendKey(str, key, value) {
      return str + (str ? '&' : '') + (key + '=' + value);
    };

    var arrayToOptions = function arrayToOptions(array) {
      return array.reduce(function (final, curr) {
        return _extends({}, final, curr);
      }, {});
    };

    var stringifyOptions = function stringifyOptions(options, endUrl) {
      var stringifiedOpts = '';

      var _iteratorNormalCompletion = true;
      var _didIteratorError = false;
      var _iteratorError = undefined;

      try {
        for (var _iterator = Object.keys(options)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
          var key = _step.value;

          if (Array.isArray(options[key])) {
            var _iteratorNormalCompletion2 = true;
            var _didIteratorError2 = false;
            var _iteratorError2 = undefined;

            try {
              for (var _iterator2 = options[key][Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
                var value = _step2.value;

                stringifiedOpts = appendKey(stringifiedOpts, key, value);
              }
            } catch (err) {
              _didIteratorError2 = true;
              _iteratorError2 = err;
            } finally {
              try {
                if (!_iteratorNormalCompletion2 && _iterator2.return) {
                  _iterator2.return();
                }
              } finally {
                if (_didIteratorError2) {
                  throw _iteratorError2;
                }
              }
            }
          } else {
            if (_DateTimeHelper2.default.checkIfDateParam(key)) {
              options[key] = _DateTimeHelper2.default.getEpoch(options[key]);
            }
            stringifiedOpts = appendKey(stringifiedOpts, key, options[key]);
          }
        }
      } catch (err) {
        _didIteratorError = true;
        _iteratorError = err;
      } finally {
        try {
          if (!_iteratorNormalCompletion && _iterator.return) {
            _iterator.return();
          }
        } finally {
          if (_didIteratorError) {
            throw _iteratorError;
          }
        }
      }

      return stringifiedOpts;
    };

    var getQueryParamString = function getQueryParamString(arrayOfOpts) {
      var str = stringifyOptions(arrayToOptions(arrayOfOpts));
      return str ? '?' + encodeURI(str) : '';
    };

    var getURLWithQuery = function getURLWithQuery(region, serviceName, endpoint, queryArray) {
      return createRequestURL(region, serviceName, endpoint) + getQueryParamString(queryArray);
    };

    exports.default = {
      createRequestURL: createRequestURL,
      getURLWithKey: getURLWithKey,
      getURLWithQuery: getURLWithQuery,
      getAPIKey: getAPIKey
    };