Skip to content

Commit 27b90d7

Browse files
committed
Merge branch 'master' of github.com:bertho-zero/react-redux-universal-hot-example into axios
2 parents 8090172 + dad3406 commit 27b90d7

File tree

7 files changed

+41
-71
lines changed

7 files changed

+41
-71
lines changed

docs/ApiConfig.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ First things first, you need to add `APIHOST` settings in `package.json`. If you
88

99
If the port you use differs between your dev & prod API hosts, you may want to get rid of the `APIPORT` setting, including it right in `APIHOST`. Same with the protocol – if you use HTTP in dev but HTTPS in prod, you may want to include the protocol right in `APIHOST`, and then get rid of the explicit `"http://"` found in the next section.
1010

11-
## Update `ApiClient`
11+
## Update `apiClient`
1212

13-
Open up `src/helpers/ApiClient.js`. You'll see this line:
13+
Open up `src/helpers/apiClient.js`. You'll see this line:
1414

1515
``` javascript
16-
if (__SERVER__) {
17-
// Prepend host and port of the API server to the path.
18-
return 'http://' + config.apiHost + adjustedPath;
19-
}
16+
const instance = axios.create({
17+
baseURL: __SERVER__ ? `http://${config.apiHost}:${config.apiPort}` : '/api'
18+
});
2019
```
2120

2221
If you added `http://` or `https://` to your APIHOST setting, then you need to remove it here.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
},
120120
"dependencies": {
121121
"async": "^2.1.4",
122+
"axios": "^0.16.2",
122123
"babel-core": "^6.26.0",
123124
"babel-plugin-add-module-exports": "^0.2.1",
124125
"babel-plugin-transform-decorators-legacy": "^1.3.4",

src/client.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import localForage from 'localforage';
1515
import { socket, createApp } from 'app';
1616
import { Provider } from 'components';
1717
import createStore from './redux/create';
18-
import ApiClient from './helpers/ApiClient';
18+
import apiClient from './helpers/apiClient';
1919
import getRoutes from './routes';
2020
import isOnline from './utils/isOnline';
2121

@@ -24,7 +24,7 @@ const offlinePersistConfig = {
2424
whitelist: ['auth', 'info', 'chat']
2525
};
2626

27-
const client = new ApiClient();
27+
const client = apiClient();
2828
const app = createApp();
2929
const restApp = createApp('rest');
3030
const dest = document.getElementById('content');

src/components/__tests__/InfoBar-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import { InfoBar } from 'components';
99
import { Provider } from 'react-redux';
1010
import { browserHistory } from 'react-router';
1111
import createStore from 'redux/create';
12-
import ApiClient from 'helpers/ApiClient';
12+
import apiClient from 'helpers/apiClient';
1313

14-
const client = new ApiClient();
14+
const client = apiClient();
1515

1616
describe('InfoBar', () => {
1717
const mockStore = {

src/helpers/ApiClient.js

Lines changed: 0 additions & 59 deletions
This file was deleted.

src/helpers/apiClient.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import axios from 'axios';
2+
import config from '../config';
3+
4+
export default function apiClient(req) {
5+
const instance = axios.create({
6+
baseURL: __SERVER__ ? `http://${config.apiHost}:${config.apiPort}` : '/api'
7+
});
8+
9+
instance.setJwtToken = token => {
10+
instance.defaults.headers.common.Authorization = token;
11+
};
12+
13+
instance.interceptors.request.use(
14+
conf => {
15+
if (__SERVER__ && req.get('cookie')) {
16+
conf.headers.Cookie = req.get('cookie');
17+
}
18+
return conf;
19+
},
20+
error => Promise.reject(error)
21+
);
22+
23+
instance.interceptors.response.use(
24+
response => response.data,
25+
error => Promise.reject(error.response ? error.response.data : error)
26+
);
27+
28+
return instance;
29+
}

src/server.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import createHistory from 'react-router/lib/createMemoryHistory';
1616
import { Provider } from 'components';
1717
import config from 'config';
1818
import createStore from 'redux/create';
19-
import ApiClient from 'helpers/ApiClient';
19+
import apiClient from 'helpers/apiClient';
2020
import Html from 'helpers/Html';
2121
import getRoutes from 'routes';
2222
import { createApp } from 'app';
@@ -82,7 +82,7 @@ app.use((req, res) => {
8282
webpackIsomorphicTools.refresh();
8383
}
8484
const providers = {
85-
client: new ApiClient(req),
85+
client: apiClient(req),
8686
app: createApp(req),
8787
restApp: createApp(req)
8888
};

0 commit comments

Comments
 (0)