oute-services-utility-sdk
v0.0.47
Published
- Initialization
Downloads
1,516
Readme
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)