Skip to content

Commit

Permalink
Merge pull request #80 from apapacy/promised-middleware
Browse files Browse the repository at this point in the history
promised middlevare released
  • Loading branch information
wellyshen authored Jul 22, 2017
2 parents 64294d3 + ddd21e0 commit a68d1c9
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 26 deletions.
3 changes: 2 additions & 1 deletion src/containers/Home/__tests__/action.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import {
USERS_FAILURE,
USERS_SUCCESS,
} from '../action';
import promisedMiddleware from '../../../redux/promisedMiddleware';

const host = 'http://localhost';

axios.defaults.host = host;
axios.defaults.adapter = httpAdapter;

const mockStore = configureMockStore([thunk]);
const mockStore = configureMockStore([thunk, promisedMiddleware(axios)]);

describe('fetch users data', () => {
const response = [{ id: '1', name: 'Welly' }];
Expand Down
16 changes: 4 additions & 12 deletions src/containers/Home/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,10 @@ export const USERS_SUCCESS = 'USERS_SUCCESS';
export const API_URL = 'https://jsonplaceholder.typicode.com/users';

// Export this for unit testing more easily
export const fetchUsers = (axios: any, URL: string = API_URL): ThunkAction =>
(dispatch: Dispatch) => {
dispatch({ type: USERS_REQUESTING });

return axios.get(URL)
.then((res) => {
dispatch({ type: USERS_SUCCESS, data: res.data });
})
.catch((err) => {
dispatch({ type: USERS_FAILURE, err });
});
};
export const fetchUsers = (axios: any, URL: string = API_URL): ThunkAction => ({
promise: client => client.get(URL).then(value => ({ data: value.data })),
events: [USERS_REQUESTING, USERS_SUCCESS, USERS_FAILURE],
});

// Preventing dobule fetching data
/* istanbul ignore next */
Expand Down
3 changes: 2 additions & 1 deletion src/containers/UserInfo/__tests__/action.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import {
USER_FAILURE,
USER_SUCCESS,
} from '../action';
import promisedMiddleware from '../../../redux/promisedMiddleware';

const host = 'http://localhost';

axios.defaults.host = host;
axios.defaults.adapter = httpAdapter;

const mockStore = configureMockStore([thunk]);
const mockStore = configureMockStore([thunk, promisedMiddleware(axios)]);

describe('fetch user data', () => {
const userId = 'test';
Expand Down
17 changes: 5 additions & 12 deletions src/containers/UserInfo/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,11 @@ export const USER_SUCCESS = 'USER_SUCCESS';
export const API_URL = 'https://jsonplaceholder.typicode.com/users';

// Export this for unit testing more easily
export const fetchUser = (userId: string, axios: any, URL: string = API_URL): ThunkAction =>
(dispatch: Dispatch) => {
dispatch({ type: USER_REQUESTING, userId });

return axios.get(`${URL}/${userId}`)
.then((res) => {
dispatch({ type: USER_SUCCESS, userId, data: res.data });
})
.catch((err) => {
dispatch({ type: USER_FAILURE, userId, err });
});
};
export const fetchUser = (userId: string, axios: any, URL: string = API_URL): ThunkAction => ({
promise: client => client.get(`${URL}/${userId}`).then(value => ({ data: value.data })),
events: [USER_REQUESTING, USER_SUCCESS, USER_FAILURE],
userId,
});

// Using for preventing dobule fetching data
/* istanbul ignore next */
Expand Down
17 changes: 17 additions & 0 deletions src/redux/promisedMiddleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export default (...args) => ({ dispatch, getState }) => next => (action) => {
if (typeof action === 'function') {
return action(dispatch, getState);
}
const { promise, events, ...rest } = action;
if (!promise) {
return next(action);
}
const [REQUEST, SUCCESS, FAILURE] = events;
next({ ...rest, type: REQUEST });
return promise(...args).then(
value => next({ ...rest, ...value, type: SUCCESS }),
err => next({ ...rest, err, type: FAILURE }),
).catch((err) => {
next({ ...rest, err, type: FAILURE });
});
};
2 changes: 2 additions & 0 deletions src/redux/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import chalk from 'chalk';

import type { Store } from '../types';
import rootReducer from './reducers';
import promisedMiddleware from './promisedMiddleware';

export default (history: Object, initialState: Object = {}): Store => {
const middlewares = [
thunk.withExtraArgument(axios),
routerMiddleware(history),
promisedMiddleware(axios),
];

const enhancers = [
Expand Down

0 comments on commit a68d1c9

Please sign in to comment.