forked from JinleeJeong/study-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apiClient.js
36 lines (30 loc) · 782 Bytes
/
apiClient.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
import axios from 'axios';
const apiUrl = 'https://api.studyhub.xyz';
const methods = ['get', 'post', 'put', 'delete'];
function formatUrl(path) {
return `${apiUrl}${path}`;
}
class ApiClient {
constructor() {
methods.forEach((method) => {
this[method] = (path, data) => new Promise((resolve, reject) => {
axios({
method: method,
url: formatUrl(path),
data: data,
withCredentials: true,
})
.then(res => {
resolve(res.data);
})
.catch(err => {
const response = err.response;
reject({statusCode : response.status, message: response.data});
});
});
return this[method];
});
}
}
export default new ApiClient();
export { apiUrl };