-
Notifications
You must be signed in to change notification settings - Fork 12
/
APIService.js
147 lines (128 loc) · 4.22 KB
/
APIService.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
/*eslint no-process-env: 0*/
import axios from 'axios';
import {get, omit, isPlainObject, isString, defaults } from 'lodash';
import { currentUserToken, getAPIURL } from '../common/utils';
const APIServiceProvider = {};
const RESOURCES = [
{ name: 'concepts', relations: [] },
{ name: 'mappings', relations: [] },
{ name: 'orgs', relations: ['pins', 'sources', 'collections'] },
{ name: 'sources', relations: ['concepts', 'mappings'] },
{ name: 'collections', relations: ['concepts', 'mappings'] },
{ name: 'users', relations: ['login', 'signup', 'verify', 'password', 'pins', 'collections', 'sources', 'orgs'] },
{ name: 'user', relations: ['orgs'] },
{ name: 'feedback', relations: [] },
{ name: 'version', relations: [] },
{ name: 'locales', relations: [] },
{ name: 'toggles', relations: [] },
{ name: 'new', relations: [] },
];
class APIService {
constructor(name, id, relations) {
const apiURL = getAPIURL();
if (name === 'empty') this.URL = `${apiURL}/`;
else this.URL = `${apiURL}/${name}/`;
this.headers = {
'Content-Type': 'application/json',
Accept: '*/*'
};
if (id) {
this.URL += `${id}/`;
}
if (relations) {
relations.forEach(relation => {
this[relation] = relationId => {
this.URL += `${relation}/${relationId ? `${relationId}/` : ''}`;
return this;
};
});
}
}
appendToUrl(s) {
this.URL += s;
return this;
}
overrideURL(url) {
this.URL = `${getAPIURL()}${url}`;
return this;
}
head(token, headers = {}, query, raw=false) {
return this.sendRequest('HEAD', null, token, headers, query, raw);
}
get(token, headers = {}, query, raw=false) {
return this.sendRequest('GET', null, token, headers, query, raw);
}
getBlob(token, headers = {}, query) {
return this.sendRequest('GET', null, token, headers, query, true, true);
}
post(data, token, headers = {}, query, raw=false) {
return this.sendRequest('POST', data, token, headers, query, raw);
}
put(data, token, headers = {}, query, raw=false) {
return this.sendRequest('PUT', data, token, headers, query, raw);
}
delete(data, token, headers = {}, query, raw=false) {
return this.sendRequest('DELETE', data, token, headers, query, raw);
}
sendRequest(method, data, token, headers, query, raw=false, blob=false) {
const request = this.getRequest(method, data, token, headers, query, blob);
return axios(request)
.then(response => response || null)
.catch(error => {
if(raw)
return error;
return error.response ? error.response.data : error.message;
});
}
getRequest(method, data, token, headers, query, blob=false) {
const request = {
url: this.URL,
method,
headers: headers === false ? {} : this.getHeaders(token, headers),
params: this.getQueryParams(query),
data,
};
if(blob)
request.responseType = 'blob'
return request
}
request(method, data, token, config) {
let headers = {};
let query = {};
if (isPlainObject(config)) {
headers = get(config, 'headers', {});
query = get(config, 'query', {});
}
let request = this.getRequest(method, data, token, headers, query);
request = {...request, ...omit(config, ['headers', 'query'])};
return axios(request);
};
getHeaders(token, headers) {
token = token || (token !== false ? currentUserToken() : token);
const obj = defaults(headers, this.headers);
if (token) obj['Authorization'] = `Token ${token}`;
return obj;
}
getQueryParams(query) {
if (isPlainObject(query)) {
return query;
}
if (isString(query)) {
const questionMarkSplit = query.split('?');
if (questionMarkSplit.length === 2) {
const params = {};
const ampersandSplit = questionMarkSplit[1].split('&');
ampersandSplit.forEach(param => {
const paramSplit = param.split('=');
params[paramSplit[0]] = get(paramSplit, '[1]', true);
});
return params;
}
}
return {};
}
}
RESOURCES.forEach(resource => {
APIServiceProvider[resource.name] = (id, query) => new APIService(resource.name, id, resource.relations, query);
});
export default APIServiceProvider;