redux-thunk-request
v1.0.5
Published
Redux-thunk extra request
Downloads
7
Maintainers
Readme
redux-thunk-request
Redux-thunk extra request
function request(
url : String,
options : Object<{
method : String<['get', 'post', 'delete', 'put']>,
accept : String<default = 'application/json'>,
query : Object,
send : Object,
contentType : String<default = 'application/json'>,
accessToken : String,
attach : Array,
fields : Array<[name, value]>
}>
)
Example:
import ReduxThunkRequest from 'redux-thunk-request';
import {createStore, applyMiddleware} from 'redux';
import thunk from 'redux-thunk';
function defaultErrorHandler(err, res) {
if(res.unauthorized) {
unauthorized = true;
}
}
function getAccessToken() {
Cookies.get('access_token');
}
const extraRequest = new ReduxThunkRequest({
baseUrl: 'http://localhost:8080',
defaultErrorHandler,
getAccessToken
});
const {request} = extraRequest;
const store = createStore(reducer, applyMiddleware(thunk.withExtraArgument({request})));
store.dispatch((dispatch, getState, {request}) => {
const req = request('/', {waitServer : true}).then(({value}) => dispatch({
type : UPDATE_VALUE,
value
}));
assert(true === extraRequest.isWaited());
return req;
}).then(({value})=>{
assert(5 === value);
assert(5 === store.getState().value);
assert(false === apiRequest.isWaited());
});