0.3.45 • Published 9 months ago

ws-process v0.3.45

Weekly downloads
72
License
MIT
Repository
github
Last release
9 months ago

ws-process

Call REST API

Process management for Web Socket Client

Each process will be running parallelly.

Requests in a process will be running sequently.

import { newProcess } from "ws-process";

// Basic usage skeleton
const promise = newProcess()
  .newRequest()
  .newRequest()
  .start();

promise
  .then(result=>{})
  .catch((err)=>{})
/*
.start() returns a Promise instance.
When the all requests belong to process are finished, it resolve with following:
*/
result = {
  data: {
	  isLast: true,
	  processID,
	  jsonResponse,
	  cbData
	},
  dispatch,
  getState,
  processFinished: true,
  processID
}

Sample

// To query a production order
dispatch(
  getOrder(
    { orderID },
    {
      afterSucceed: () => {
        history.push(`/prodconfirm/order/${orderID}`);
      }
    }
  )
);

// getOrder - an action creator for the thunk which is the middleware of redux
/*
  newProcess()
    arguments:
      Process ID: String without space
      Options: {
        appIsBusy = true(default)
      }
        Options are provided to "beforeEachProcess", "afterEachProcess" which are interceptors.
*/
const getOrder = (params, callback) => {
  return (dispatch, getState) => {
    const promise = newProcess("productionOrder")
      .newRequest({
        requestHandler: reqOrder,
        params,
        responseHandler: resOrder,
        callback
      })
      .start();
  };
};
/*
  params: Provided to 'request' function
*/

const reqOrder = ({ params }) => {
  return (dispatch, getState) => {
    return {
      version: "20190625",
      action: "requestodata",
      subAction: "getProductionOrder",
      description: "Fetch production order data from ByDesign",
      httpMethod: "GET",
      headers: {}, // headers could be ignored, authentication will be added in ws-process package,
      // Attributes in 'headers' will overwrite the attributes defined in ws-process package
      url: dispatch(
        convURL.custom("productionorder/ProductionOrderCollection", {
          $expand: ["ProductionLot/ProductionLotMaterialOutput"].join(","),
          $filter: [escape("ID eq '" + params.orderID + "'")]
        })
      )
      /* convURL has following methods
        .custom() - Returns url with process.env.REACT_APP_BYD_CUSTOM_ODATA_PATH,
        .report() or .reportCC() - Returns url with process.env.REACT_APP_BYD_REPORT_ODATA_PATH,
        .reportANA() - Returns url with process.env.REACT_APP_BYD_REPORT_ODATA_PATH_ANA,
        .datasource() - Returns url with process.env.REACT_APP_BYD_DATASOURCE_ODATA_PATH;
      */
      /* Default query parameters:
        sap-language: currentUser.lang || "en"
        $format: "json",
        $inlinecount: "allpages"
      */
    };
  };
};

// resOrder - An action creator for thunk the middleware of redux
/*
  arguments:
    
    @isLast: SAP Odata would split response if the data size exceeds the limit(?), so isLast is used for determine if the response is last.
    
    @jsonResponse: Response data as json format
  
  return:
    array - [boolean,data for callback]

    If no data for callback function need to be transferred, then it could return only boolean value, not array.
    If no callback function is provided, then 'return' could be ignored.
*/
function resOrder({ isLast, jsonResponse }) {
  return (dispatch, getState) => {
    const body = jsonResponse.body;
    const results = body.d.results;
    const count = parseInt(body.d.__count);

    if (count > 0) {
      dispatch(getProductionOrder(results));
      return [true];
    } else {
      dispatch(addError(["None of order is fetched"]));
      return [false];
    }
  };
}
  • callback functions: optional

    • afterSucceed(cbData): function - optional

      If response handler returns true or true, \, then invoke afterSucceed and always.

    • afterFailed(cbData): function - optional

      If response handler returns false or true, \, then invoke afterFailed and always.

    • always(cbData): function - optional

      Whatever returned by response handler, if always callback function is provided, it will run anyway for the specific request.

    callback functions will be invoked by the result of response handler.

Call REST API

url: The URL of REST API, if no domain is included, the API Hub domain will be used.

method: The HTTP Method

description: The text represented on Loading Dialog

params: The query string parameters

data: The body of request

newProcess("get master data")
  .newRequest({
    requestHandler: () => () => {
      return {
        url: "/master",
        method: "GET",
        description: "get vendor list",
        params: {
          type: "vendor"
        }
      };
    },
    responseHandler: ({ isFirst, isLast, jsonResponse }) => {
      return dispatch => {
        const body = jsonResponse.body;

        return [false];
      };
    },
    callback: {
      afterSucceed: () => {},
      afterFailed: () => {},
      always: () => {}
    }
  })
  .start();
0.3.45

9 months ago

0.3.44

1 year ago

0.3.43

1 year ago

0.3.42

1 year ago

0.3.41

1 year ago

0.3.40

1 year ago

0.3.39

1 year ago

0.3.38

1 year ago

0.3.37

1 year ago

0.3.36

1 year ago

0.3.35

1 year ago

0.3.31

2 years ago

0.3.30

2 years ago

0.3.34

2 years ago

0.3.33

2 years ago

0.3.32

2 years ago

0.3.29

3 years ago

0.3.28

3 years ago

0.3.27

3 years ago

0.3.26

4 years ago

0.3.25

4 years ago

0.3.24

4 years ago

0.3.23

4 years ago

0.3.22

4 years ago

0.3.21

4 years ago

0.3.20

4 years ago

0.3.19

4 years ago

0.3.18

4 years ago

0.3.17

4 years ago

0.3.16

5 years ago

0.3.15

5 years ago

0.3.14

5 years ago

0.3.13

5 years ago

0.3.12

5 years ago

0.3.10

5 years ago

0.3.9

5 years ago

0.3.8

5 years ago

0.3.7

5 years ago

0.3.6

5 years ago

0.3.5

5 years ago

0.3.4

5 years ago

0.3.2

5 years ago

0.3.3

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.33

5 years ago

0.2.32

5 years ago

0.2.31

5 years ago

0.2.30

5 years ago

0.2.29

5 years ago

0.2.28

5 years ago

0.2.27

5 years ago

0.2.26

5 years ago

0.2.25

5 years ago

0.2.24

6 years ago

0.2.23

6 years ago

0.2.22

6 years ago

0.2.21

6 years ago

0.2.20

6 years ago

0.2.19

6 years ago

0.2.18

6 years ago

0.2.17

6 years ago

0.2.16

6 years ago

0.2.15

6 years ago

0.2.14

6 years ago

0.2.13

6 years ago

0.2.12

6 years ago

0.2.11

6 years ago

0.2.10

6 years ago

0.2.9

6 years ago

0.2.8

6 years ago

0.2.7

6 years ago

0.2.6

6 years ago

0.2.5

6 years ago

0.2.4

6 years ago

0.2.3

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago