diff --git a/src/actions/configure.js b/src/actions/configure.js index be94814..6d99105 100644 --- a/src/actions/configure.js +++ b/src/actions/configure.js @@ -1,5 +1,5 @@ import {createAction} from 'redux-actions'; -import axios from '../apis'; +import * as api from '../apis'; export const FETCH_CONFIGURE_REQUEST = 'FETCH_CONFIGURE_REQUEST'; export const FETCH_CONFIGURE_SUCCESS = 'FETCH_CONFIGURE_SUCCESS'; @@ -19,7 +19,7 @@ export const fetchConfigure = () => async (dispatch) => { dispatch(fetchConfigureRequest()); try { - const {data} = await axios.electron.get('configure'); + const data = await api.getConfigure(); return dispatch(fetchConfigureSuccess(data)); } catch (err) { @@ -31,7 +31,7 @@ export const submitConfigure = (payload) => async (dispatch) => { dispatch(submitConfigureRequest()); try { - const {data} = await axios.electron.post('configure', payload); + const data = await api.postConfigure(payload); if (data.error) { return dispatch(submitConfigureFailure(data)); diff --git a/src/actions/logout.js b/src/actions/logout.js index a4db155..250f330 100644 --- a/src/actions/logout.js +++ b/src/actions/logout.js @@ -1,5 +1,5 @@ import {createAction} from 'redux-actions'; -import axios from "axios/index"; +import * as api from '../apis'; export const FETCH_LOGOUT_REQUEST = 'FETCH_LOGOUT_REQUEST'; export const FETCH_LOGOUT_SUCCESS = 'FETCH_LOGOUT_SUCCESS'; @@ -13,7 +13,7 @@ export const fetchLogout = () => async (dispatch) => { dispatch(fetchLogoutRequest()); try { - const {data} = await axios.get('logout'); + const data = await api.getLogout(); if (data.error) { return dispatch(fetchLogoutFailure(data)); diff --git a/src/actions/profile.js b/src/actions/profile.js index 92966a2..08ade41 100644 --- a/src/actions/profile.js +++ b/src/actions/profile.js @@ -1,6 +1,6 @@ import {createAction} from 'redux-actions'; -import axios from '../apis'; import {fetchConfigure} from './configure'; +import * as api from '../apis'; export const DELETE_PROFILE_REQUEST = 'DELETE_PROFILE_REQUEST'; export const DELETE_PROFILE_SUCCESS = 'DELETE_PROFILE_SUCCESS'; @@ -14,7 +14,7 @@ export const deleteProfile = (payload) => async (dispatch) => { dispatch(deleteProfileRequest()); try { - const {data} = await axios.electron.delete('profile', { + const {data} = await api.deleteProfile({ params: payload }); dispatch(deleteProfileSuccess(data)); diff --git a/src/actions/refresh.js b/src/actions/refresh.js index 125c977..5a3c74f 100644 --- a/src/actions/refresh.js +++ b/src/actions/refresh.js @@ -1,5 +1,5 @@ import {createAction} from 'redux-actions'; -import axios from "axios/index"; +import * as api from '../apis'; export const FETCH_REFRESH_REQUEST = 'FETCH_REFRESH_REQUEST'; export const FETCH_REFRESH_SUCCESS = 'FETCH_REFRESH_SUCCESS'; @@ -13,7 +13,7 @@ export const fetchRefresh = () => async (dispatch) => { dispatch(fetchRefreshRequest()); try { - const {data} = await axios.get('refresh'); + const data = await api.getRefresh(); if (data.error) { return dispatch(fetchRefreshFailure(data)); diff --git a/src/apis/index.js b/src/apis/index.js index 62d4ea2..b84d843 100644 --- a/src/apis/index.js +++ b/src/apis/index.js @@ -1,14 +1,73 @@ import axios from 'axios'; +import CONSTANTS from '../constants'; -const instances = {}; +const {endpoints} = CONSTANTS; -export const createAxiosInstances = (endpoints) => { - Object.keys(endpoints).forEach((entry) => { - instances[entry] = axios.create({ - baseURL: endpoints[entry], - timeout: 30000 - }); +const axiosClient = axios.create({ + baseURL: endpoints.electron, + timeout: 30000 +}); + +/** + * Generic HTTP request function to wrap axios operations + * @param url + * @param method + * @param payload + * @param client + * @returns {Promise} + */ +const executeRequest = async ({url, method = 'get', payload = null, client = axiosClient}) => { + const {data} = await client.request({ + url, + method, + data: payload }); + + return data; +}; + +/** + * Execute GET request against the /configure endpoint + * @returns {Promise<*>} + */ +export const getConfigure = async () => { + return await executeRequest({url: 'configure'}); +}; + +/** + * Execute POST request against the /configure endpoint with payload + * @param {Object} payload + * @returns {Promise<*>} + */ +export const postConfigure = async (payload) => { + return await executeRequest({url: 'configure', method: 'post', payload}); }; -export default instances; +/** + * Execute GET request against the SPA /refresh endpoint + * @returns {Promise<*>} + */ +export const getRefresh = async () => { + // Note: this call triggers the /refresh route on the SPA to make sure that + // we get routed to the right component. + return await executeRequest({url: 'refresh', client: axios}); +}; + +/** + * Execute GET request against the SPA /logout endpoint + * @returns {Promise<*>} + */ +export const getLogout = async () => { + // Note: this call triggers the /logout route on the SPA to make sure that + // we get routed to the right component. + return await executeRequest({url: 'logout', client: axios}); +}; + +/** + * Execute DELETE request against the /profile endpoint with payload + * @param {Object} payload + * @returns {Promise<*>} + */ +export const deleteProfile = async (payload) => { + return await executeRequest({url: 'profile', method: 'delete', payload}); +}; diff --git a/src/index.js b/src/index.js index 8d74b32..de1115f 100644 --- a/src/index.js +++ b/src/index.js @@ -3,8 +3,6 @@ import {render} from 'react-dom'; import {Provider} from 'react-redux'; import {ConnectedRouter} from 'react-router-redux'; import store, {history} from './store'; -import CONSTANTS from './constants'; -import {createAxiosInstances} from './apis'; import './index.css'; import 'bootstrap/dist/css/bootstrap.min.css'; @@ -16,8 +14,6 @@ import faExclamationTriangle from '@fortawesome/fontawesome-free-solid/faExclama import App from './containers/App'; -createAxiosInstances(CONSTANTS.endpoints); - fontawesome.library.add(faCopy, faTrashAlt, faExclamationTriangle); const target = document.querySelector('#root');