-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
executable file
·68 lines (59 loc) · 1.93 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
const fetch = require('isomorphic-fetch')
const url = '//api.openaq.org'
const buildQueryString = (params) => {
let queryString = []
for (var prop in params) queryString.push(encodeURIComponent(prop) + '=' + encodeURIComponent(params[prop]))
return queryString.join('&')
}
const get = (endpoint, params) => {
if (!params) {
return fetch(url + endpoint)
.then(response => {
if (response.status >= 400) {
throw new Error("Bad response from server")
}
return response.json()
})
}
return fetch(url + endpoint + params)
.then(response => {
if (response.status >= 400) {
throw new Error("Bad response from server")
}
return response.json()
})
}
class OpenAQ {
cities(params) {
if (params) return get('/v1/cities/?' + buildQueryString(params))
return get('/v1/cities/')
}
countries(params) {
if (params) return get('/v1/countries/?' + buildQueryString(params))
return get('/v1/countries/')
}
fetches(params) {
if (params) return get('/v1/fetches/?' + buildQueryString(params))
return get('/v1/fetches/')
}
latest(params) {
if (params) return get('/v1/latest/?' + buildQueryString(params))
return get('/v1/latest/')
}
locations(params){
if (params) return get('/v1/locations/?' + buildQueryString(params))
return get('/v1/locations/')
}
measurements(params){
if (params) return get('/v1/measurements/?' + buildQueryString(params))
return get('/v1/measurements/')
}
parameters(){
return get('/v1/parameters/')
}
sources(params){
if (params) return get('/v1/sources/?' + buildQueryString(params))
return get('/v1/sources/')
}
}
module.exports = OpenAQ