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 }], 'quote-props': [2, 'as-needed', {keywords: true, numbers: false }],
'require-jsdoc': 0, 'require-jsdoc': 0,
'semi-spacing': [2, {before: false, after: true}], 'semi-spacing': [2, {before: false, after: true}],
'semi': [2, 'always'], // 'semi': [2, 'always'],
'sort-vars': 0, 'sort-vars': 0,
'keyword-spacing': 'error', 'keyword-spacing': 'error',
'space-before-blocks': [2, 'always'], 'space-before-blocks': [2, 'always'],

View File

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

View File

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

View File

@ -4,31 +4,33 @@ export function fetchLayouts() {
return AJAX({ return AJAX({
url: `/chronograf/v1/layouts`, url: `/chronograf/v1/layouts`,
method: 'GET', method: 'GET',
resource: 'layouts',
}); });
} }
export function getMe() { export function getMe() {
return AJAX({ return AJAX({
url: `/chronograf/v1/me`, resource: 'me',
method: 'GET', method: 'GET',
}); });
} }
export function getSources() { export function getSources() {
return AJAX({ return AJAX({
url: '/chronograf/v1/sources', resource: 'sources',
}); });
} }
export function getSource(sourceID) { export function getSource(id) {
return AJAX({ return AJAX({
url: `/chronograf/v1/sources/${sourceID}`, resource: 'sources',
id,
}); });
} }
export function createSource(attributes) { export function createSource(attributes) {
return AJAX({ return AJAX({
url: '/chronograf/v1/sources', resource: 'sources',
method: 'POST', method: 'POST',
data: attributes, data: attributes,
}); });

View File

@ -1,20 +1,41 @@
import axios from 'axios'; import axios from 'axios';
export default function AJAX({ let links
export default async function AJAX({
url, url,
resource,
id,
method = 'GET', method = 'GET',
data = {}, data = {},
params = {}, params = {},
headers = {}, headers = {},
}) { }) {
try {
if (window.basepath) { if (window.basepath) {
url = `${window.basepath}${url}`; 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({ return axios({
url, url,
method, method,
data, data,
params, params,
headers, headers,
}); })
}
catch (error) {
console.error(error)
}
} }