lol-api-js
Version:
Integration package LoL Riot API and your typescript/javascript
178 lines (155 loc) • 5.13 kB
text/typescript
import Summoner, { summonerData } from "./summoner";
import League, { leagueData } from "./league";
import ChampionMastery, { championMasteryData } from "./championMastery";
import axios from "axios";
export default class API {
key: string;
dataPath: string;
region: string = "br1";
constructor(api_key: string, path: string) {
this.key = api_key;
this.dataPath = path;
}
/**
* Returns the URL with indentifiers, keys and search params`
*
* @returns A @var string that represents a URL with keys and params
*
* @param url - string with the href
* @param identifier - string to join after the url with @var encodeURI
* @param args - JSON Object with search parameters
*
* @remarks
* This method is part of lol-api-js package as a utils function
*
* @example
* const rawURL = "https://www.google.com/";
* const identifier = "search";
* const args = {q: "I love artifical intelligence"};
* const url = buildUrl(rawURL, identifier, args)
* // url = "https://www.google.com/search?q=I+love+artificial+intelligence"
*/
buildUrl = (url: string, identifier: string, args: Object): string => {
var params = new URLSearchParams();
params.set("api_key", this.key);
Object.entries(args).forEach(([key, value]) => {
params.set(key, value);
});
var queryUrl = new URL(url + encodeURI(identifier));
queryUrl.search = params.toString();
return queryUrl.toString();
};
/**
* Method to retrieve a summoner object from RiotGames API
*
* @returns A @var string that represents a URL with keys and params
*
* @param summonerName - a string containing the summoner name
*
* @remarks
* This method is part of lol-api-js package as a API class method
*
* @example
* const api = new API('your Riot Api Key', 'your local datadragon path');
* const summoner = api.getSummoner('summonerName');
*/
getSummoner = async (summonerName: string): Promise<Summoner> => {
const url: string = this.buildUrl(summonerUrl, summonerName, {});
var resp = await axios.get(url);
var data: summonerData = resp.data;
const leagueData = this.getLeagues(data.id);
const champMastData = this.getChampionMastery(data.id);
const matchIds = this.getMatchIds(data.puuid, {});
// const summoner =
return new Summoner(
this.key,
this.dataPath,
data,
await leagueData,
await champMastData,
await matchIds
);
};
/**
* Sets the class properties @var championMastery as an `Array<ChampionMastery>`
*
* @remarks
* This method is part of lol-api-js package and Summoner class
*
*/
getChampionMastery = async (id: string): Promise<championMasteryData[]> => {
const url: string = this.buildUrl(championMasteryUrl, id, {});
try {
var resp = await axios.get(url);
var data = resp.data;
} catch (err) {
console.error(err);
}
let championMasteryArray = data.map((object: championMasteryData) => {
return new ChampionMastery(object);
});
return championMasteryArray;
};
/**
* Sets the summoner class properties @var rankedFlex and @var rankedSolo
*
* @remarks
* This method is part of lol-api-js package and API class
*
*/
getLeagues = async (id: string): Promise<leagueData[]> => {
const url: string = this.buildUrl(leagueEntriesUrl, id, {});
var resp = await axios.get(url);
var data: leagueData[] = resp.data;
var flexArr = data[0];
var soloArr = data[1];
data.forEach((object: leagueData) => {
if (object.queueType == "RANKED_FLEX_SR") {
flexArr = object;
} else {
soloArr = object;
}
});
return [soloArr, flexArr];
};
/**
* Returns an Array of matchIds.
*
* @remarks
* This method is part of lol-api-js package and API class
*
* @param params - optional pre-defined Object: `params = {start: 0, count: 20};`
*
* @returns An array of strings: `Array<string>;`
*
*/
getMatchIds = async (
puuid: string,
params: Object = {}
): Promise<string[]> => {
let searchParams = {
start: 0,
count: 20,
...params,
};
// exclusive
const preUrl = matchesUrl + encodeURI(puuid) + "/ids";
const url: string = this.buildUrl(preUrl, "", searchParams);
try {
var resp = await axios.get(url);
var data = resp.data;
return data;
} catch (err) {
console.error(err);
return [`error: ${err}`];
}
};
}
const summonerUrl =
"https://br1.api.riotgames.com/lol/summoner/v4/summoners/by-name/";
const championMasteryUrl =
"https://br1.api.riotgames.com/lol/champion-mastery/v4/champion-masteries/by-summoner/";
const leagueEntriesUrl =
"https://br1.api.riotgames.com/lol/league/v4/entries/by-summoner/";
const matchesUrl =
"https://americas.api.riotgames.com/lol/match/v5/matches/by-puuid/";