mirror of
https://github.com/vkaelin/LeagueStats.git
synced 2026-03-25 12:57:28 +00:00
Make the Jax wrapper a little easier to use
This commit is contained in:
parent
1c1dc49c69
commit
932a8e568e
2 changed files with 26 additions and 30 deletions
|
|
@ -12,29 +12,29 @@ import DDragonChampionEndpoint from './Endpoints/DDragonEndpoints/DDragonChampio
|
|||
|
||||
class Jax {
|
||||
constructor(key = process.env.API_KEY, region = 'euw1') {
|
||||
return (async () => {
|
||||
this.key = key
|
||||
const limiterOptions = {
|
||||
strategy: STRATEGY.BURST
|
||||
}
|
||||
this.limiter = new RiotRateLimiter(limiterOptions)
|
||||
this.region = region
|
||||
|
||||
this.League = new LeagueEndpoint(this.limiter, this.region)
|
||||
this.Match = new MatchEndpoint(this.limiter, this.region)
|
||||
this.Matchlist = new MatchlistEndpoint(this.limiter, this.region)
|
||||
this.Summoner = new SummonerEndpoint(this.limiter, this.region)
|
||||
this.key = key
|
||||
const limiterOptions = {
|
||||
strategy: STRATEGY.BURST
|
||||
}
|
||||
this.limiter = new RiotRateLimiter(limiterOptions)
|
||||
this.region = region
|
||||
|
||||
this.version = (await new DDragonVersionEndpoint().list())[0]
|
||||
this.League = new LeagueEndpoint(this.limiter, this.region)
|
||||
this.Match = new MatchEndpoint(this.limiter, this.region)
|
||||
this.Matchlist = new MatchlistEndpoint(this.limiter, this.region)
|
||||
this.Summoner = new SummonerEndpoint(this.limiter, this.region)
|
||||
|
||||
this.DDragon = {
|
||||
Champion: new DDragonChampionEndpoint(this.version),
|
||||
Version: this.version
|
||||
}
|
||||
this.initDDragon()
|
||||
}
|
||||
|
||||
return this
|
||||
})()
|
||||
async initDDragon() {
|
||||
this.version = (await new DDragonVersionEndpoint().list())[0]
|
||||
|
||||
this.DDragon = {
|
||||
Champion: new DDragonChampionEndpoint(this.version),
|
||||
Version: this.version
|
||||
}
|
||||
}
|
||||
|
||||
set regionName(regionName) {
|
||||
|
|
@ -43,5 +43,5 @@ class Jax {
|
|||
}
|
||||
|
||||
module.exports = {
|
||||
Jax
|
||||
Jax: new Jax()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,13 +27,9 @@ app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
|
|||
// Create a middleware that adds a X-Response-Time header to responses
|
||||
app.use(responseTime())
|
||||
|
||||
// Setup Jax
|
||||
let jax
|
||||
|
||||
/* Launch app */
|
||||
app.listen(app.get('port'), async () => {
|
||||
app.listen(app.get('port'), () => {
|
||||
console.log(`RiotAPI app listening on port ${app.get('port')}!`)
|
||||
jax = await new Jax()
|
||||
})
|
||||
|
||||
// Send data of a summoner
|
||||
|
|
@ -49,24 +45,24 @@ app.post('/api', async function (req, res) {
|
|||
}
|
||||
|
||||
const finalJSON = {}
|
||||
jax.regionName = req.body.region
|
||||
Jax.regionName = req.body.region
|
||||
|
||||
try {
|
||||
const account = await jax.Summoner.summonerName(req.body.summoner)
|
||||
const account = await Jax.Summoner.summonerName(req.body.summoner)
|
||||
|
||||
// Check if the summoner is found
|
||||
if (!account) return res.send(null)
|
||||
|
||||
finalJSON.account = account
|
||||
|
||||
const ranked = await jax.League.summonerID(account.id)
|
||||
const ranked = await Jax.League.summonerID(account.id)
|
||||
const soloQ = ranked.filter(e => e.queueType === 'RANKED_SOLO_5x5')
|
||||
finalJSON.soloQ = soloQ.length ? soloQ[0] : null;
|
||||
|
||||
console.time('getMatches')
|
||||
const { matches } = await jax.Matchlist.accountID(account.accountId)
|
||||
const { matches } = await Jax.Matchlist.accountID(account.accountId)
|
||||
const gameIds = matches.slice(0, 10).map(({ gameId }) => gameId)
|
||||
const requests = gameIds.map(jax.Match.get)
|
||||
const requests = gameIds.map(Jax.Match.get)
|
||||
const results = await Promise.all(requests)
|
||||
finalJSON.matchesDetails = results
|
||||
finalJSON.allMatches = matches
|
||||
|
|
@ -84,6 +80,6 @@ app.post('/api', async function (req, res) {
|
|||
app.post('/ddragon', async function (req, res) {
|
||||
console.log('DDragon Request')
|
||||
const endpoint = req.body.endpoint
|
||||
const result = await jax.DDragon[endpoint].list()
|
||||
const result = await Jax.DDragon[endpoint].list()
|
||||
res.send(result)
|
||||
})
|
||||
|
|
|
|||
Loading…
Reference in a new issue