-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
88 lines (65 loc) · 2.12 KB
/
api.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
/*
* api.js
*/
import axios from 'axios'
import {EPIVAR_NODES} from "./config";
import queryString from './helpers/queryString'
/*
* API functions
*/
export function fetchAssays(node, params) {
return get(node, '/assays/list', params)
}
export function fetchPeaks(node, params) {
return get(node, '/peaks/query', params)
}
export function fetchPositions(node, params, cancelToken) {
return get(node, '/autocomplete/positions', params, {cancelToken})
}
export const fetchDataset = (node) => get(node, '/dataset');
export const fetchDatasets = (_) => Promise.all(EPIVAR_NODES.map((node) => fetchDataset(node)));
export const fetchOverviewConfig = (node) => get(node, '/overview/config');
export const fetchManhattanData = (node, {chrom, assay}) => get(node, `/overview/assays/${assay}/topBinned/${chrom}`);
export function createSession(node, params) {
return post(node, '/sessions/create', params)
}
export function fetchUser(node) {
return get(node, '/auth/user');
}
export function saveUser(node, user) {
// Only terms agreement info is actually save-able. Everything else is
// read-only from the identity provider.
return put(node, '/auth/user', user);
}
export function fetchMessages(node) {
return get(node, '/messages/list');
}
// Helpers
function fetchAPI(node, url, params, options = {}) {
const {method = "get", ...other} = options;
const finalURL = `${node}/api${url}${(method === "get" && params) ? `?${queryString(params)}` : ""}`;
const data = (["patch", "post", "put"].includes(method) && params)
? params
: undefined;
const config = {
method,
url: finalURL,
data,
withCredentials: true,
...other,
};
return axios(config).then(({ data }) => (
data.ok
? Promise.resolve(data.data)
: Promise.reject(new Error(data.message))
));
}
function get(node, url, params, options) {
return fetchAPI(node, url, params, options)
}
function post(node, url, params, options = {}) {
return fetchAPI(node, url, params, { ...options, method: 'post' })
}
function put(node, url, params, options = {}) {
return fetchAPI(node, url, params, { ...options, method: 'put' })
}