|
| 1 | +import axios from "axios"; |
| 2 | +axios.defaults.baseURL = "https://disease.sh/v3/covid-19"; |
| 3 | + |
| 4 | +let countryCodes: { [key: string]: string } = {}; |
| 5 | +(async () => { |
| 6 | + countryCodes = (await axios.get("http://country.io/names.json")).data; |
| 7 | +})(); |
| 8 | + |
| 9 | +export interface PlainData { |
| 10 | + data: { |
| 11 | + [key: string]: string; |
| 12 | + }; |
| 13 | + metainfo: { |
| 14 | + [key: string]: number | string; |
| 15 | + }; |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * @param isPlain Set to true to recieve an object containing the responses instead of the rows |
| 20 | + * @returns an object containing the data and metainfo **if isPlain is set to true** |
| 21 | + * @returns an array in the format of [timestamp, rows] **if isPlain is set to false** |
| 22 | + */ |
| 23 | +export const getAllInfo: ( |
| 24 | + isPlain?: boolean |
| 25 | +) => Promise<[number, (string[] | string)[]] | PlainData> = async ( |
| 26 | + isPlain = false |
| 27 | +) => { |
| 28 | + let { data: globalData } = await axios.get("/all"); |
| 29 | + let { cases, deaths, recovered, updated } = globalData; |
| 30 | + |
| 31 | + let mortalityPercentage = ((deaths / cases) * 100).toFixed(2) + "%"; |
| 32 | + let recoveredPercentage = ((recovered / cases) * 100).toFixed(2) + "%"; |
| 33 | + |
| 34 | + [cases, deaths, recovered] = [cases, deaths, recovered].map((num: number) => |
| 35 | + num.toLocaleString("en-US", { maximumFractionDigits: 0 }) |
| 36 | + ); |
| 37 | + |
| 38 | + // Return object containing information if isPlain is set to true |
| 39 | + if (isPlain) { |
| 40 | + return { |
| 41 | + data: { |
| 42 | + Cases: cases, |
| 43 | + Deaths: deaths, |
| 44 | + "Mortality %": mortalityPercentage, |
| 45 | + Recovered: recovered, |
| 46 | + "Recovered %": recoveredPercentage, |
| 47 | + }, |
| 48 | + metainfo: { |
| 49 | + updated, |
| 50 | + }, |
| 51 | + }; |
| 52 | + } |
| 53 | + |
| 54 | + // Return rows if isPlain is set to false |
| 55 | + // prettier-ignore |
| 56 | + return [updated, [ |
| 57 | + ["Cases".magenta, "Deaths".red,"Recovered".green, "Mortality %".red,"Recovered %".green], |
| 58 | + [cases, deaths, recovered, mortalityPercentage, recoveredPercentage]]] |
| 59 | +}; |
| 60 | + |
| 61 | +/** |
| 62 | + * @param country the country code or string that the user provides from req.params or CLI |
| 63 | + * @param isPlain Set to true to recieve an object containing the responses instead of the rows |
| 64 | + * @returns an object containing the data and metainfo **if isPlain is set to true** |
| 65 | + * @returns an array in the format of [timestamp, API countryname, formal countryname, rows[]] **if isPlain is false |
| 66 | + */ |
| 67 | +export const getCountryInfo: ( |
| 68 | + country: string, |
| 69 | + isPlain?: boolean |
| 70 | +) => Promise< |
| 71 | + [number, string, string, (string[] | string)[]] | PlainData |
| 72 | +> = async (country, isPlain) => { |
| 73 | + // Wait 1 second for countryCodes to initialize, needed for CLI |
| 74 | + if (Object.keys(countryCodes).length === 0) { |
| 75 | + await new Promise((resolve) => { |
| 76 | + setTimeout(resolve, 1000); |
| 77 | + }); |
| 78 | + } |
| 79 | + |
| 80 | + country = |
| 81 | + country.length < 3 ? countryCodes[country.toUpperCase()] : country; // Convert country code to country name |
| 82 | + |
| 83 | + if (country === undefined || typeof country === "undefined") |
| 84 | + throw new Error(`Cannot find provided country`); |
| 85 | + |
| 86 | + try { |
| 87 | + let { data: countryData } = await axios.get(`/countries/${country}`); |
| 88 | + // prettier-ignore |
| 89 | + let { country: countryName, updated, cases, deaths, recovered, active, casesPerOneMillion, todayCases, todayDeaths, critical} = countryData; |
| 90 | + |
| 91 | + let mortalityPercentage = ((deaths / cases) * 100).toFixed(2) + "%"; |
| 92 | + let recoveredPercentage = ((recovered / cases) * 100).toFixed(2) + "%"; |
| 93 | + |
| 94 | + // prettier-ignore |
| 95 | + [ cases, deaths, recovered, active, casesPerOneMillion, todayCases, todayDeaths, critical ] = |
| 96 | + [ cases, deaths, recovered, active, casesPerOneMillion, todayCases, todayDeaths, critical, |
| 97 | + ].map((num: number) => |
| 98 | + num.toLocaleString("en-US", { maximumFractionDigits: 0 }) |
| 99 | + ); |
| 100 | + |
| 101 | + // Return object containing information if isPlain is set to true |
| 102 | + if (isPlain) { |
| 103 | + return { |
| 104 | + data: { |
| 105 | + Cases: cases, |
| 106 | + "Today Cases": todayCases, |
| 107 | + Active: active, |
| 108 | + Recovered: recovered, |
| 109 | + Deaths: deaths, |
| 110 | + "Today Deaths": todayDeaths, |
| 111 | + Critical: critical, |
| 112 | + "Mortality %": mortalityPercentage, |
| 113 | + "Recovery %": recoveredPercentage, |
| 114 | + "Cases/Million": casesPerOneMillion, |
| 115 | + }, |
| 116 | + metainfo: { |
| 117 | + updated, |
| 118 | + countryName, |
| 119 | + }, |
| 120 | + }; |
| 121 | + } |
| 122 | + |
| 123 | + //prettier-ignore |
| 124 | + return [updated, country, countryName, [ |
| 125 | + [ "Cases".magenta, "Deaths".red, "Recovered".green, "Active".blue, "Cases/Million".blue,], |
| 126 | + [ cases, deaths, recovered, active, casesPerOneMillion,], |
| 127 | + [ "Today Cases".magenta, "Today Deaths".red, "Critical".red, "Mortaility %".red, "Recovery %".green], |
| 128 | + [ todayCases, todayDeaths, critical, mortalityPercentage, recoveredPercentage]] |
| 129 | + ] |
| 130 | + } catch { |
| 131 | + throw new Error(`Cannot find the provided country`); |
| 132 | + } |
| 133 | +}; |
0 commit comments