Add logger utility and JSON manipulation class; update package.json

This commit is contained in:
David Vargas 2024-11-06 01:09:12 -06:00
parent 1821c01f67
commit 526998a080
4 changed files with 1135 additions and 7 deletions

1026
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -3,7 +3,8 @@
"version": "1.0.0", "version": "1.0.0",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1" "start": "node index.js",
"dev": "nodemon index.js"
}, },
"keywords": [], "keywords": [],
"author": "", "author": "",
@ -14,8 +15,14 @@
"dotenv": "^16.4.5", "dotenv": "^16.4.5",
"form-data": "^4.0.1", "form-data": "^4.0.1",
"node-cron": "^3.0.3", "node-cron": "^3.0.3",
"pino": "^9.5.0",
"pino-pretty": "^11.3.0",
"qrcode-terminal": "^0.12.0", "qrcode-terminal": "^0.12.0",
"translate-google": "^1.5.0",
"whatsapp-web.js": "^1.26.0", "whatsapp-web.js": "^1.26.0",
"winston": "^3.16.0" "winston": "^3.16.0"
},
"devDependencies": {
"nodemon": "^3.1.7"
} }
} }

94
utils/json_utils.js Normal file
View File

@ -0,0 +1,94 @@
class JSONUtils {
constructor(data) {
if (typeof data !== 'object' || data === null) {
throw new Error('Data must be a valid JSON object');
}
this.data = data;
}
getValue(path) {
const keys = path.split('.');
let result = this.data;
try {
for (const key of keys) {
if (result[key] === undefined) {
throw new Error(`Property '${key}' does not exist`);
}
result = result[key];
}
return result;
} catch (error) {
console.error(error.message);
return null; // Devuelve null si hay un error
}
}
setValue(path, value) {
const keys = path.split('.');
let current = this.data;
try {
keys.forEach((key, index) => {
if (index === keys.length - 1) {
current[key] = value;
} else {
if (current[key] === undefined) {
current[key] = {}; // Crea objeto si no existe
}
current = current[key];
}
});
} catch (error) {
console.error(`Error setting value: ${error.message}`);
}
}
updateValue(path, value) {
const currentValue = this.getValue(path);
if (currentValue !== null) {
this.setValue(path, value);
} else {
console.error(`Cannot update: Property at '${path}' does not exist`);
}
}
deleteValue(path) {
const keys = path.split('.');
let current = this.data;
try {
keys.forEach((key, index) => {
if (index === keys.length - 1) {
delete current[key];
} else {
if (current[key] === undefined) {
throw new Error(`Property '${key}' does not exist`);
}
current = current[key];
}
});
} catch (error) {
console.error(`Error deleting value: ${error.message}`);
}
}
saveToFile() {
try {
fs.writeFileSync(this.filePath, JSON.stringify(this.data, null, 2));
console.log('Datos guardados exitosamente en el archivo.');
} catch (error) {
console.error('Error al guardar los datos:', error.message);
}
}
getJSON() {
return this.data;
}
printJSON() {
console.log(JSON.stringify(this.data, null, 2));
}
}
module.exports = JSONUtils;

13
utils/logger.js Normal file
View File

@ -0,0 +1,13 @@
const pino = require('pino');
const logger = pino({
level: process.env.LOG_LEVEL || 'info',
transport: {
target: 'pino-pretty',
options: {
colorize: true
}
}
});
module.exports = logger;