Skip to content

Commit 7f438e1

Browse files
committed
fix: switch to axios and ensure production builds work
1 parent 32212a1 commit 7f438e1

File tree

5 files changed

+50
-16
lines changed

5 files changed

+50
-16
lines changed

common/js/actions/todos.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
ADD_TODO, REMOVE_TODO, TOGGLE_TODO,
33
FETCH_TODOS_REQUEST, FETCH_TODOS_SUCCESS, FETCH_TODOS_FAILURE
44
} from 'constants/index';
5-
import { fetch } from 'helper/api';
5+
import api from 'helper/api';
66
import generateActionCreator from 'helper/generateActionCreator';
77

88
export const addTodo = generateActionCreator(ADD_TODO, 'text');
@@ -14,15 +14,19 @@ export const fetchTodosSuccess = generateActionCreator(FETCH_TODOS_SUCCESS, 'tod
1414
export const fetchTodosFailure = generateActionCreator(FETCH_TODOS_FAILURE, 'error');
1515

1616
export const fetchTodos = () => {
17-
return async (dispatch) => {
17+
return (dispatch) => {
1818
dispatch(fetchTodosRequest());
1919

20-
try {
21-
const response = await fetch('/api/todos', { method: 'GET' });
22-
const todos = await response.json();
23-
dispatch(fetchTodosSuccess(todos));
24-
} catch (e) {
25-
dispatch(fetchTodosFailure(e.message));
26-
}
20+
return api.get('/api/todos')
21+
.then(todos => {
22+
dispatch(fetchTodosSuccess(todos));
23+
24+
return Promise.resolve(todos);
25+
})
26+
.catch(error => {
27+
dispatch(fetchTodosFailure(error));
28+
29+
return Promise.reject(error);
30+
});
2731
};
2832
};

common/js/helper/api.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
import isomorphicFetch from 'isomorphic-fetch';
1+
import axios from 'axios';
22

3-
const apiUrl = process.env.APPLICATION_BASE_URL || '';
3+
const headers = { 'Content-Type': 'application/json' };
4+
const baseURL = process.env.APPLICATION_BASE_URL || '';
5+
const api = axios.create({ baseURL, headers, timeout: 200000 });
46

5-
// Overrides the fetch() method to add the base API url to the front.
6-
export const fetch = (url, ...rest) => isomorphicFetch(apiUrl + url, ...rest);
7+
api.interceptors.response.use(
8+
(response) => Promise.resolve(response.data) ,
9+
(err) => Promise.reject(err.response.data)
10+
);
11+
12+
module.exports = api;

package-lock.json

Lines changed: 23 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "universal-react-redux",
33
"description": "A universal starter kit built with ES2015, react, react-router and redux. Server rendering with react and express. Bundled with Webpack with HMR transforms and css-modules support.",
4-
"version": "4.1.1",
4+
"version": "5.0.0",
55
"license": "MIT",
66
"main": "client/index.js",
77
"repository": {
@@ -129,6 +129,7 @@
129129
"dependencies": {
130130
"app-module-path": "^2.2.0",
131131
"autoprefixer": "^7.2.1",
132+
"axios": "^0.17.1",
132133
"babel": "^6.23.0",
133134
"babel-cli": "^6.26.0",
134135
"babel-core": "^6.26.0",
@@ -193,6 +194,7 @@
193194
"webpack": "^3.10.0",
194195
"webpack-bundle-analyzer": "^2.9.1",
195196
"webpack-isomorphic-tools": "^3.0.5",
197+
"webpack-node-externals": "^1.6.0",
196198
"yn": "^2.0.0"
197199
}
198200
}

webpack/constants.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,6 @@ export const SERVER_RESOLVE_PATHS = {
6565
auth: 'server/auth',
6666
models: 'server/models',
6767
services: 'server/services',
68-
constants: 'server/constants',
68+
constants: 'common/js/constants',
6969
lib: 'server/lib'
7070
};

0 commit comments

Comments
 (0)