Skip to content
Justin Lazaro edited this page Nov 5, 2018 · 12 revisions

ACTION

because this is for async action so we need to use a middleware for redux to handle this method / action, which is redux-observable.

How to use redux-observable with rxjs-agile-request library?

Principle:

middleware epic

 in Agile request, we standardize to have an action of loading, success, error, retry (optional), cancel (optional, recommended if the data is to large). - Documentation WIP - @chelzea

example: agile get request.

import {
  getRequest,
} from 'agileRequest';

const getBlogUrl = `${jsonplacehoder}/post321s/1`;
export const getBlogEpic = action$ => {
  const object = {
    action: action$,
    loading: ACTION.getBlogLoading,
    success: ACTION.getBlogSuccess,
    error: ACTION.getBlogError,
    cancel: TYPES.GET_BLOG_CANCEL,
    epic: TYPES.GET_BLOG_EPIC,
    url: getBlogUrl,
    retry: error => {
      return 404 === error.status;
    },
  };

  return getRequest(object);
};

simple right?


Behind the scenes what rxjs-agile-request doing,

request methods https://github.com/juztinlazaro/rxjx-agile-request/blob/master/src/agileRequest/index.js

Ajax call https://github.com/juztinlazaro/rxjx-agile-request/blob/master/src/agileRequest/ajax.js


import { of } from 'rxjs/observable/of';
import { _throw } from 'rxjs/observable/throw';

const ajaxCall = (ajaxreq, success, cancel, error, retry) => {
  if (cancel === undefined) {
    // agile request gives you a warning that cancel actions is nice to have special if your requesting a big data from api
    console.warn(
      'AgileRequest warn: its nice if you have a cancel action in your request',
    );
  }

  if (retry === undefined) {
     // agile request gives you a warning that retry actions is nice to have special if client  got lost connection or etc
    console.warn(
      'AgileRequest warn: its nice if you have a retry action in your request',
    );
  }

  let requestError;
  return ajaxreq
    .takeUntil(cancel)
    .flatMap(result => {
      return [success(result.response)];
    })
    .retryWhen(error => {
      console.log('here');
      return (
        error
          .flatMap(error => {
            requestError = error;
            if (retry !== undefined) {
              /*
            if you have error status retry
          */
              if (retry(error)) {
                return of(error.status);
              }
              return _throw(error.status);
            } else {
              /* 
            else all error will retry 
          */
              return of(error.status);
            }
          })
          // we decide to retry 5x
          .take(5)
          .concat(_throw('Sorry, there was an error (after 5 retries)'))
      );
    })
    .catch(message => {
      const err = {
        requestError,
        message,
      };
      return of(error(err));
    });
};

export default ajaxCall;

History / Inspiration

Way back in feb 2018, i just play around about this concept for my boredom, and abandon this library when its almost done. The main concept of this library is to force the developer to follow the standards, generalize and uniform the code base. Prevent issues or bad practice handling asynchronous calls. And to be lazy :).

For Multiple request, still waiting for our one of the BAE (BUILDER, AMBITIOUS, ENTHUSIASTIC) contributor @nicho.