-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcovidApi.js
66 lines (54 loc) · 1.89 KB
/
covidApi.js
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
const covidApi = {
getSummary: async () => {
return await fetchRequest(covidApiEndPoints.summary())
},
getWorldAllTimeCases: async () => {
return await fetchRequest(covidApiEndPoints.worldAllTimeCases())
},
getCountryAllTimeCases: async (country, status) => {
return await fetchRequest(covidApiEndPoints.countryAllTimeCases(country, status))
},
getWorldDaysCases: async () => {
return await fetchRequest(covidApiEndPoints.worldDaysCases())
},
getCountryDaysCases: async (country, status) => {
return await fetchRequest(covidApiEndPoints.countryDaysCases(country, status))
}
}
const covid_api_base = 'https://api.covid19api.com/'
const covidApiEndPoints = {
summary: () => {
return getApiPath('summary')
},
worldAllTimeCases: () => {
return getApiPath('world')
},
countryAllTimeCases: (country, status) => {
let end_point = `dayone/country/${country}/status/${status}`
return getApiPath(end_point)
},
countryDaysCases: (country, status) => {
let date = getDaysRange(30)
let end_point = `country/${country}/status/${status}?from=${date.start_date}&to=${date.end_date}`
return getApiPath(end_point)
},
worldDaysCases: () => {
let date = getDaysRange(30)
let end_point = `world?from=${date.start_date}&to=${date.end_date}`
return getApiPath(end_point)
}
}
// get the date at days before today
getDaysRange = (days) => {
let d = new Date()
let from_d = new Date(d.getTime() - (days * 24 * 60 * 60 * 1000))
let to_date = `${d.getFullYear()}-${d.getMonth() + 1}-${d.getDate()}`
let from_date = `${from_d.getFullYear()}-${from_d.getMonth() + 1}-${from_d.getDate()}`
return {
start_date: from_date,
end_date: to_date
}
}
getApiPath = (end_point) => {
return covid_api_base + end_point
}