forked from Bit-Developer/code-editor-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpHelper.js
More file actions
30 lines (29 loc) · 886 Bytes
/
Copy pathHttpHelper.js
File metadata and controls
30 lines (29 loc) · 886 Bytes
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
class HttpHelper {
static fetch(url, method, headers, body) {
let options = Object.assign({ method });
if (headers) {
options = Object.assign(options, { headers });
}
if (body && method !== 'GET') {
options = Object.assign(options, { body });
}
const request = new Request(url, options);
// console.log(options);
// console.log(request);
return fetch(request)
.then((response) => {
// console.log(response);
if (response.status >= 200 && response.status < 300) {
return response.json();
}
const error = new Error(`${response.statusText}(${response.status}), URL: ${response.url}`);
// error.response = response
// console.log(error);
return Promise.reject(error);
})
.catch((error) => {
throw error;
});
}
}
export default HttpHelper;