-
Notifications
You must be signed in to change notification settings - Fork 225
/
Copy pathhtttp.service.js
41 lines (29 loc) · 1.24 KB
/
htttp.service.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
import Axios from "axios";
const API_URL = process.env.REACT_APP_API_URL;
Axios.defaults.baseURL = API_URL;
export class HttpService {
_axios = Axios.create();
addRequestInterceptor = (onFulfilled, onRejected) => {
this._axios.interceptors.request.use(onFulfilled, onRejected);
};
addResponseInterceptor = (onFulfilled, onRejected) => {
this._axios.interceptors.response.use(onFulfilled, onRejected);
};
get = async (url) => await this.request(this.getOptionsConfig("get", url));
post = async (url, data) => await this.request(this.getOptionsConfig("post", url, data));
put = async (url, data) => await this.request(this.getOptionsConfig("put", url, data));
patch = async (url, data) => await this.request(this.getOptionsConfig("patch", url, data));
delete = async (url) => await this.request(this.getOptionsConfig("delete", url));
getOptionsConfig = (method, url, data) => {
return { method, url, data, headers: { 'Content-Type': 'application/vnd.api+json' } };
};
request(options) {
return new Promise((resolve, reject) => {
this._axios
.request(options)
.then((res) => resolve(res.data))
.catch((ex) => reject(ex.response.data));
});
}
}
export default new HttpService();