Last active
November 28, 2018 08:58
-
-
Save ger86/8f3cb21d1a07979a668883644910794e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const createRequest = ( | |
{ requestId, requestParams }, | |
needsAuthentication = false | |
) => async (dispatch, getState) => { | |
const cancelTokenSource = request.getCancelTokenSource(); | |
dispatch(requestStartedAction(requestId, cancelTokenSource)); | |
try { | |
const res = await request.send({ | |
...requestParams, | |
cancelToken: cancelTokenSource.token | |
}); | |
dispatch(requestSucceededAction(requestId)); | |
return res; | |
} catch (error) { | |
if (error.message === 'canceled') { | |
throw error; | |
} | |
if (error.response && error.response.status === 401) { | |
if ( | |
needsAuthentication && | |
error.response.data.message === 'Expired JWT Token' | |
) { | |
const renewingWasSuccessfull = await dispatch( | |
renewThunk(getState().user.refreshToken) | |
); | |
if (renewingWasSuccessfull) { | |
return dispatch( | |
createRequest({ requestId, requestParams }, needsAuthentication) | |
); | |
} | |
} | |
} | |
dispatch(requestFailedAction(requestId, error)); | |
throw error; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment