In my app, I need to call several REST API endpoints:
// The UI Class class LoginForm extends Component { handleSubmit(){ store.dispatch(login(username, password)); } } // An action function login(username, password){ return dispatch => { fetch(LOGIN_API, {...}) .then(response => { if (response.status >= 200 && response.status < 300){ // success } else { // fail } }) } } The gist is above and easy to understand. User triggers an action, an ajax call to the corresponding endpoint is made.
As I am adding more and more API endpoints, I end up with a bunch of functions similar to the skeleton of the login function above.
How should I structure my code in such a way that I don't repeat myself with duplicate ajax functions?
Thanks!