riot-api-nodejs
Version:
A NodeJS Library for Riot Games API
686 lines (685 loc) • 30.1 kB
JavaScript
// NodeJS Library for Riot Games
// Project: https://developer.riotgames.com/
// Library by : Luca Laissue <https://github.com/zafixlrp>
"use strict";
const request = require("request");
exports.BASE_URL = "https://{region}.api.pvp.net/api/lol/{region}/";
// ClassicURLS
exports.URL_1_2 = `${exports.BASE_URL}v1.2/`;
exports.URL_1_3 = `${exports.BASE_URL}v1.3/`;
exports.URL_1_4 = `${exports.BASE_URL}v1.4/`;
exports.URL_2_2 = `${exports.BASE_URL}v2.2/`;
exports.URL_2_4 = `${exports.BASE_URL}v2.4/`;
exports.URL_2_5 = `${exports.BASE_URL}v2.5/`;
// ChampionMasteryURL
const CHAMPIONMASTERY_URL = "https://{region}.api.pvp.net/championmastery/location/{endpoint}/";
// Statics Global URLS
const GLOBAL_URL_1_2 = "https://global.api.pvp.net/api/lol/static-data/{region}/v1.2/";
// Spectator
const URL_SPECTATOR_1_0 = "https://{region}.api.pvp.net/observer-mode/rest/";
// Tournament ULRS
const TOURNAMENT_URL_1 = "https://global.api.pvp.net/tournament/public/v1/";
function region_e_TO_endpointString(param) {
switch (param) {
case region_e.BR:
return "BR1";
case region_e.EUNE:
return "EUNE1";
case region_e.EUW:
return "EUW1";
case region_e.KR:
return "KR";
case region_e.LAN:
return "LA1";
case region_e.LAS:
return "LA2";
case region_e.NA:
return "NA1";
case region_e.OCE:
return "OCE1";
case region_e.TR:
return "TR1";
case region_e.RU:
return "RU";
case region_e.PBE:
return "PBE1";
}
}
function region_e_TO_string(param) {
switch (param) {
case region_e.BR:
return "br";
case region_e.EUNE:
return "eune";
case region_e.EUW:
return "euw";
case region_e.KR:
return "kr";
case region_e.LAN:
return "lan";
case region_e.LAS:
return "las";
case region_e.NA:
return "na";
case region_e.OCE:
return "oce";
case region_e.TR:
return "tr";
case region_e.RU:
return "ru";
case region_e.PBE:
return "pbe";
}
}
// Regions List
(function (region_e) {
region_e[region_e["BR"] = 0] = "BR";
region_e[region_e["EUNE"] = 1] = "EUNE";
region_e[region_e["EUW"] = 2] = "EUW";
region_e[region_e["KR"] = 3] = "KR";
region_e[region_e["LAN"] = 4] = "LAN";
region_e[region_e["LAS"] = 5] = "LAS";
region_e[region_e["NA"] = 6] = "NA";
region_e[region_e["OCE"] = 7] = "OCE";
region_e[region_e["TR"] = 8] = "TR";
region_e[region_e["RU"] = 9] = "RU";
region_e[region_e["PBE"] = 10] = "PBE";
})(exports.region_e || (exports.region_e = {}));
var region_e = exports.region_e;
const ERROR_CODES = {
400: "Bad request",
401: "Unauthorized",
403: "Forbidden",
404: "Not found",
415: "Unsupported media type",
429: "Rate limit exceeded",
500: "Internal server error",
503: "Service unavailable"
};
/**
* Base API
*/
class API {
constructor(apiKeys) {
this.apiKeys = apiKeys;
this.apiKey = apiKeys[0];
}
/**
* Change the Api Key for the next requests
*/
switchApiKey() {
this.apiKey = this.apiKeys[(this.apiKeys.indexOf(this.apiKey) + 1) % this.apiKeys.length];
}
/**
* Send a request to the Riot Games Api and return a formatted json via a callback
* @param {string} url request url
* @param {string} method method(post / put / get)
* @param {[type]} data body parameters
* @param {(JSON} callback callback function with formatted JSON
*/
getJSON(url, method, data) {
this.switchApiKey();
return new Promise((success, fail) => {
request({
url: url,
method: method,
headers: {
"User-Agent": "Nodejs Server Request",
"Accept-Charset": "ISO-8859-1, utf-8",
"X-Riot-Token": this.apiKey
},
json: true,
body: data
}, (err, res, body) => {
if (res.statusCode == 200 || res.statusCode == 204) {
try {
success(JSON.parse(body));
}
catch (E) {
success(body);
}
}
else if (res.statusCode == 429) {
setTimeout(() => { this.getJSON(url, method, data).then(success); }, res["Retry-After"] * 1000);
}
else {
fail({ code: res.statusCode, message: ERROR_CODES[res.statusCode] });
}
});
});
}
request(url, method, data, prop) {
return new Promise((success, fail) => {
this.getJSON(url, method, data).then((data) => {
if (prop === null) {
success();
}
else if (prop === undefined) {
success(data);
}
else {
success(data[prop]);
}
}).catch((err) => {
fail(err);
});
});
}
/**
* get the API Key that is used for the requests
* @return {string} the current API Key
*/
getCurrentApiKey() {
return this.apiKey;
}
/**
* set the API Keys
* @param {string[]} ApiKeys the API Keys
*/
setApikeys(apiKeys) {
this.apiKeys = apiKeys;
}
}
exports.API = API;
/**
* Tournament API
*/
class TournamentAPI extends API {
constructor(...apiKeys) {
super(apiKeys);
}
// **************************** tournament-provider-v1 *************************** //
/**
* create tournament Codes for a given tournament
* @param {number} tournamentId the ID of the tournament
* @param {number} count Number of codes you want
* @param {RiotGamesAPI.TournamentProvider.TournamentCodeParameters} params Tournament Code parameters
* @param {number[]} callback Tournaments Codes [description]
*/
createTournamentCodes(tournamentId, count, params) {
return this.request(TOURNAMENT_URL_1 + "code?tournamentId=" + tournamentId + "&count=" + count, "post", params);
}
/**
* get tournament infos for a given tournament code
* @param {string} tournamentCode Tournament Code
* @param {RiotGamesAPI.TournamentProvider.TournamentCodeDto} callback Tournament Infos
*/
getTournamentByCode(tournamentCode) {
return this.request(TOURNAMENT_URL_1 + "code?tournamentCode=" + tournamentCode, "get", null);
}
/**
* edit the tournament Code parameters for a given tournament Code
* @param {string} tournamentCode Tournament Code to update
* @param {RiotGamesAPI.TournamentProvider.TournamentCodeUpdateParameters} params parameters to edit
* @param {(} callback callback if succes
*/
editTournamentByCode(tournamentCode, params) {
return this.request(TOURNAMENT_URL_1 + "code/" + tournamentCode, "put", params, null);
}
/**
* get the lobby envents for a given tournament Code
* @param {string} tournamentCode the tournament code to get the lobby events
* @param {RiotGamesAPI.TournamentProvider.LobbyEventDto} callback lobby events
*/
getLobbyEventByCode(tournamentCode) {
return this.request(TOURNAMENT_URL_1 + "lobby/events/by-code/" + tournamentCode, "get", null);
}
/**
* Register a new tournament provider
* @param {region_e} region region where you want to register the provider
* @param {string} url url of callback for the POST notifications
* @param {number} callback returns the tounament provider ID
*/
registerProvider(region, url) {
return this.request(TOURNAMENT_URL_1 + "provider", "post", { "region": region_e_TO_string(region), "url": url });
}
/**
* Register a new tournament
* @param {string} name Name of tournament
* @param {number} providerId Provider ID
* @param {number} callback returns the tournament ID
*/
registerTournament(name, providerId) {
return this.request(TOURNAMENT_URL_1 + "tournament", "post", { "name": name, "providerId": providerId });
}
}
exports.TournamentAPI = TournamentAPI;
class ClassicAPI extends API {
/**
* ClassicAPI Constructor
* @param {string[]} ApiKeys API Keys for the requests
* @param {region_e} region region where you want to send requests
*/
constructor(apiKeys, region) {
super(apiKeys);
this.region = region;
}
/**
* Edit the consts for a valid url for the riot games api
* @param {string} unparsedURL the URL to parse
* @return {string} the Parsed URL
*/
parseURL(unparsedURL) {
let parsedURL = unparsedURL.replace(/{region}/g, region_e_TO_string(this.region));
parsedURL = parsedURL.replace(/{endpoint}/g, region_e_TO_endpointString(this.region));
// if there are other params in the url :
return parsedURL + (parsedURL.indexOf("?") > -1 ? "&" : "?") + "api_key=" + this.getCurrentApiKey();
}
/**
* get the region where send send request
* @return {region_e} the current region
*/
getRegion() {
return this.region;
}
/**
* set the region where you want to send requests
* @param {region_e} region the region
*/
setRegion(region) {
this.region = region;
}
// **************************** champion-v1.2 ************************************ //
/**
* get all champions of league of legends
* @param {RiotGamesAPI.Champion.ChampionListDto} callback data callback
*/
getChampions() {
return this.request(this.parseURL(exports.URL_1_2 + "champion"), "get", null, "champions");
}
/**
* get the champion for a given id
* @param {number} id the champion id
* @param {RiotGamesAPI.Champion.ChampionDto} callback data callback
*/
getChampionById(id) {
return this.request(this.parseURL(exports.URL_1_2 + "champion/" + id), "get", null);
}
/**
* get the free to play champions
* @param {RiotGamesAPI.Champion.ChampionListDto} callback data callback
*/
getFreeToPlayChampions() {
return this.request(this.parseURL(exports.URL_1_2 + "champion?freeToPlay=true"), "get", null, "champions");
}
// ******************************************************************************* //
// **************************** championmastery ********************************** //
/**
* get Champion mastery of a player for a given champion ID
* @param {number} summonerId summoner ID
* @param {number} championId Champion ID
* @param {RiotGamesAPI.ChampionMastery.ChampionMasteryDto} callback data callback
*/
getChampionMastery(summonerId, championId) {
return this.request(this.parseURL(CHAMPIONMASTERY_URL + "player/" + summonerId + "/champion/" + championId), "get", null);
}
/**
* get all champion masteries for a given summoner
* @param {number} summonerId Summoner ID
* @param {[RiotGamesAPI.ChampionMastery.ChampionMasteryDto]} callback data callback
*/
getChampionMasteryBySummoner(summonerId) {
return this.request(this.parseURL(CHAMPIONMASTERY_URL + "player/" + summonerId + "/champions"), "get", null);
}
/**
* get the mastery score of a summoner
* @param {number} summonerId Summoner ID
* @param {number} callback Mastery Score
*/
getChampionMasteryScore(summonerId) {
return this.request(this.parseURL(CHAMPIONMASTERY_URL + "player/" + summonerId + "/score"), "get", null);
}
/**
* get The 3 best champion masteries
* @param {[type]} summonerId Summoner ID
* @param {[RiotGamesAPI.ChampionMastery.ChampionMasteryDto]} callback data callback
*/
getTopChampionMastery(summonerId) {
return this.request(this.parseURL(CHAMPIONMASTERY_URL + "player/" + summonerId + "/topchampions"), "get", null);
}
// ******************************************************************************* //
// ***************************** current-game-v1.0 ******************************* //
/**
* get the current game infos for a given summoner ID
* @param {number} summonerId Summoner ID
* @param {RiotGamesAPI.CurrentGame.CurrentGameInfo} callback data callback
*/
getCurrentGame(summonerId) {
return this.request(this.parseURL(URL_SPECTATOR_1_0 + "consumer/getSpectatorGameInfo/{endpoint}/" + summonerId), "get", null);
}
// ******************************************************************************* //
// ***************************** featured-games-v1.0 ***************************** //
/**
* get the featured games
* @param {RiotGamesAPI.FeaturedGames.FeaturedGames} callback data callback
*/
getFeaturedGame() {
return this.request(this.parseURL(URL_SPECTATOR_1_0 + "featured"), "get", null);
}
// ******************************************************************************* //
// ********************************** game-v1.3 ********************************** //
/**
* get the recents games for a given Summoner ID
* @param {number} summonerId Summoner ID
* @param {RiotGamesAPI.Game.RecentGamesDto} callback data callback
*/
getRecentGames(summonerId) {
return this.request(this.parseURL(exports.URL_1_3 + "game/by-summoner/" + summonerId + "/recent"), "get", null);
}
// ******************************************************************************* //
// ********************************** league-v2.5********************************* //
/**
* Get League infos of a summoner
* @param {number} summonerId Summoner ID
* @param {RiotGamesAPI.League.LeagueDto[]} callback data callback
*/
getLeagueBySummonerId(summonerId) {
return this.request(this.parseURL(exports.URL_2_5 + "league/by-summoner/" + summonerId), "get", null, summonerId.toString());
}
/**
* get League infos of a summoner
* @param {number} summonerId Summoner ID
* @param {RiotGamesAPI.League.LeagueDto[]} callback data callback
*/
getLeagueBySummonerIdEntry(summonerId) {
return this.request(this.parseURL(exports.URL_2_5 + "league/by-summoner/" + summonerId + "/entry"), "get", null, summonerId.toString());
}
/**
* get league infos by team
* @param {string} teamId Team ID
* @param {RiotGamesAPI.League.LeagueDto[]} callback data callback
*/
getLeagueByTeamId(teamId) {
return this.request(this.parseURL(exports.URL_2_5 + "league/by-team/" + teamId), "get", null, teamId.toString());
}
/**
* get league infos by team
* @param {string} teamId Team ID
* @param {RiotGamesAPI.League.LeagueDto[]} callback data callback
*/
getLeagueByTeamIdEntry(teamId) {
return this.request(this.parseURL(exports.URL_2_5 + "league/by-team/" + teamId + "/entry"), "get", null, teamId.toString());
}
/**
* get Challengers in SOLO Queue
* @param {RiotGamesAPI.League.LeagueDto} callback data callback
*/
getChallengers_SOLO() {
return this.request(this.parseURL(exports.URL_2_5 + "league/challenger?type=RANKED_SOLO_5x5"), "get", null);
}
/**
* get Challengers Teams in 3x3
* @param {RiotGamesAPI.League.LeagueDto} callback data callback
*/
getChallengers_3x3() {
return this.request(this.parseURL(exports.URL_2_5 + "league/challenger?type=RANKED_TEAM_3x3"), "get", null);
}
/**
* get Challengers Teams in 5x5
* @param {RiotGamesAPI.League.LeagueDto} callback data callback
*/
getChallengers_5x5() {
return this.request(this.parseURL(exports.URL_2_5 + "league/challenger?type=RANKED_TEAM_5x5"), "get", null);
}
/**
* get Masters in Solo Queue
* @param {RiotGamesAPI.League.LeagueDto} callback data callback
*/
getMasters_SOLO() {
return this.request(this.parseURL(exports.URL_2_5 + "league/master?type=RANKED_SOLO_5x5"), "get", null);
}
/**
* get Master Teams in 3x3
* @param {RiotGamesAPI.League.LeagueDto} callback data callback
*/
getMasters_3x3() {
return this.request(this.parseURL(exports.URL_2_5 + "league/master?type=RANKED_TEAM_3x3"), "get", null);
}
/**
* get Master Teams in 5x5
* @param {RiotGamesAPI.League.LeagueDto} callback data callback
*/
getMasters_5x5() {
return this.request(this.parseURL(exports.URL_2_5 + "league/master?type=RANKED_TEAM_5x5"), "get", null);
}
// ******************************************************************************* //
// ***************************** lol-static-data-v1.2 **************************** //
/**
* get Champions (static data)
* @param {RiotGamesAPI.LolStaticData.ChampionListDto} callback data callback
*/
staticDataChampions() {
return this.request(this.parseURL(GLOBAL_URL_1_2 + "champion"), "get", null);
}
/**
* get data by champion ID
* @param {number} championsId Champion ID
* @param {RiotGamesAPI.LolStaticData.ChampionDto} callback data callback
*/
staticDataChampionById(championsId) {
return this.request(this.parseURL(GLOBAL_URL_1_2 + "champion/" + championsId), "get", null);
}
/**
* get League of Legends Items
* @param {RiotGamesAPI.LolStaticData.ItemListDto} callback data callback
*/
staticDataItems() {
return this.request(this.parseURL(GLOBAL_URL_1_2 + "item"), "get", null);
}
/**
* Get item infos by ID
* @param {number} itemId item ID
* @param {RiotGamesAPI.LolStaticData.ItemDto} callback data callback
*/
staticDataItemById(itemId) {
return this.request(this.parseURL(GLOBAL_URL_1_2 + "item/" + itemId), "get", null);
}
/**
* get league of legends languages
* @param {RiotGamesAPI.LolStaticData.LanguageStringsDto} callback data callback
*/
staticDataLanguagesStrings() {
return this.request(this.parseURL(GLOBAL_URL_1_2 + "language-strings"), "get", null);
}
/**
* get league of legends languages
* @param {string[]} callback data callback
*/
staticDataLanguages() {
return this.request(this.parseURL(GLOBAL_URL_1_2 + "languages"), "get", null);
}
/**
* get Map data
* @param {RiotGamesAPI.LolStaticData.MapDataDto} callback data callback
*/
staticDataMap() {
return this.request(this.parseURL(GLOBAL_URL_1_2 + "map"), "get", null);
}
/**
* get all masteries
* @param {RiotGamesAPI.LolStaticData.MasteryListDto} callback data callback
*/
staticDataMastery() {
return this.request(this.parseURL(GLOBAL_URL_1_2 + "mastery"), "get", null);
}
/**
* get data by mastery ID
* @param {number} masteryId Mastery ID
* @param {RiotGamesAPI.LolStaticData.MasteryDto} callback data callback
*/
staticDataMasteryById(masteryId) {
return this.request(this.parseURL(GLOBAL_URL_1_2 + "mastery/" + masteryId), "get", null);
}
staticDataRealm() {
return this.request(this.parseURL(GLOBAL_URL_1_2 + "realm"), "get", null);
}
/**
* get all runes
* @param {RiotGamesAPI.LolStaticData.RuneListDto} callback data callback
*/
staticDataRunes() {
return this.request(this.parseURL(GLOBAL_URL_1_2 + "rune"), "get", null);
}
/**
* get rune by Rune ID
* @param {number} runeId Rune ID
* @param {RiotGamesAPI.LolStaticData.RuneDto} callback data callback
*/
staticDataRuneById(runeId) {
return this.request(this.parseURL(GLOBAL_URL_1_2 + "rune/" + runeId), "get", null);
}
/**
* get all summoner spells
* @param {RiotGamesAPI.LolStaticData.SummonerSpellListDto} callback data callback
*/
staticDataSummonerSpells() {
return this.request(this.parseURL(GLOBAL_URL_1_2 + "summoner-spell"), "get", null);
}
/**
* get summoner spell by summoner spell ID
* @param {number} summonerSpellId Summoner spell ID
* @param {RiotGamesAPI.LolStaticData.SummonerSpellDto} callback data callback
*/
staticDataSummonSpellById(summonerSpellId) {
return this.request(this.parseURL(GLOBAL_URL_1_2 + "summoner-spell/" + summonerSpellId), "get", null);
}
/**
* get league of legends versions
* @param {string[]} callback data callback
*/
staticDataVersion() {
return this.request(this.parseURL(GLOBAL_URL_1_2 + "versions"), "get", null);
}
// ******************************************************************************* //
// ******************************** lol-status-v1.0 ****************************** //
/**
* get league of legends status
* @param {RiotGamesAPI.LolStatus.Shard[]} callback data callback
*/
getStatus() {
return this.request("http://status.leagueoflegends.com/shards", "get", null);
}
/**
* get status for a given region
* @param {region_e} region region
* @param {RiotGamesAPI.LolStatus.Shard} callback data callback
*/
getStatusByRegion(region) {
return this.request("http://status.leagueoflegends.com/shards/" + region_e_TO_string(region), "get", null);
}
// ******************************************************************************* //
// ********************************* match-v2.2 ********************************** //
/**
* get match infos for a given match ID
* @param {number} matchId Match ID
* @param {RiotGamesAPI.Match.MatchDetail} callback data callback
*/
getMatch(matchId) {
return this.request(this.parseURL(exports.URL_2_2 + "match/" + matchId), "get", null);
}
/**
* get all matches for a given tournament code
* @param {string} tournamentCode Tournament Code
* @param {number[]} callback data callback
*/
getMatchIdsByTournamentCode(tournamentCode) {
return this.request(this.parseURL(exports.URL_2_2 + "match/by-tournament/" + tournamentCode + "/ids"), "get", null);
}
/**
* get match by ID in a tournament
* @param {number} matchId Match ID
* @param {RiotGamesAPI.Match.MatchDetail} callback data callback
*/
getMatchForTournament(matchId) {
return this.request(this.parseURL(exports.URL_2_2 + "match/for-tournament/" + matchId), "get", null);
}
// ******************************************************************************* //
// ******************************** matchlist-v2.2 ******************************* //
/**
* get match list of a summoner
* @param {number} summonerId Summoner ID
* @param {RiotGamesAPI.MatchList.MatchList} callback data callback
*/
getMatchList(summonerId) {
return this.request(this.parseURL(exports.URL_2_2 + "matchlist/by-summoner/" + summonerId), "get", null);
}
// ******************************************************************************* //
// ********************************** stats-v1.3 ********************************* //
/**
* get ranked stats of summoner
* @param {number} summonerId Summoner ID
* @param {RiotGamesAPI.Stats.RankedStatsDto} callback data callback
*/
getStatsRanked(summonerId) {
return this.request(this.parseURL(exports.URL_1_3 + "stats/by-summoner/" + summonerId + "/ranked"), "get", null);
}
/**
* get summary ranked stats of summoner
* @param {number} summonerId Summoner ID
* @param {RiotGamesAPI.Stats.PlayerStatsSummaryListDto} callback data callback
*/
getStatsSummary(summonerId) {
return this.request(this.parseURL(exports.URL_1_3 + "stats/by-summoner/" + summonerId + "/summary"), "get", null);
}
// ******************************************************************************* //
// ******************************** summoner-v1.4 ******************************** //
/**
* get summoner infos by Summoner Name
* @param {string} summonerName Summoner Name
* @param {RiotGamesAPI.Summoner.SummonerDto} callback data callback
*/
getSummonerByName(summonerName) {
return this.request(this.parseURL(exports.URL_1_4 + "summoner/by-name/" + summonerName), "get", null, summonerName);
}
/**
* get summoner infos by summoner ID
* @param {number} summonerId Summoner ID
* @param {RiotGamesAPI.Summoner.SummonerDto} callback data callback
*/
getSummonerById(summonerId) {
return this.request(this.parseURL(exports.URL_1_4 + "summoner/" + summonerId), "get", null, summonerId.toString());
}
/**
* get masteries of a summoner
* @param {number} summonerId Summoner ID
* @param {RiotGamesAPI.Summoner.MasteryPagesDto} callback data callback
*/
getSummonerMasteries(summonerId) {
return this.request(this.parseURL(exports.URL_1_4 + "summoner/" + summonerId + "/masteries"), "get", null, summonerId.toString());
}
/**
* get the Summoner Name of a summoner ID
* @param {number} summonerId Summoner ID
* @param {string} callback data callback
*/
getSummonerName(summonerId) {
return this.request(this.parseURL(exports.URL_1_4 + "summoner/" + summonerId + "/name"), "get", null, summonerId.toString());
}
/**
* get the runes of a summoner
* @param {number} summonerId Summoner ID
* @param {RiotGamesAPI.Summoner.RunePagesDto} callback data callback
*/
getSummonerRunes(summonerId) {
return this.request(this.parseURL(exports.URL_1_4 + "summoner/" + summonerId + "/runes"), "get", null, summonerId.toString());
}
// ******************************************************************************* //
// ********************************* team-v2.4 *********************************** //
/**
* get teams of a summoner
* @param {number} summonerId Summoner ID
* @param {RiotGamesAPI.Team.TeamDto[]} callback data callback
*/
getTeamsBySummoner(summonerId) {
return this.request(this.parseURL(exports.URL_2_4 + "team/by-summoner/" + summonerId), "get", null, summonerId.toString());
}
/**
* get Team infos by Team ID
* @param {string} teamId Team ID
* @param {RiotGamesAPI.Team.TeamDto} callback data callback
*/
getTeamById(teamId) {
return this.request(this.parseURL(exports.URL_2_4 + "team/" + teamId), "get", null, teamId.toString());
}
}
exports.ClassicAPI = ClassicAPI;