forked from disease-sh/node-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
286 lines (261 loc) · 12.3 KB
/
index.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
const fetch = require('@aero/centra'),
curSettings = { baseUrl: 'https://disease.sh' },
fetchJson = (path) => fetch(`${curSettings.baseUrl}/${path}`).json(),
yesterday = {}, twoDaysAgo = {}, jhucsse = {}, historical = {}, nyt = {}, apple = {}
const createPath = (opts, path) => {
if(opts.sort || String(opts.strict) !== 'undefined' || opts.yesterday || String(opts.allowNull) !== 'undefined') {
path += '?'
if(opts.sort)
path += `sort=${opts.sort}`
if(opts.yesterday)
path += (opts.sort ?'&':'')+'yesterday='+opts.yesterday
if(opts.twoDaysAgo)
path += (opts.sort || opts.yesterday ?'&':'')+'twoDaysAgo='+opts.twoDaysAgo
if(String(opts.allowNull) !== 'undefined')
path += (opts.sort || opts.yesterday || opts.twoDaysAgo ?'&':'')+'allowNull='+opts.allowNull
if(String(opts.strict) !== 'undefined')
path += (opts.sort || opts.yesterday || opts.twoDaysAgo || String(opts.allowNull) !== 'undefined' ?'&':'')+'strict='+opts.strict
}
return path
}
const _all = (opts) => fetchJson(createPath(opts, `v3/covid-19/all`))
/**
* Specify settings to the wrapper
* @param {object} opts object holding the options
* @param {string} opts.baseUrl url to use for requests
*/
const settings = (opts = {}) => (curSettings.baseUrl = opts.baseUrl)
/**
* Retrieve a summary of global data
* @param {object} opts object holding the options for that request
* @param {boolean} opts.allowNull whether to allow null values (true) or automatically transform them to 0 (false)
* @returns {object} summary object
*/
const all = (opts = {}) => _all({...opts, yesterday: false})
/**
* Retrieve country specific data
* @param {object} opts object holding the options for that request
* @param {string|string[]} opts.country country name/s to be queried
* @param {boolean} opts.allowNull whether to allow null values (true) or automatically transform them to 0 (false)
* @param {string} opts.sort property name which will be used for sorting
* @param {boolean} opts.strict whether to use strict name checking or not
* @returns {object|object[]} country specific data
*/
const countries = (opts = {}) => {
let path = 'v3/covid-19/countries'
if(opts.country)
path += `/${Array.isArray(opts.country) ? (opts.country.join('|')) : opts.country}`
return fetchJson(createPath(opts, path))
}
/**
* Retrieve continent specific data
* @param {object} opts object holding the options for that request
* @param {string|string[]} opts.continent continent name/s to be queried
* @param {boolean} opts.allowNull whether to allow null values (true) or automatically transform them to 0 (false)
* @param {string} opts.sort property name which will be used for sorting
* @param {boolean} opts.strict whether to use strict name checking or not
* @returns {object|object[]} continent specific data
*/
const continents = (opts = {}) => {
let path = 'v3/covid-19/continents'
if(opts.continent)
path += `/${opts.continent}`
return fetchJson(createPath(opts, path))
}
/**
* Retrieve state specific data
* @param {object} opts object holding the options for that request
* @param {string|string[]} opts.state state name/s to be queried
* @param {boolean} opts.allowNull whether to allow null values (true) or automatically transform them to 0 (false)
* @param {string} opts.sort property name which will be used for sorting
* @param {boolean} opts.strict whether to use strict name checking or not
* @returns {object|object[]} state specific data
*/
const states = (opts = {}) => {
let path = 'v3/covid-19/states'
if(opts.state)
path += `/${Array.isArray(opts.state) ? (opts.state.join('|')) : opts.state}`
return fetchJson(createPath(opts, path))
}
/**
* Retrieve a summary of yesterdays global data
* @param {object} opts object holding the options for that request
* @param {boolean} opts.allowNull whether to allow null values (true) or automatically transform them to 0 (false)
* @returns {object} summary object
*/
yesterday.all = (opts = {}) => _all({...opts, yesterday: true})
/**
* Retrieve yesterdays country specific data
* @param {object} opts object holding the options for that request
* @param {string|string[]} opts.country country name/s to be queried
* @param {boolean} opts.allowNull whether to allow null values (true) or automatically transform them to 0 (false)
* @param {string} opts.sort property name which will be used for sorting
* @param {boolean} opts.strict whether to use strict name checking or not
* @returns {object|object[]} country specific data
*/
yesterday.countries = (opts = {}) => countries({...opts, yesterday: true})
/**
* Retrieve yesterdays continent specific data
* @param {object} opts object holding the options for that request
* @param {string|string[]} opts.continent continent name/s to be queried
* @param {string} opts.sort property name which will be used for sorting
* @param {boolean} opts.strict whether to use strict name checking or not
* @returns {object|object[]} continent specific data
*/
yesterday.continents = (opts = {}) => continents({...opts, yesterday: true})
/**
* Retrieve yesterdays state specific data
* @param {object} opts object holding the options for that request
* @param {string|string[]} opts.state state name/s to be queried
* @param {boolean} opts.allowNull whether to allow null values (true) or automatically transform them to 0 (false)
* @param {string} opts.sort property name which will be used for sorting
* @param {boolean} opts.strict whether to use strict name checking or not
* @returns {object|object[]} state specific data
*/
yesterday.states = (opts = {}) => states({...opts, yesterday: true})
/**
* Retrieve a summary of global data from two days ago
* @param {object} opts object holding the options for that request
* @param {boolean} opts.allowNull whether to allow null values (true) or automatically transform them to 0 (false)
* @returns {object} summary object
*/
twoDaysAgo.all = (opts = {}) => _all({...opts, twoDaysAgo: true})
/**
* Retrieve country specific data from two days ago
* @param {object} opts object holding the options for that request
* @param {string|string[]} opts.country country name/s to be queried
* @param {boolean} opts.allowNull whether to allow null values (true) or automatically transform them to 0 (false)
* @param {string} opts.sort property name which will be used for sorting
* @param {boolean} opts.strict whether to use strict name checking or not
* @returns {object|object[]} country specific data
*/
twoDaysAgo.countries = (opts = {}) => countries({...opts, twoDaysAgo: true})
/**
* Retrieve continent specific data from two days ago
* @param {object} opts object holding the options for that request
* @param {string|string[]} opts.continent continent name/s to be queried
* @param {string} opts.sort property name which will be used for sorting
* @param {boolean} opts.strict whether to use strict name checking or not
* @returns {object|object[]} continent specific data
*/
twoDaysAgo.continents = (opts = {}) => continents({...opts, twoDaysAgo: true})
/**
* Retrieve an array of infected countries
* @returns {object[]} array of infected countries
*/
jhucsse.all = () => fetchJson('v3/covid-19/jhucsse')
/**
* Retrieve county specific data
* @param {object} opts object holding the options for that request
* @param {string|string[]} opts.county county name/s to be queried
* @returns {object|object[]} county specific data
*/
jhucsse.counties = (opts = {}) => {
let path = 'v3/covid-19/jhucsse/counties'
if(opts.county)
path += `/${Array.isArray(opts.county) ? (opts.county.join('|')) : opts.county}`
return fetchJson(path)
}
/**
* Retrieve an array of the global timeline
* @param {object} opts object holding the options for that request
* @param {string|number} opts.days the number of days to get data for, or 'all'
* @returns {object[]} timeline data
*/
historical.all = (opts = {}) => fetchJson(`v3/covid-19/historical/all${opts.days ? `?lastdays=${opts.days}`:''}`)
/**
* Retrieve an array of the country specific timelines
* @param {object} opts object holding the options for that request
* @param {string|string[]} opts.country country name/s to be queried
* @param {string|string[]} opts.province province name/s to be queried (must have 1 country)
* @param {string|number} opts.days the number of days to get data for, or 'all'
* @returns {object[]} timeline data
*/
historical.countries = (opts = {}) => {
let path = 'v3/covid-19/historical'
if(opts.country) {
path += `/${Array.isArray(opts.country) ? (opts.country.join('|')) : opts.country}`
if(opts.province)
path += `/${Array.isArray(opts.province) ? (opts.province.join('|')) : opts.province}`
if(opts.days)
path += `?lastdays=${opts.days}`
}
return fetchJson(path)
}
/**
* Retrieve a timeline of USA data
* @returns {object[]} USA timeline data
*/
nyt.usa = () => fetchJson('v3/covid-19/nyt/usa')
/**
* Retrieve a timeline of US state specific data
* @param {object} opts object holding the options for that request
* @param {string} opts.state state name to be queried
* @returns {object|object[]} state specific timeline data
*/
nyt.states = (opts = {}) => {
let path = 'v3/covid-19/nyt/states'
if(opts.state)
path += `/${opts.state}`
return fetchJson(path)
}
/**
* Retrieve a timeline of US county specific data
* @param {object} opts object holding the options for that request
* @param {string} opts.county county name to be queried
* @returns {object|object[]} county specific timeline data
*/
nyt.counties = (opts = {}) => {
let path = 'v3/covid-19/nyt/counties'
if(opts.county)
path += `/${opts.county}`
return fetchJson(path)
}
/**
* Retrieve an array of available countries
* @returns {string[]} country names
*/
apple.countries = () => fetchJson('v3/covid-19/apple/countries')
/**
* Retrieve a list of available subregions for a country
* @param {string} country country name to be queried
* @returns {object} object containing country name and list of subregions
*/
apple.subregions = (country) => fetchJson(`v3/covid-19/apple/countries/${country}`)
/**
* Retrieve mobility data for a specific country and subregion
* @param {object} opts object holding the options for that request
* @param {string} opts.country country name to be queried
* @param {string|string[]} opts.subregion subregion name/s to be queried
* @returns {object|object[]} mobility data
*/
apple.mobilityData = (opts = {}) => {
let path = 'v3/covid-19/apple/countries'
if(opts.country) {
path += `/${opts.country}`
if(opts.subregion)
path += `/${Array.isArray(opts.subregion) ? (opts.subregion.join('|')) : opts.subregion}`
}
return fetchJson(path)
}
/**
* Retrieve official government data
* @param {string} country country name to be queried (empty to get an array of names)
* @param {boolean} opts.allowNull whether to allow null values (true) or automatically transform them to 0 (false)
* @returns {object} official government data
*/
const gov = (country) => fetchJson(`v3/covid-19/gov/${country ? country : ''}`)
module.exports = {
settings,
all,
countries,
continents,
states,
yesterday,
twoDaysAgo,
jhucsse,
historical,
nyt,
apple,
gov
}