Issues (33)

lib/index.js (5 issues)

1 View Code Duplication
"use strict";
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
2
Object.defineProperty(exports, "__esModule", { value: true });
3
var utils_1 = require("./utils");
4
var mastery_1 = require("./mastery");
5
var regions_1 = require("./regions");
6
var summoner_1 = require("./summoner");
7
var cache_1 = require("./cache");
8
var realm_1 = require("./realm");
9
var icon_1 = require("./icon");
10
var LoLNode = /** @class */ (function () {
11
    /**
12
     * Represents the Riot API Wrapper Object.
13
     * @constructor
14
     * @param {string} APIKEY - Riot API Key
15
     */
16
    function LoLNode(APIKEY) {
17
        this.cacheList = {};
18
        this.APIKEY = APIKEY;
19
        for (var region in regions_1.RegionDomains) {
0 ignored issues
show
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
20
            if (this.cacheList[region] !== {}) {
21
                this.cacheList[region] = {};
22
            }
23
        }
24
    }
25
    /**
26
     * @function
27
     * Creates a Realm Object of said region
28
     * @param {string} region - Region being called
29
     * @callback Realm - Returns a Realm Object
30
     */
31
    LoLNode.prototype.realm = function (region, callback) {
32
        var _this = this;
33
        region = region.toUpperCase();
34
        var realmCache = this.cacheList[region].realm;
35
        var cacheState;
36
        if (typeof realmCache === 'undefined') {
37
            cacheState = 0;
38
        }
39
        else if (realmCache.get() === null) {
40
            cacheState = 1;
41
        }
42
        else {
43
            cacheState = 2;
44
        }
45
        if (cacheState < 2) {
46
            var regionURL = utils_1.getRegion(region);
47
            var url = regionURL + "/lol/static-data/v3/realms?api_key=" + this.APIKEY;
48
            utils_1.getJSON(url, function (res, err) {
49
                if (err) {
50
                    callback(null, err);
51
                }
52
                else {
53
                    var data = res.data;
54
                    var realm = new realm_1.Realm(data.lg, data.dd, data.l, data.n, data.profileiconmax, data.v, data.cdn, data.css);
55
                    cacheState === 1 ?
56
                        _this.cacheList[region].realm.refresh(realm) :
57
                        _this.cacheList[region].realm = new cache_1.Cache(realm, 1800);
58
                    callback(realm);
59
                }
60
            });
61
        }
62
        else {
63
            callback(realmCache.get());
64
        }
65
    };
66
    /**
67
     * Creates a Summoner Object of said region
68
     * @param {string} region - Region being called
69
     * @param {array} identifier Identification for
70
     * how you enter your data.
71
     * _________________________
72
     * structure => [type, val]
73
     *
74
     * - type => account|name|id
75
     * - - account => The Account ID
76
     * - - name => The Summoner Name
77
     * - - id => The Summoner ID
78
     *
79
     * - val => Either a string containing the name or
80
     * a number containing the Account ID or Summoner ID
81
     *
82
     * @param callback - Returns a Summoner Object
83
     * @callback Summoner - Returns a Summoner Object
84
     */
85
    LoLNode.prototype.summoner = function (region, identifier, callback) {
86
        var _this = this;
87
        region = region.toUpperCase();
88
        var regionURL = utils_1.getRegion(region);
89
        var url = regionURL + "/lol/summoner/v3/summoners";
90
        switch (identifier[0]) {
0 ignored issues
show
As per coding-style, switch statements should have a default case.
Loading history...
91
            case 'account':
92
                url += "/by-account/" + identifier[1] + "?api_key=" + this.APIKEY;
93
                break;
94
            case 'name':
95
                url += "/by-name/" + identifier[1] + "?api_key=" + this.APIKEY;
96
                break;
97
            case 'id':
98
                url += "/" + identifier[1] + "?api_key=" + this.APIKEY;
99
                break;
100
        }
101
        utils_1.getJSON(url, function (res, err) {
102
            if (err) {
103
                callback(null, err);
104
            }
105
            else {
106
                var data_1 = res.data;
107
                _this.realm(region, function (_a) {
108
                    var dd = _a.dd, cdn = _a.cdn;
109
                    callback(new summoner_1.Summoner(data_1.id, data_1.name, new icon_1.Icon(data_1.profileIconId, dd, cdn), data_1.summonerLevel, data_1.accountId, data_1.revisionDate, new mastery_1.SummonerMastery(_this.APIKEY, region, data_1.id)));
110
                });
111
            }
112
        });
113
    };
114
    /**
115
     *
116
     * @param region
117
     * @param identifier
118
     * @param type
119
     * @param callback
120
     */
121
    LoLNode.prototype.mastery = function (region, identifier, type, callback) {
122
        this.summoner(region, identifier, function (summoner) {
123
            switch (type[0]) {
124
                case 'level':
125
                    summoner.mastery.level(function (level) { return callback(level); });
126
                    break;
127
                case 'points':
128
                    summoner.mastery.level(function (points) { return callback(points); });
129
                    break;
130
                case 'top':
131
                    var size = void 0;
0 ignored issues
show
Consider using undefined instead of void(0). It is equivalent and more straightforward to read.
Loading history...
The assignment to variable size seems to be never used. Consider removing it.
Loading history...
132
                    type.length > 1 ? size = type[1] : size = 0;
133
                    summoner.mastery.top(size, function (data) { return callback(data); });
134
                    break;
135
                case 'champion':
136
                    summoner.mastery.champion(type[1], function (data) { return callback(data); });
137
                    break;
138
                default:
139
                    callback(null, 'incorrect type to mastery method in the LoLNode object');
140
            }
141
        });
142
    };
143
    /**
144
     *
145
     * @param region
146
     * @param value
147
     * - test
148
     *      -- test2
149
     * @param callback
150
     */
151
    LoLNode.prototype.champion = function (region, value, options, callback) {
152
        callback();
153
    };
154
    return LoLNode;
155
}());
156
exports.LoLNode = LoLNode;
157
//# sourceMappingURL=index.js.map