{"size": 1128, "ext": "js", "max_stars_repo_path": "server/services/ddragonVersion.js", "max_stars_repo_name": "mrdoowan/lhgWebsite", "max_stars_repo_stars_event_min_datetime": "2020-08-25T20:20:23.000Z", "max_stars_repo_stars_event_max_datetime": "2021-02-08T14:40:26.000Z", "max_issues_repo_path": "server/services/ddragonVersion.js", "max_issues_repo_name": "mrdoowan/lhgWebsite", "max_issues_repo_issues_event_min_datetime": "2020-08-26T04:53:23.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-28T01:46:50.000Z", "max_forks_repo_path": "server/services/ddragonVersion.js", "max_forks_repo_name": "mrdoowan/lhgWebsite", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": 5, "max_forks_count": null, "avg_line_length": 38.8965517241, "max_line_length": 143, "alphanum_fraction": 0.655141844}
/*  Declaring npm modules */
const redis = require('redis');

import axios from "axios";
import { CACHE_KEYS } from "../functions/apiV1/dependencies/cacheKeys";

const cache = (process.env.NODE_ENV === 'production') ? redis.createClient(process.env.REDIS_URL) : redis.createClient(process.env.REDIS_PORT);

/**
 * Gets the DDragon version of the LoL patch based on: https://ddragon.leagueoflegends.com/api/versions.json
 * @param {string} patch    Specified League of Legends patch (i.e. "10.23"). 'null' to get latest Patch
 * @returns 
 */
export const createDdragonVersionList = () => {
  const cacheKey = CACHE_KEYS.VERSIONS;
  return new Promise((resolve, reject) => {
    cache.get(cacheKey, async (err, data) => {
      if (err) { console(err); reject(err); return; }
      else if (data) { resolve(JSON.parse(data)); return; }
      axios.get(`https://ddragon.leagueoflegends.com/api/versions.json`)
      .then((res) => {
        cache.set(cacheKey, JSON.stringify(res.data, null, 2), 'EX', GLOBAL_CONSTS.TTL_DURATION);
        resolve(res.data);
      }).catch((err) => {
        reject(err);
      });
    });
  });
}