|
|
@@ -1,6 +1,7 @@
|
|
|
import { Container } from 'unstated';
|
|
|
|
|
|
import axios from 'axios';
|
|
|
+import urljoin from 'url-join';
|
|
|
|
|
|
import InterceptorManager from '@commons/service/interceptor-manager';
|
|
|
|
|
|
@@ -13,6 +14,7 @@ import {
|
|
|
} from '../util/interceptor/detach-code-blocks';
|
|
|
|
|
|
import i18nFactory from '../util/i18n';
|
|
|
+import apiv3ErrorHandler from '../util/apiv3ErrorHandler';
|
|
|
|
|
|
/**
|
|
|
* Service container related to options for Application
|
|
|
@@ -68,6 +70,8 @@ export default class AppContainer extends Container {
|
|
|
this.apiGet = this.apiGet.bind(this);
|
|
|
this.apiPost = this.apiPost.bind(this);
|
|
|
this.apiRequest = this.apiRequest.bind(this);
|
|
|
+
|
|
|
+ this.apiv3Root = '/_api/v3';
|
|
|
this.apiv3 = {
|
|
|
get: this.apiv3Get.bind(this),
|
|
|
post: this.apiv3Post.bind(this),
|
|
|
@@ -345,16 +349,21 @@ export default class AppContainer extends Container {
|
|
|
}
|
|
|
|
|
|
async apiv3Request(method, path, params) {
|
|
|
- const res = await axios[method](`/_api/v3${path}`, params);
|
|
|
-
|
|
|
- return res.data;
|
|
|
+ try {
|
|
|
+ const res = await axios[method](urljoin(this.apiv3Root, path), params);
|
|
|
+ return res.data;
|
|
|
+ }
|
|
|
+ catch (err) {
|
|
|
+ const errors = apiv3ErrorHandler(err);
|
|
|
+ throw errors;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- apiv3Get(path, params) {
|
|
|
+ async apiv3Get(path, params) {
|
|
|
return this.apiv3Request('get', path, { params });
|
|
|
}
|
|
|
|
|
|
- apiv3Post(path, params) {
|
|
|
+ async apiv3Post(path, params) {
|
|
|
if (!params._csrf) {
|
|
|
params._csrf = this.csrfToken;
|
|
|
}
|
|
|
@@ -362,7 +371,7 @@ export default class AppContainer extends Container {
|
|
|
return this.apiv3Request('post', path, params);
|
|
|
}
|
|
|
|
|
|
- apiv3Put(path, params) {
|
|
|
+ async apiv3Put(path, params) {
|
|
|
if (!params._csrf) {
|
|
|
params._csrf = this.csrfToken;
|
|
|
}
|
|
|
@@ -370,7 +379,7 @@ export default class AppContainer extends Container {
|
|
|
return this.apiv3Request('put', path, params);
|
|
|
}
|
|
|
|
|
|
- apiv3Delete(path, params) {
|
|
|
+ async apiv3Delete(path, params) {
|
|
|
if (!params._csrf) {
|
|
|
params._csrf = this.csrfToken;
|
|
|
}
|