Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'redux-api-middleware' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
return next => action => {
const nextCheckPostponed = checkNextAction(
next,
postponedRSAAs,
rsaaMiddleware
);
if (isRSAA(action)) {
const state = getState();
const refreshToken = JwtTokenHandlerDesktop.getRefreshToken(state);
// If it is a LOGIN_REQUEST or LOGOUT_REQUEST we don't try to refresh the token
if (
action[RSAA].types.indexOf(authActions.LOGOUT_REQUEST) > -1 ||
action[RSAA].types.indexOf(authActions.LOGIN_REQUEST) > -1
) {
return rsaaMiddleware(next)(action);
}
if (refreshToken && JwtTokenHandlerDesktop.isAccessTokenExpired()) {
postponedRSAAs.push(action);
if (postponedRSAAs.length > 0) {
return rsaaMiddleware(nextCheckPostponed)(
dialBackendApi().refreshAccessToken()
);
return next => action => {
const nextCheckPostponed = checkNextAction(
next,
postponedRSAAs,
rsaaMiddleware
);
if (isRSAA(action)) {
const state = getState();
const refreshToken = JwtTokenHandlerMobile.getRefreshToken(state);
// If it is a LOGIN_REQUEST or LOGOUT_REQUEST we don't try to refresh the token
if (
action[RSAA].types.indexOf(authActions.LOGOUT_REQUEST) > -1 ||
action[RSAA].types.indexOf(authActions.LOGIN_REQUEST) > -1
) {
return rsaaMiddleware(next)(action);
}
if (refreshToken && JwtTokenHandlerMobile.isAccessTokenExpired(state)) {
postponedRSAAs.push(action);
if (postponedRSAAs.length > 0) {
return rsaaMiddleware(nextCheckPostponed)(
dialBackendApi().refreshAccessToken()
);
return next => action => {
const nextCheckPostponed = checkNextAction(
next,
postponedRSAAs,
rsaaMiddleware
);
if (isRSAA(action)) {
const refreshToken = getRefreshToken();
// If it is a LOGIN_REQUEST or LOGOUT_REQUEST we don't try to refresh the token
if (
action[RSAA].types.indexOf(LOGOUT_REQUEST) > -1 ||
action[RSAA].types.indexOf(LOGIN_REQUEST) > -1
) {
return rsaaMiddleware(next)(action);
}
if (refreshToken && isAccessTokenExpired()) {
logMessage("Access token is expired but we have refresh token");
postponedRSAAs.push(action);
logMessage("postponed RSAAs: ", postponedRSAAs);
if (postponedRSAAs.length > 0) {
return rsaaMiddleware(nextCheckPostponed)(refreshAccessToken());
} else {
return (next) => (action) => {
const nextCheckPostoned = (nextAction) => {
// Run postponed actions after token refresh
if (nextAction.type === TOKEN_RECEIVED) {
next(nextAction);
postponedRSAAs.forEach((postponed) => {
rsaaMiddleware(next)(postponed)
})
postponedRSAAs = []
} else {
next(nextAction)
}
}
if(isRSAA(action)) {
const state = getState(),
token = refreshToken(state)
if(token && isAccessTokenExpired(state)) {
postponedRSAAs.push(action)
if(postponedRSAAs.length === 1) {
return rsaaMiddleware(nextCheckPostoned)(refreshAccessToken(token))
} else {
return
}
}
return rsaaMiddleware(next)(action);
}
return next(action);
}
return next => (action) => {
if (!isRSAA(action)) {
return next(action)
}
// abort if not authenticated
if (!store.getState().auth.isAuthenticated) {
return undefined
}
const callApi = action[CALL_API]
// prepend API_ROOT to all endpoints
callApi.endpoint = process.env.API_ROOT + callApi.endpoint
// add Authorization header with token
callApi.headers = { 'Content-Type': 'application/json', Authorization: `Bearer ${AuthService.getToken()}` }
// normalize data on SUCCESS
const type = typeof callApi.types[1] === 'string' ? callApi.types[1] : callApi.types[1].type // 0 is REQUEST, 1 is SUCCESS and 2 is FAILURE
callApi.types[1] = {
type,
payload: (action, state, res) => {
return getJSON(res).then((json) => {
// Resolve the passed in promise, if supplied
if (get(promiseHandler, 'promise._deferreds.length')) {
// We need to ensure that the promise resolves AFTER the redux store has been
// updated, so pass it off to the next tick
const shouldResolve = promiseHandler && promiseHandler.resolve
shouldResolve && setImmediate(() => promiseHandler.resolve())
}
return {
requestType: requestType,
responseData: dataParser(json)
}
})
}
}, {
payload: (_action, _state, res) => {
// @ts-ignore null
return getJSON(res).then((json) => json.results)
}
},
payload: /* istanbul ignore next */ (action, state, res) => {
return getJSON(res)
.then((json) => normalize(json, Schemas.CONVO_ARRAY));
}
},
payload: (_action, _state, res) =>
// @ts-ignore
getJSON(res).then(phraseList => ({
docId,
// @ts-ignore any
phraseList: phraseList.map(phrase => ({
...phrase,
status: transUnitStatusToPhraseStatus(phrase.status)
}))
})),
meta: { filter: filtered }
return ({ dispatch, getState }) => {
const rsaaMiddleware = apiMiddleware({dispatch, getState})
return (next) => (action) => {
const nextCheckPostoned = (nextAction) => {
// Run postponed actions after token refresh
if (nextAction.type === TOKEN_RECEIVED) {
next(nextAction);
postponedRSAAs.forEach((postponed) => {
rsaaMiddleware(next)(postponed)
})
postponedRSAAs = []
} else {
next(nextAction)
}
}
if(isRSAA(action)) {