Add resource and id methods to AJAX method to access link URLs.

pull/922/head
Hunter Trujillo 2017-02-13 13:19:34 -07:00
parent 9cc425d39d
commit d87a3350f3
5 changed files with 41 additions and 18 deletions

View File

@ -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'],

View File

@ -3,6 +3,6 @@ import AJAX from 'utils/ajax';
export function getDashboards() {
return AJAX({
method: 'GET',
url: `/chronograf/v1/dashboards`,
resource: 'dashboards',
});
}

View File

@ -87,7 +87,7 @@ export async function getAllHosts(proxyLink, telegrafDB) {
export function getMappings() {
return AJAX({
method: 'GET',
url: `/chronograf/v1/mappings`,
resource: 'mappings',
});
}

View File

@ -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,
});

View File

@ -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,
});
}