forked from osdio/noder-react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.js
More file actions
64 lines (48 loc) · 1.06 KB
/
request.js
File metadata and controls
64 lines (48 loc) · 1.06 KB
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
import qs from 'query-string';
import config from '../configs';
const urlPrefix = config.domain + config.apiPath;
const isDebuggingInChrome = __DEV__ && !!window.navigator.userAgent;
function filterJSON(res) {
return res.json();
}
function filterStatus(res) {
if (res.status >= 200 && res.status < 300) {
return res;
}
else {
let error = new Error(res.statusText);
error.res = res;
error.type = 'http';
throw error;
}
}
export function get(url, params) {
url = urlPrefix + url;
if (params) {
url += `?${qs.stringify(params)}`;
}
if (isDebuggingInChrome) {
console.info(`GET: `, url);
console.info(`Params: `, params)
}
return fetch(url)
.then(filterStatus)
.then(filterJSON);
}
export function post(url, body) {
url = urlPrefix + url;
if (isDebuggingInChrome) {
console.info(`POST: `, url);
console.info(`Body: `, body);
}
return fetch(url, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
})
.then(filterStatus)
.then(filterJSON);
}