2019-10-27 05:18:26 +00:00
|
|
|
/* global axios */
|
|
|
|
|
2020-05-11 06:10:14 +00:00
|
|
|
const DEFAULT_API_VERSION = 'v1';
|
2019-10-27 05:18:26 +00:00
|
|
|
|
|
|
|
class ApiClient {
|
2020-03-09 17:57:10 +00:00
|
|
|
constructor(resource, options = {}) {
|
2020-05-11 06:10:14 +00:00
|
|
|
this.apiVersion = `/api/${options.apiVersion || DEFAULT_API_VERSION}`;
|
2020-03-09 17:57:10 +00:00
|
|
|
this.options = options;
|
|
|
|
this.resource = resource;
|
|
|
|
}
|
|
|
|
|
|
|
|
get url() {
|
2020-06-22 07:49:26 +00:00
|
|
|
return `${this.baseUrl()}/${this.resource}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
baseUrl() {
|
2020-03-09 17:57:10 +00:00
|
|
|
let url = this.apiVersion;
|
2022-07-18 19:03:06 +00:00
|
|
|
|
|
|
|
if (this.options.enterprise) {
|
|
|
|
url = `/enterprise${url}`;
|
|
|
|
}
|
|
|
|
|
2020-03-09 17:57:10 +00:00
|
|
|
if (this.options.accountScoped) {
|
|
|
|
const isInsideAccountScopedURLs = window.location.pathname.includes(
|
|
|
|
'/app/accounts'
|
|
|
|
);
|
|
|
|
|
|
|
|
if (isInsideAccountScopedURLs) {
|
|
|
|
const accountId = window.location.pathname.split('/')[3];
|
|
|
|
url = `${url}/accounts/${accountId}`;
|
|
|
|
}
|
|
|
|
}
|
2020-06-22 07:49:26 +00:00
|
|
|
|
|
|
|
return url;
|
2019-10-27 05:18:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get() {
|
|
|
|
return axios.get(this.url);
|
|
|
|
}
|
|
|
|
|
|
|
|
show(id) {
|
|
|
|
return axios.get(`${this.url}/${id}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
create(data) {
|
|
|
|
return axios.post(this.url, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
update(id, data) {
|
|
|
|
return axios.patch(`${this.url}/${id}`, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(id) {
|
|
|
|
return axios.delete(`${this.url}/${id}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ApiClient;
|