You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
854 B
49 lines
854 B
5 years ago
|
import axios from "axios";
|
||
|
|
||
5 years ago
|
export default class axiosApi {
|
||
5 years ago
|
|
||
|
constructor(urlObj) {
|
||
|
this.staticMethods = ["get", "post", "put", "delete"];
|
||
5 years ago
|
this.baseURL = '/';
|
||
|
|
||
5 years ago
|
this.method = urlObj.method;
|
||
|
this.url = urlObj.url;
|
||
|
this.data = urlObj.data;
|
||
|
}
|
||
|
|
||
|
init() {
|
||
5 years ago
|
axios.defaults.baseURL = this.baseURL;
|
||
|
|
||
5 years ago
|
if (this.checkMethod()) {
|
||
|
return this[this.method]();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
checkMethod() {
|
||
|
return this.staticMethods.includes(this.method);
|
||
|
}
|
||
|
|
||
|
get() {
|
||
|
const params = this.data;
|
||
|
return axios.get(this.url, {
|
||
|
params
|
||
|
});
|
||
|
}
|
||
|
|
||
|
post() {
|
||
|
return axios.post(this.url, this.data);
|
||
|
}
|
||
|
|
||
|
put() {
|
||
|
const data = this.data;
|
||
|
data['_method'] = 'put';
|
||
|
return axios.put(this.url, data);
|
||
|
}
|
||
|
|
||
|
delete() {
|
||
|
const data = this.data;
|
||
|
data['_method'] = 'delete';
|
||
|
return axios.put(this.url, data);
|
||
|
}
|
||
|
}
|