Skip to content

Commit 1853eda

Browse files
authored
Merge pull request #332 from bertho-zero/axios
Axios
2 parents dad3406 + 2d56780 commit 1853eda

File tree

11 files changed

+63
-90
lines changed

11 files changed

+63
-90
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: 2 additions & 1 deletion
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",
@@ -149,6 +150,7 @@
149150
"feathers-rest": "^1.7.3",
150151
"feathers-socketio": "^2.0.0",
151152
"http-proxy": "^1.16.2",
153+
"is-promise": "^2.1.0",
152154
"js-cookie": "^2.1.3",
153155
"localforage": "^1.4.3",
154156
"lru-memoize": "^1.0.1",
@@ -176,7 +178,6 @@
176178
"serve-favicon": "^2.3.2",
177179
"socket.io": "^2.0.1",
178180
"socket.io-client": "^2.0.1",
179-
"superagent": "^3.4.4",
180181
"verror": "^1.10.0"
181182
},
182183
"devDependencies": {

src/app.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import rest from 'feathers-rest/client';
66
import socketio from 'feathers-socketio/client';
77
import authentication from 'feathers-authentication-client';
88
import io from 'socket.io-client';
9-
import superagent from 'superagent';
9+
import axios from 'axios';
1010
import config from './config';
1111

1212
const storage = __SERVER__ ? null : require('localforage');
@@ -23,17 +23,19 @@ export const socket = io('', { path: host('/ws'), autoConnect: false });
2323

2424
export function createApp(req) {
2525
if (req === 'rest') {
26-
return configureApp(rest(host('/api')).superagent(superagent));
26+
return configureApp(rest(host('/api')).axios(axios));
2727
}
2828

2929
if (__SERVER__ && req) {
3030
const app = configureApp(
31-
rest(host('/api')).superagent(superagent, {
32-
headers: {
33-
Cookie: req.get('cookie'),
34-
authorization: req.header('authorization') || ''
35-
}
36-
})
31+
rest(host('/api')).axios(
32+
axios.create({
33+
headers: {
34+
Cookie: req.get('cookie'),
35+
authorization: req.header('authorization') || ''
36+
}
37+
})
38+
)
3739
);
3840

3941
const accessToken = req.header('authorization') || (req.cookies && req.cookies['feathers-jwt']);

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: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
let token;
10+
11+
instance.setJwtToken = newToken => {
12+
token = newToken;
13+
};
14+
15+
instance.interceptors.request.use(
16+
conf => {
17+
if (__SERVER__) {
18+
if (req.header('cookie')) {
19+
conf.headers.Cookie = req.header('cookie');
20+
}
21+
if (req.header('authorization')) {
22+
conf.headers.authorization = token || req.header('authorization') || '';
23+
}
24+
}
25+
return conf;
26+
},
27+
error => Promise.reject(error)
28+
);
29+
30+
instance.interceptors.response.use(
31+
response => response.data,
32+
error => Promise.reject(error.response ? error.response.data : error)
33+
);
34+
35+
return instance;
36+
}

src/redux/modules/survey.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ export default function reducer(state = initialState /* , action = {} */) {
1111
export function isValidEmail(data) {
1212
return {
1313
types: [IS_VALID, IS_VALID_SUCCESS, IS_VALID_FAIL],
14-
promise: ({ client }) =>
15-
client.post('/survey/isValid', {
16-
data
17-
})
14+
promise: ({ client }) => client.post('/survey/isValid', data)
1815
};
1916
}

src/redux/modules/widgets.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,7 @@ export function save(widget) {
100100
return {
101101
types: [SAVE, SAVE_SUCCESS, SAVE_FAIL],
102102
id: widget.id,
103-
promise: ({ client }) =>
104-
client.post('/widget/update', {
105-
data: widget
106-
})
103+
promise: ({ client }) => client.post('/widget/update', widget)
107104
};
108105
}
109106

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)