Skip to content

Commit

Permalink
Moved API calls from actions to apis module.
Browse files Browse the repository at this point in the history
  • Loading branch information
davepgreene committed Jul 3, 2018
1 parent 12f6dd7 commit caec7c4
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 21 deletions.
6 changes: 3 additions & 3 deletions src/actions/configure.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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) {
Expand All @@ -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));
Expand Down
4 changes: 2 additions & 2 deletions src/actions/logout.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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));
Expand Down
4 changes: 2 additions & 2 deletions src/actions/profile.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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));
Expand Down
4 changes: 2 additions & 2 deletions src/actions/refresh.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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));
Expand Down
75 changes: 67 additions & 8 deletions src/apis/index.js
Original file line number Diff line number Diff line change
@@ -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<void>}
*/
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});
};
4 changes: 0 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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');
Expand Down

0 comments on commit caec7c4

Please sign in to comment.