forked from resource-watch/resource-watch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.js
37 lines (34 loc) · 961 Bytes
/
query.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
import { WRIAPI } from 'utils/axios';
/**
* Send GET request to /query
* Check out the API docs for this endpoint {@link https://resource-watch.github.io/doc-api/index-rw.html#query|here}
* @param {String} token token User's token
* @param {*} sql mandatory parameter
* @param {Object} params request paremeters to API.
*/
export const fetchQuery = (token, sql, params = {}) => {
if (!token) {
console.error('This is an authorized endpoint. A token need to be provided.');
return null;
}
if (!sql) {
console.error('A SQL query is mandatory to perform this fetching.');
return null;
}
return WRIAPI.get('query', {
headers: {
...WRIAPI.defaults.headers,
Authorization: token
},
params: {
sql,
...params
}
})
.then((response) => {
if (response.status === 200) return response;
throw response;
})
.catch((err) => { throw err; });
};
export default { fetchQuery };