How to use redux-api-middleware - 10 common examples

To help you get started, we’ve selected a few redux-api-middleware examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github cern-phone-apps / desktop-phone-app / webapp / src / middleware.js View on Github external
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()
          );
github cern-phone-apps / desktop-phone-app / mobile / middleware.js View on Github external
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()
          );
github cern-phone-apps / desktop-phone-app / src / middleware.js View on Github external
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 {
github viewflow / cookbook / _articles / redux_jwt_auth / frontend / src / middleware.js View on Github external
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);
    }
github amaurymartiny / timed / app / client / src / middleware / api.js View on Github external
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,
github Patreon / nion / actions / request-types.js View on Github external
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)
                }
            })
        }
    }, {
github zanata / zanata-platform / server / zanata-frontend / src / app / actions / version-actions.js View on Github external
payload: (_action, _state, res) => {
        // @ts-ignore null
        return getJSON(res).then((json) => json.results)
      }
    },
github tylercrosse / gitter-clone / src / common / actions / index.js View on Github external
payload: /* istanbul ignore next */ (action, state, res) => {
          return getJSON(res)
            .then((json) => normalize(json, Schemas.CONVO_ARRAY));
        }
      },
github zanata / zanata-platform / server / zanata-frontend / src / app / editor / watchers / phrase-list.js View on Github external
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 }
github viewflow / cookbook / _articles / redux_jwt_auth / frontend / src / middleware.js View on Github external
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)) {