forked from OSSPhilippines/covid19-tracker-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetInformation.ts
More file actions
153 lines (138 loc) · 4.59 KB
/
getInformation.ts
File metadata and controls
153 lines (138 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import axios from "axios";
import { convertCountryCode } from "../utils/libs/convertCountry";
import { capitalizeFirstLetter } from "../utils/libs/capitalizeFirstLetter";
axios.defaults.baseURL = "https://disease.sh/v3/covid-19";
/**
* @returns An object containing the epoch timestamp of when the data was updated, and the raw data from the API
*/
export const getAllInfo: () => Promise<{
updated: number;
data: {
active: number;
cases: number;
deaths: number;
recovered: number;
deathRate: number;
recoveryRate: number;
};
}> = async () => {
let { data: globalData } = await axios.get("/all");
let { cases, deaths, recovered, updated, active } = globalData;
let deathRate = (deaths / cases) * 100;
let recoveryRate = (recovered / cases) * 100;
return {
updated,
data: {
active,
cases,
deaths,
recovered,
deathRate,
recoveryRate,
},
};
};
/**
* @param country Country string
* @returns Object containing the time when the data was updated, API countryname, formal countryname, and the raw data from the API
*/
export const getCountryInfo: (
country: string
) => Promise<{
updated: number;
formalCountryName: string;
apiCountryName: string;
data: {
cases: number;
todayCases: number;
active: number;
recovered: number;
deaths: number;
todayDeaths: number;
critical: number;
deathRate: number;
recoveryRate: number;
casesPerOneMillion: number;
};
}> = async (country) => {
// Convert country to country code
country = await convertCountryCode(country);
let formalCountryName = capitalizeFirstLetter(country);
try {
let { data: countryData } = await axios.get(`/countries/${country}`);
// prettier-ignore
let { country: apiCountryName, updated, cases, deaths, recovered, active, casesPerOneMillion, todayCases, todayDeaths, critical} = countryData;
let deathRate = (deaths / cases) * 100;
let recoveryRate = (recovered / cases) * 100;
return {
updated,
formalCountryName,
apiCountryName,
// prettier-ignore
data: {
cases, todayCases, active, recovered, deaths, todayDeaths, critical, deathRate, recoveryRate, casesPerOneMillion
},
};
} catch {
throw new Error(`Cannot find the provided country`);
}
};
type getHistoricalMode = "cases" | "deaths" | "recovered" | "all";
// This is a way of setting conditional types depending on what was passed to the mode parameter
// If the mode parameter receives "all" then the type will be different because instead of only receiving one
// set of data, it will be receiving everything
// prettier-ignore
export async function getHistorical<T extends getHistoricalMode>(
mode: T,
country?: string
): Promise<
T extends "all" ? {
date: string;
chartData: {
[key: string]: {
[key: string]: number;
};
};
} : {
date: string;
chartData: number[];
}
>;
/**
*
* @param mode What data the user wants to receive, if all, then the user will be receiving everything but it can be specified to one type of data
* @param country Country string
* @returns Object containing date already formatted, and the data which is either a number[] or the raw data in another object *if* mode is set to all
*/
export async function getHistorical(
mode: getHistoricalMode,
country = "all"
): Promise<{
date: string;
chartData:
| number[]
| {
[key: string]: {
[key: string]: number;
};
};
}> {
const { data: historicalData } = await axios.get(`/historical/${country}`);
// Get all the modes
let chartData =
country === "all" ? historicalData : historicalData["timeline"];
// If the user did not select all, then get the mode they wanted
if (mode !== "all") chartData = chartData[mode];
// Get first and last date
const dates = Object.keys(mode === "all" ? chartData["cases"] : chartData);
// Label for chart
const informationType =
mode === "all" ? "Data" : mode.charAt(0).toUpperCase() + mode.slice(1);
const date = `${informationType} from ${dates.shift()} to ${dates.pop()}`;
// If mode is not all then set the chartData to the values of the dates from the API
if (mode !== "all") chartData = Object.values(chartData) as number[];
return {
date,
chartData,
};
}