lol-api-js
Version:
Integration package LoL Riot API and your typescript/javascript
158 lines (141 loc) • 4.33 kB
text/typescript
import axios from "axios";
import API from ".";
import ChampionMastery, { championMasteryData } from "./championMastery";
import League, { leagueData } from "./league";
import Match, { jsonMatch } from "./match";
class Summoner extends API {
id: string;
accountId: string;
profileIconId: number;
revisionDate: number;
name: string;
puuid: string;
summonerLevel: number;
championMasteryArray: ChampionMastery[];
rankedSolo: League;
rankedFlex: League;
matchIds: string[];
constructor(
api_key: string,
path: string,
jsonObj: summonerData,
leagueArr: leagueData[],
champMastObj: championMasteryData[],
matchIds: string[]
) {
super(api_key, path);
// console.log("called");
this.id = jsonObj.id;
this.accountId = jsonObj.accountId;
this.profileIconId = jsonObj.profileIconId;
this.revisionDate = jsonObj.revisionDate;
this.name = jsonObj.name;
this.puuid = jsonObj.puuid;
this.summonerLevel = jsonObj.summonerLevel;
this.championMasteryArray = champMastObj;
this.rankedSolo = new League(leagueArr[0]);
this.rankedFlex = new League(leagueArr[1]);
this.matchIds = matchIds;
}
/**
* Returns the path to the icon location`
*
* @returns A @var string that represents a path to an `.png`
*
* @remarks
* This method is part of lol-api-js package and Summoner class
*
* @example
* summoner = new Summoner();
* summoner.get(summonerName);
* summoner.getProfileIcon();
*/
getProfileIcon = (): string => {
var iconUrl = `${this.dataPath}/11.19.1/img/profileicon/`;
return iconUrl + this.profileIconId + ".png";
};
/**
* Returns a Match object with match details for a given matchId.
*
* @remarks
* This method is part of lol-api-js package from the Match class
*
* @param matchId - matchId that can be acessed through:
*
* ```
* summoner = new Summoner();
* summoner.get(summonerName);
* arrayOfMatchIds = summoner.getMatchIds();
* ```
*
* @example
* match = new Match();
* match.getMatch(matchId);
*/
getMatch = async (matchId: string): Promise<Match> => {
const url: string = this.buildUrl(matchUrl, matchId, {});
const resp = await axios.get(url);
const data: jsonMatch = resp.data;
const participants = this.getInvolvedSummoners(data.metadata.participants);
let metadata = {
...data.metadata,
participants: await participants,
};
let info = data.info;
return new Match(this.key, this.dataPath, metadata, info);
};
/**
* Returns an Array of Summoners object with summoners details.
*
* @remarks
* This method is part of lol-api-js package from the Match class
*
* @async
* @param participants
* array of strings (Participant interface)
*
* @returns
* Returns an array of Summoners object.
*
* @private
* This is a private method, it cannot be called outside of its class
*
*/
private getInvolvedSummoners = async (
participants: string[]
): Promise<Summoner[]> => {
const summonerUrls: string[] = participants.map((puuid: string) => {
return this.buildUrl(summonerFromPuuid, puuid, {});
});
const summoners = summonerUrls.map(async (url) => {
const resp = await axios.get(url);
const data: summonerData = resp.data;
const leagueData = this.getLeagues(data.id);
const champMastData = this.getChampionMastery(data.id);
const matchIds = this.getMatchIds(data.puuid, {});
return new Summoner(
this.key,
this.dataPath,
data,
await leagueData,
await champMastData,
await matchIds
);
});
return await Promise.all(summoners);
};
}
export default Summoner;
// encryptedAccountId (accountId)
export interface summonerData {
id: string;
accountId: string;
profileIconId: number;
revisionDate: number;
name: string;
puuid: string;
summonerLevel: number;
}
const matchUrl = "https://americas.api.riotgames.com/lol/match/v5/matches/";
const summonerFromPuuid =
"https://br1.api.riotgames.com/lol/summoner/v4/summoners/by-puuid/";