-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
45 lines (35 loc) · 1.05 KB
/
utils.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
const fetchApi = (props) => {
console.log('fetchApi', props);
let {method, body, url, headers} = props;
console.log('method', method);
console.log('body', body);
console.log('url', url);
console.log('headers', headers);
if(!method || (method==='post' && !body) || !url) return Promise.reject('one of the props is null');
console.log('fetchApi2');
if(!headers)
{
console.log('no header');
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
}
return fetch(url, method==='get' ? {
method,
headers,
} : {
method,
headers,
body,
}
)
.then(response => {
if (response.status >= 200 && response.status <= 299) {
return response.json();
} else{
throw Error(response.statusText);
}
})
}
export default fetchApi;