Make the Jax wrapper a little easier to use

This commit is contained in:
Valentin Kaelin 2019-09-04 20:11:16 +02:00
parent 1c1dc49c69
commit 932a8e568e
2 changed files with 26 additions and 30 deletions

View file

@ -12,29 +12,29 @@ import DDragonChampionEndpoint from './Endpoints/DDragonEndpoints/DDragonChampio
class Jax { class Jax {
constructor(key = process.env.API_KEY, region = 'euw1') { 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.key = key
this.Match = new MatchEndpoint(this.limiter, this.region) const limiterOptions = {
this.Matchlist = new MatchlistEndpoint(this.limiter, this.region) strategy: STRATEGY.BURST
this.Summoner = new SummonerEndpoint(this.limiter, this.region) }
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 = { this.initDDragon()
Champion: new DDragonChampionEndpoint(this.version), }
Version: this.version
}
return this async initDDragon() {
})() this.version = (await new DDragonVersionEndpoint().list())[0]
this.DDragon = {
Champion: new DDragonChampionEndpoint(this.version),
Version: this.version
}
} }
set regionName(regionName) { set regionName(regionName) {
@ -43,5 +43,5 @@ class Jax {
} }
module.exports = { module.exports = {
Jax Jax: new Jax()
} }

View file

@ -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 // Create a middleware that adds a X-Response-Time header to responses
app.use(responseTime()) app.use(responseTime())
// Setup Jax
let jax
/* Launch app */ /* Launch app */
app.listen(app.get('port'), async () => { app.listen(app.get('port'), () => {
console.log(`RiotAPI app listening on port ${app.get('port')}!`) console.log(`RiotAPI app listening on port ${app.get('port')}!`)
jax = await new Jax()
}) })
// Send data of a summoner // Send data of a summoner
@ -49,24 +45,24 @@ app.post('/api', async function (req, res) {
} }
const finalJSON = {} const finalJSON = {}
jax.regionName = req.body.region Jax.regionName = req.body.region
try { 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 // Check if the summoner is found
if (!account) return res.send(null) if (!account) return res.send(null)
finalJSON.account = account 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') const soloQ = ranked.filter(e => e.queueType === 'RANKED_SOLO_5x5')
finalJSON.soloQ = soloQ.length ? soloQ[0] : null; finalJSON.soloQ = soloQ.length ? soloQ[0] : null;
console.time('getMatches') 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 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) const results = await Promise.all(requests)
finalJSON.matchesDetails = results finalJSON.matchesDetails = results
finalJSON.allMatches = matches finalJSON.allMatches = matches
@ -84,6 +80,6 @@ app.post('/api', async function (req, res) {
app.post('/ddragon', async function (req, res) { app.post('/ddragon', async function (req, res) {
console.log('DDragon Request') console.log('DDragon Request')
const endpoint = req.body.endpoint const endpoint = req.body.endpoint
const result = await jax.DDragon[endpoint].list() const result = await Jax.DDragon[endpoint].list()
res.send(result) res.send(result)
}) })