IKOA Business Opportunity Core API
Utility functions for basic development. This library is part of IKOA Business Opportunity Microservices Infraestructure.
Installation
npm install @ikoabo/core
Predefined constants
Package include a set of predefined constants to be used inside backend/frontend development.
It includes constants to prefeined object status, prefined general errors, logs level, and HTTP status responses.
import { LOG_LEVEL, SERVER_STATUS, SERVER_ERRORS, HTTP_STATUS } from "@ikoabo/core";
Using Logger
Logger is an small wrapper of [winston
][winston] logger. It only hande logs to the console
output and must be configured on server initialization. Logger support the same log levels of
[winston
][winston].
import { Logger, LOG_LEVEL } from "@ikoabo/core";
Logger.setLogLevel(LOG_LEVEL.DEBUG);
const _logger1 = new Logger("MiComponent");
const _logger2 = new Logger("OtherComponent");
_logger1.error("Error from one component", {
code: 2,
msg: "Invalid call"
});
_logger2.debug("Debug from another component", {
field: "social",
value: 10
});
Using Arrays utilities functions
Arrays implements functions to improve array data manipulation. it implements functions to ensure array initialization with include/exclude values, array sort, binary search and multiple arrays intersection.
import { Arrays } from "@ikoabo/core";
let arr1 = [1, 2, 3, 5, 7];
let arrInclude = [3, 15, 6];
let arrExclude = [2, 5];
let newArr = Arrays.initialize < number > (arr1, arrInclude, arrExclude);
console.log(newArr);
Arrays.sort < number > newArr;
console.log(Arrays.search(newArr, 7));
let intArr = Arrays.intersect < number > (newArr, arr1, arrInclude);
console.log(intArr);
Using Objects utilities functions
Objects utilities functions allow to fetch object properties and set
a default value if any path don't exists.
import { Objects } from "@ikoabo/core";
let obj = {
alfa: {
profiles: [
{
name: "Jhon",
age: 25
}
]
}
};
console.log(Objects.get(obj, "alfa.profiles.0.name", "no-name"));
if (!Objects.get(obj, "alfa.profiles.0.social.facebook")) {
console.log("Facebook not configured");
}
Also functions allow to set an object value following the geiven path.
If any elements inside path don't exists then it's created.
import { Objects } from "@ikoabo/core";
let obj = {
alfa: {
profiles: [
{
name: "Jhon",
age: 25
}
]
}
};
Objects.set(obj, "alfa.profiles.0.name", "Jhon Doe");
console.log(Objects.get(obj, "alfa.profiles.0.name"));
Objects.set(obj, "alfa.profiles.0.social.facebook.profile", "facebookid");
console.log(Objects.get(obj, "alfa.profiles.0.social.facebook.profile"));
Using Tokens utilities functions
Tokens its a set of function to generate pseudorandoms tokens. There are functions to generate
short, medium and long tokens. Short and medium token are generated with [uniqid
][uniqid] and long tokens are generated with [sha.js
][sha.js].
import { Tokens } from "@ikoabo/core";
const shortToken = Tokens.short;
const mediumToken1 = Tokens.medium1;
const mediumToken2 = Tokens.medium2;
const longToken = Tokens.long;
Using Streams
Stream class allow to pipe streamed data to the express
response. User can use a filter function to prepare the object data to be sent into the response. Filter function its an optional parameter.
import { Streams } from "@ikoabo/core";
...
router.get("/data",
(req: Request, res: Response, _next: NextFunction) => {
MongoModel.find({ ... }).cursor().pipe(Streams.stringify((data: any)=>{
return {
id: data.id,
name: data.name
};
})).pipe(res.type("json"));
},
ResponseHandler.success,
ResponseHandler.error
);