0.0.64 • Published 1 year ago

oute-services-utility-sdk v0.0.64

Weekly downloads
-
License
-
Repository
-
Last release
1 year ago

This module expose helper functions

  • Initialization
  var utility = require("oute-services-utility-sdk")
  • Validate and parse string object
  utility.validateAndParseObject("{}")
  • Execute API
  var options = {
    method: 'GET',
    url: 'http://localhost:3101/service/v0/variable/get/transformed',
    headers: {
      'token': "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoidGVzdEBnb2ZvLmFwcCIsIm9yZ0lkcyI6WyJaekJmSk1wZlEiXSwiaWF0IjoxNjgyMjQ4MjcwLCJleHAiOjMzMjA4MjkwNjcwfQ.OADEKdh1IaABuKCEwFgskapkNVrg_IIjzj1cd0HoGdc",
      'Content-Type': 'application/json'
    },
    path_variable: {p: "123"},
    query_params: {asset_id: "asset"},
    convert_to_curl: false, //this will convert the request to curl and respond with curl
    timeout: 700, //This will be in milliseconds
    response_type: "stream", //possiblie value ["stream", null], This type is used for getting the response in specified format.
    body_info: {
      type: "", //form-data, x-www-form-urlencoded, binary, raw, none or ""
      sub_type: "" //if raw => json, text, html, xml, javascript
    }
  }
  await utility.executeAPI(options)
  • Compare array
  src_array = [1,2]
  comparing_array = [2,1]
  compare_type= "all" 
  // all => all element of both array should be same
  // any_one => any one element of comparing array should be in src array
  utility.compareArray(src_array, comparing_array, compare_type)
  • Query array
  src_array = [{a: "test", b: "mtro"},{a: "mets"}, {a: "test1", b: "mtro"}, {a: "test1", b: "wert"}]
  search_options = {a: /test/, b: /mt/}
  utility.queryArray(src_array, search_options)
  • Invoke the function
  //Note if trying to call the callback function then ensure the last args should be callback function
  //Callback function assumed as (error, result) => {}
  fn = (a)=>
    new Promise((resolve, reject) =>
      return reject(a)
    )
  func_itentity = "promise"
  await utility.invokeFunction(fn, func_itentity, {a: 1})
  • Function call with retry and delay
  max_retry = 1
  retry_delay_ms = 1000
  fn = (a)=>
    new Promise((resolve, reject) =>
      return reject(a)
    )
  fn_type = "promise"
  await utility.retry(max_retry, retry_delay_ms, fn, fn_type, {a: 1})
  • sleep function
  sleep_ms = 500
  await utility.sleep(sleep_ms)
  • Parse cmd args
  utility.parseArgs(process.argv)

Transformations

  • json to schema
  object = { orderId : "123", items : [ {medicineId : "2", qty : "3"},{ankit : "ankit"} ], address : { line1 :  {medicineId : "2", qty : "3"}, line2 : 500033 }}
  schema_config = {}
  utility.jsonToSchema(object, schema_config)
  • schema to json
  schema_config = {"schema":[{"type":"string","key":"name","sample_value":"Asd","path":["name"],"pathStr":"name"},{"type":"string","key":"orderId","sample_value":"123","path":["orderId"],"pathStr":"orderId"},{"type":"object","key":"items","schema":[{"type":"array","key":"items2","schema":[{"type":"string","key":"medicineId","sample_value":"2","path":["items","items2","medicineId"],"pathStr":"{items}.[items2].medicineId"},{"type":"string","key":"qty","sample_value":"3","path":["items","items2","qty"],"pathStr":"{items}.[items2].qty"},{"type":"string","key":"ankit","sample_value":"ankit","path":["items","items2","ankit"],"pathStr":"{items}.[items2].ankit"}],"path":["items"],"pathStr":"{items}.items2"},{"type":"string","key":"medicineId","sample_value":"2","path":["items","medicineId"],"pathStr":"{items}.medicineId"},{"type":"string","key":"qty","sample_value":"3","path":["items","qty"],"pathStr":"{items}.qty"}],"path":[],"pathStr":"items"}],"path":[],"keys":["name","orderId","items","{items}.items2","{items}.[items2].medicineId","{items}.[items2].qty","{items}.[items2].ankit","{items}.medicineId","{items}.qty"],"flattened_schema":[{"type":"string","key":"name","sample_value":"Asd","path":["name"],"pathStr":"name"},{"type":"string","key":"orderId","sample_value":"123","path":["orderId"],"pathStr":"orderId"},{"type":"object","key":"items","path":["items"],"pathStr":"{items}"},{"type":"array","key":"items2","path":["items","items2"],"pathStr":"{items}.items2"},{"type":"string","key":"medicineId","sample_value":"2","path":["items","items2","medicineId"],"pathStr":"{items}.[items2].medicineId"},{"type":"string","key":"qty","sample_value":"3","path":["items","items2","qty"],"pathStr":"{items}.[items2].qty"},{"type":"string","key":"ankit","sample_value":"ankit","path":["items","items2","ankit"],"pathStr":"{items}.[items2].ankit"},{"type":"string","key":"medicineId","sample_value":"2","path":["items","medicineId"],"pathStr":"{items}.medicineId"},{"type":"string","key":"qty","sample_value":"3","path":["items","qty"],"pathStr":"{items}.qty"}]}
  utility.schemaToJson(schema_config)
  • get default value and type for key
  path_str = "{a}.{b}.c"
  key_index = 1
  key_separator = "."
  utility.getDefaultValueAndTypeForKey(path_str, key_index, key_separator)
  • Check, parse and format the date
  //This function usasage the dayjs lib to parse and validate please check the doc for more info
  [docs](https://day.js.org/docs/en/installation/installation)
  date = "2024-03-19T07:38:01.172Z"
  src_format = undefined //Date original format
  out_format = "YYYY-MM-DD HH:mm:ss" //Format for the ouput date string
  tz_string = undefined //timezone string to parse basis of timezone
  utility.processDate(date, src_format, out_format, tz_string)
  • Clean the object based of conditions
  inputs = [] || {}
  filterFn = (value) => return (value != "") //default is to filter the undefined keys
  utility_instance.cleanObjectBy(inputs, filterFn)
  • Check if value is empty
  value = {} //{}, [], "", null, undefinded returns true else false
  utility.isEmpty(value)
  • Check if the value is null or empty
  value = {} //"", null, undefinded returns true else false
  utility.isEmptyOrNull(value)
  • Json config to curl
  value = { method: 'POST', url: 'google.com/service/v0/variable/get/transformed?asset_id=asset', headers: { 'Content-Type': 'application/json' }, body: { a: 'assa' } }
  utility.jsonToCurl(value)
  • Curl to json
  value = "curl -L -X POST google.com/service/v0/variable/get/transformed?asset_id=asset -H 'Content-Type: application/x-www-form-urlencoded' -F 'a=\"assa\"'"
  utility.curlToJson(value)
  • Get the date instance
  let date = "2024-03-19T07:38:01.172Z";
  let src_format = undefined;
  let out_format = "YYYY-MM-DD HH:mm:ss";
  let tz_string = undefined;
  let date_instance = utility.getDateInstance();
  console.log("RESULT", date_instance(date, src_format).tz(tz_string).format(out_format));
  • Update the request based on the config
  let date = "2024-03-19T07:38:01.172Z";
  let src_format = undefined;
  let out_format = "YYYY-MM-DD HH:mm:ss";
  let tz_string = undefined;
  let date_instance = utility.getDateInstance();
  console.log("RESULT", date_instance(date, src_format).tz(tz_string).format(out_format));
  • Get the UUID instance
  let uuid_instance = utility.getUuidInstance();
  console.log("RESULT", uuid_instance.v4());
  • Convert db type
  let raw_type = "varchar";
  let result = utility.convertDbType(raw_type);
  console.log("RESULT", result);
0.0.62

1 year ago

0.0.63

1 year ago

0.0.64

1 year ago

0.0.60

1 year ago

0.0.61

1 year ago

0.0.59

1 year ago

0.0.51

1 year ago

0.0.52

1 year ago

0.0.53

1 year ago

0.0.54

1 year ago

0.0.55

1 year ago

0.0.56

1 year ago

0.0.57

1 year ago

0.0.58

1 year ago

0.0.48

1 year ago

0.0.49

1 year ago

0.0.46

1 year ago

0.0.47

1 year ago

0.0.45

1 year ago

0.0.44

1 year ago

0.0.42

1 year ago

0.0.43

1 year ago

0.0.41

1 year ago

0.0.40

1 year ago

0.0.37

1 year ago

0.0.38

1 year ago

0.0.39

1 year ago

0.0.36

1 year ago

0.0.34

1 year ago

0.0.35

1 year ago

0.0.33

1 year ago

0.0.32

1 year ago

0.0.30

1 year ago

0.0.31

1 year ago

0.0.28

2 years ago

0.0.29

2 years ago

0.0.27

2 years ago

0.0.26

2 years ago

0.0.25

2 years ago

0.0.24

2 years ago

0.0.20

2 years ago

0.0.21

2 years ago

0.0.22

2 years ago

0.0.23

2 years ago

0.0.18

2 years ago

0.0.19

2 years ago

0.0.16

2 years ago

0.0.17

2 years ago

0.0.15

2 years ago

0.0.14

2 years ago

0.0.13

2 years ago

0.0.12

2 years ago

0.0.11

2 years ago

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago