ts-logs
Version:
This package provide a skd for audit and manager logs in nodejs and express
279 lines • 9.81 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Step = void 0;
const log_utils_1 = require("../utils/log.utils");
const node_crypto_1 = require("node:crypto");
const utils_1 = require("../utils");
const extract_body_util_1 = require("../utils/extract-body.util");
const encrypt_keys_util_1 = require("../utils/encrypt-keys.util");
const decrypt_keys_util_1 = require("../utils/decrypt-keys.util");
const mask_step_data_util_1 = require("../utils/mask-step-data.util");
class Step {
constructor(props) {
this.name = props.name ?? 'default';
this.tags = props.tags ?? Step.extractTagsFromData(props.data);
this.url = props.url ?? 'none';
this.stack = props.stack ?? 'none';
this.data = props.data ?? {};
this.statusCode = props.statusCode ?? 200;
this.message = props.message ?? 'none';
this.type = props.type ?? 'info';
this.method = props.method ?? 'NONE';
this.createdAt = new Date();
const uid = props.uid ?? Step.extractId(props.data);
this.uid = uid;
this.additionalInfo = props.additionalInfo ?? null;
this.category = props.category ?? 'none';
Object.freeze(this);
}
/**
* @description Encrypt attributes.
* @param options object as EncryptStepOption.
* @returns instance of Step.
*/
async encrypt(options) {
const data = await (0, encrypt_keys_util_1.default)(this, options);
return new Step({ ...this, data });
}
/**
* @description Decrypt attributes.
* @param options object as EncryptStepOption.
* @returns instance of Step.
*/
async decrypt(options) {
const data = await (0, decrypt_keys_util_1.default)(this, options);
return new Step({ ...this, data });
}
/**
* @description Create an instance of Step type: error
* @param props as Object with SProps params
* @returns an instance of Step
*/
static error(props) {
const statusCode = props.statusCode ?? 400;
return new Step({ ...props, type: 'error', statusCode });
}
/**
* @description Create an instance of Step adding additional info.
* @param info as string.
* @returns an instance of Step
*/
setAdditionalInfo(info) {
return new Step({ ...this, info });
}
/**
* @description Create an instance of Step type: stack
* @param props as Object with SProps params
* @returns an instance of Step
*/
static stack(props) {
const statusCode = props.statusCode ?? 500;
return new Step({ ...props, type: 'stack', statusCode });
}
/**
* @description Create an instance of Step type: debug
* @param props as Object with SProps params
* @returns an instance of Step
*/
static debug(props) {
return new Step({ ...props, type: 'debug' });
}
/**
* @description Create an instance of Step type: fatal
* @param props as Object with SProps params
* @returns an instance of Step
*/
static fatal(props) {
return new Step({ ...props, type: 'fatal' });
}
/**
* @description Create an instance of Step type: info
* @param props as Object with SProps params
* @returns an instance of Step
*/
static info(props) {
return new Step({ ...props, type: 'info' });
}
/**
* @description Create an instance of Step type: warn
* @param props as Object with SProps params
* @returns an instance of Step
*/
static warn(props) {
return new Step({ ...props, type: 'warn' });
}
/**
* @description Extract id from request body if exists.
* @param body as string or object
* @returns id or undefined
*/
static extractId(body = {}) {
const data = (0, extract_body_util_1.default)(body);
const id = data?.['id'] ?? (0, node_crypto_1.randomUUID)();
return id;
}
/**
* @description Extract tags from body if exists keys.
* @param body as string or object
* @returns tags as array of string or empty array.
*/
static extractTagsFromData(body = {}) {
const data = (0, extract_body_util_1.default)(body);
const isObject = data && typeof data === 'object';
const isNotArray = !(Array.isArray(data));
return (isObject && isNotArray) ? Object.keys(data).slice(0, 5) : [];
}
/**
* @description Create an instance of Step
* @param props as Object with SProps params
* @returns an instance of Step
*/
static create(props) {
return new Step({ ...props });
}
/**
* @description Create an step instance from error: Error from a try catch block.
* @param error as instance of Error from catch block.
* @param props object with CatchProps.
*/
static catch(error, props) {
const rmKeys = (props && props.remove) ? props.remove : [];
const err = new Error('sample');
const name = utils_1.Reference.fromError(err);
error.name = name;
const params = (0, utils_1.StepPropsFromAxiosError)(error, rmKeys);
return Step.create(params);
}
/**
* @description Create a new instance of Step with provided tags.
* @param tags as Array of Tags.
* @returns new instance of Step with tags.
*/
addTags(tags) {
return new Step({ ...this, tags: [...this.tags, ...tags] });
}
;
/**
* @description Create a new instance of Step with provided tag.
* @param tag as Tag.
* @returns new instance of Step with tag.
*/
addTag(tag) {
return new Step({ ...this, tags: [...this.tags, tag] });
}
/**
* @description Create a new instance of Step with provided name.
* @param name as string.
* @returns new instance of Step with provided name.
*/
setName(name) {
return new Step({ ...this, name });
}
/**
* @description Remove keys from body matching provided param.
* @param keys as array of string.
* @returns new instance of Step.
*/
remove(keys) {
const body = this.data;
const result = (0, extract_body_util_1.default)(body);
const updated = (0, utils_1.DeleteObjectKey)(result, keys);
const data = JSON.stringify(updated);
return new Step({ ...this, data });
}
/**
* @description Create a new instance of Step with provided method.
* @param method as Method.
* @returns new instance of Step with provided method.
*/
setMethod(method) {
return new Step({ ...this, method });
}
/**
* @description Create a new instance of Step with provided stack.
* @param stack as Error stack as string.
* @returns new instance of Step with provided stack.
*/
setStack(stack) {
return new Step({ ...this, stack });
}
/**
* @description Create a new instance of Step with provided message.
* @param message as Error message or Info message as string.
* @returns new instance of Step with provided error or info message.
*/
setMessage(message) {
return new Step({ ...this, message });
}
/**
* @description Create a new instance of Step with provided status code.
* @param code number representing http status code. 200, 404, 400, 500 ...
* @returns new instance of Step with provided status code.
*/
setStatusCode(code) {
return new Step({ ...this, statusCode: code });
}
/**
* @description Create a new instance of Step with provided data.
* @param data params or request body as string or object.
* @returns new instance of Step with provided data.
*/
setData(data) {
return new Step({ ...this, data });
}
/**
* @description Create a new instance of Step with uid.
* @param uid as unique id or any string representing log id.
* @returns new instance of Step with provided uid.
*/
setUid(uid) {
return new Step({ ...this, uid });
}
/**
* @description Create a new instance of Step with url.
* @param url as string.
* @returns new instance of Step with provided url.
*/
setURL(url) {
return new Step({ ...this, url });
}
mask(attributes) {
const data = this.data;
if (!data)
return this;
const payload = (0, mask_step_data_util_1.default)(data, attributes);
return new Step({ ...this, data: payload });
}
/**
* @description Print single step on terminal.
* @param locales as LocalesArgument to format date.
* @param options as DateTimeFormatOptions to format date.
*/
print(locales, options) {
const message = this.getPrintableMsg(locales, options);
(0, log_utils_1.default)(message);
}
/**
* @description Create a formatted message as string to print on terminal.
* @param locales as LocalesArgument to format date.
* @param options as DateTimeFormatOptions to format date.
* @returns formatted message string based on unix code to print on terminal.
*/
getPrintableMsg(locales, options) {
const builder = utils_1.Messages[this.type];
if (!builder)
return utils_1.Messages['default'](this, locales, options);
return builder(this, locales, options);
}
/**
* @description Create a new instance of Step with provided category.
* @param category params as string.
* @returns new instance of Step with provided category.
*/
setCategory(category) {
return new Step({ ...this, category });
}
}
exports.Step = Step;
exports.default = Step;
//# sourceMappingURL=step.js.map