diff --git a/ui/.eslintrc b/ui/.eslintrc index 360af5a03..080c8b330 100644 --- a/ui/.eslintrc +++ b/ui/.eslintrc @@ -182,7 +182,7 @@ 'quote-props': [2, 'as-needed', {keywords: true, numbers: false }], 'require-jsdoc': 0, 'semi-spacing': [2, {before: false, after: true}], - 'semi': [2, 'always'], + // 'semi': [2, 'always'], 'sort-vars': 0, 'keyword-spacing': 'error', 'space-before-blocks': [2, 'always'], diff --git a/ui/src/dashboards/apis/index.js b/ui/src/dashboards/apis/index.js index f0106f76b..6778ae6ce 100644 --- a/ui/src/dashboards/apis/index.js +++ b/ui/src/dashboards/apis/index.js @@ -3,6 +3,6 @@ import AJAX from 'utils/ajax'; export function getDashboards() { return AJAX({ method: 'GET', - url: `/chronograf/v1/dashboards`, + resource: 'dashboards', }); } diff --git a/ui/src/hosts/apis/index.js b/ui/src/hosts/apis/index.js index 1a5854afa..6aa5b295f 100644 --- a/ui/src/hosts/apis/index.js +++ b/ui/src/hosts/apis/index.js @@ -87,7 +87,7 @@ export async function getAllHosts(proxyLink, telegrafDB) { export function getMappings() { return AJAX({ method: 'GET', - url: `/chronograf/v1/mappings`, + resource: 'mappings', }); } diff --git a/ui/src/shared/apis/index.js b/ui/src/shared/apis/index.js index fee75ba61..087e51bb2 100644 --- a/ui/src/shared/apis/index.js +++ b/ui/src/shared/apis/index.js @@ -4,31 +4,33 @@ export function fetchLayouts() { return AJAX({ url: `/chronograf/v1/layouts`, method: 'GET', + resource: 'layouts', }); } export function getMe() { return AJAX({ - url: `/chronograf/v1/me`, + resource: 'me', method: 'GET', }); } export function getSources() { return AJAX({ - url: '/chronograf/v1/sources', + resource: 'sources', }); } -export function getSource(sourceID) { +export function getSource(id) { return AJAX({ - url: `/chronograf/v1/sources/${sourceID}`, + resource: 'sources', + id, }); } export function createSource(attributes) { return AJAX({ - url: '/chronograf/v1/sources', + resource: 'sources', method: 'POST', data: attributes, }); diff --git a/ui/src/utils/ajax.js b/ui/src/utils/ajax.js index 5292f6490..1fd431acf 100644 --- a/ui/src/utils/ajax.js +++ b/ui/src/utils/ajax.js @@ -1,20 +1,41 @@ import axios from 'axios'; -export default function AJAX({ +let links + +export default async function AJAX({ url, + resource, + id, method = 'GET', data = {}, params = {}, headers = {}, }) { - if (window.basepath) { - url = `${window.basepath}${url}`; + try { + if (window.basepath) { + url = `${window.basepath}${url}` + } + + if (resource) { + if (!links) { + const linksRes = await axios({ + url: '/chronograf/v1/', + method: 'GET', + }) + links = linksRes.data + } + url = id ? `${links[resource]}/${id}` : links[resource] + } + + return axios({ + url, + method, + data, + params, + headers, + }) + } + catch (error) { + console.error(error) } - return axios({ - url, - method, - data, - params, - headers, - }); }