Fix syntax and valid seasons

This commit is contained in:
Nick Playfair 2025-06-05 21:01:00 +01:00
parent 7c2170ca87
commit 59b2883bd8

View File

@ -1,15 +1,15 @@
const express = require('express'); const express = require("express");
const axios = require('axios'); const axios = require("axios");
const redis = require('redis'); const redis = require("redis");
const cors = require('cors'); const cors = require("cors");
const { param, validationResult } = require('express-validator'); const { param, validationResult } = require("express-validator");
const app = express(); const app = express();
// Redis setup // Redis setup
const redisConfig = { const redisConfig = {
url: process.env.REDIS_URL, url: process.env.REDIS_URL,
database: 1, database: 1,
} };
let redisClient; let redisClient;
@ -26,16 +26,16 @@ app.use(cors());
// Legacy fetch function // Legacy fetch function
function LeagueSeason(league, season) { function LeagueSeason(league, season) {
this.url = 'https://api-football-v1.p.rapidapi.com/v3/standings'; this.url = "https://api-football-v1.p.rapidapi.com/v3/standings";
this.method = 'GET'; this.method = "GET";
this.params = { this.params = {
league: league, league: league,
season: season, season: season,
}; };
this.headers = { this.headers = {
'X-RapidAPI-Host': process.env.API_HOST, "X-RapidAPI-Host": process.env.API_HOST,
'X-RapidAPI-Key': process.env.API_KEY, "X-RapidAPI-Key": process.env.API_KEY,
} };
} }
// Fetch table from remote API // Fetch table from remote API
@ -64,7 +64,7 @@ async function getLeagueTable(req, res) {
// Fetch remotely // Fetch remotely
results = await fetchTable(league, season); results = await fetchTable(league, season);
if (results.length === 0) { if (results.length === 0) {
throw "API returned no data" throw "API returned no data";
} }
// Store table in cache // Store table in cache
await redisClient.set(seasontable, JSON.stringify(results), { await redisClient.set(seasontable, JSON.stringify(results), {
@ -75,7 +75,7 @@ async function getLeagueTable(req, res) {
// Return the league table for the corresponding season // Return the league table for the corresponding season
res.status(200).send({ res.status(200).send({
fromCache: isCached, fromCache: isCached,
table: results table: results,
}); });
} catch (error) { } catch (error) {
console.error(error); console.error(error);
@ -85,18 +85,26 @@ async function getLeagueTable(req, res) {
// Routes // Routes
// new method // new method
app.get('/league/:league/:season', getLeagueTable); app.get("/league/:league/:season", getLeagueTable);
// Current Premier League Table // Current Premier League Table
app.get('/pl', getLeagueTable); app.get("/pl", getLeagueTable);
app.get(
app.get('/v2/:league/:year', "/v2/:league/:year",
[ [
// League id must be one of these // League id must be one of these
param('league').isIn(['39', '40', '41', '42']), param("league").isIn(["39", "40", "41", "42"]),
// Year must be no earlier than 2018 // Year must be no earlier than 2018
param('year').isIn(['2018', '2019', '2020', '2021']), param("year").isIn([
"2018",
"2019",
"2020",
"2021",
"2022",
"2023",
"2024",
]),
], ],
async (req, res) => { async (req, res) => {
// Validate // Validate
@ -116,13 +124,13 @@ app.get('/v2/:league/:year',
if (leagueTable) { if (leagueTable) {
res.status(200).send({ res.status(200).send({
table: JSON.parse(leagueTable), table: JSON.parse(leagueTable),
message: 'data retrieved from cache', message: "data retrieved from cache",
}); });
} else { } else {
// Fetch from the API // Fetch from the API
reqSeason = new LeagueSeason(league, year); reqSeason = new LeagueSeason(league, year);
const leagueTable = await axios.get( const leagueTable = await axios.get(
'https://v3.football.api-sports.io/standings', "https://v3.football.api-sports.io/standings",
reqSeason reqSeason
); );
// Save result to cache // Save result to cache
@ -130,14 +138,15 @@ app.get('/v2/:league/:year',
// Return data from API // Return data from API
res.status(200).send({ res.status(200).send({
table: leagueTable.data, table: leagueTable.data,
message: 'cache miss', message: "cache miss",
}); });
} }
}); });
} catch (err) { } catch (err) {
res.status(500).send({ message: err.message }); res.status(500).send({ message: err.message });
} }
}); }
);
app.listen(process.env.PORT || 3001, () => { app.listen(process.env.PORT || 3001, () => {
console.log(`Server running`); console.log(`Server running`);