From 932a8e568e5f49e78bad6ce5f654aed187d36d16 Mon Sep 17 00:00:00 2001 From: Valentin Kaelin Date: Wed, 4 Sep 2019 20:11:16 +0200 Subject: [PATCH] Make the Jax wrapper a little easier to use --- server/src/Jax/Jax.js | 38 +++++++++++++++++++------------------- server/src/server.js | 18 +++++++----------- 2 files changed, 26 insertions(+), 30 deletions(-) diff --git a/server/src/Jax/Jax.js b/server/src/Jax/Jax.js index fd16c0f..ae03dd6 100644 --- a/server/src/Jax/Jax.js +++ b/server/src/Jax/Jax.js @@ -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() } diff --git a/server/src/server.js b/server/src/server.js index a66925f..d54351b 100644 --- a/server/src/server.js +++ b/server/src/server.js @@ -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) })