vnlp-sdk
v1.3.1
Published
VNLP's API SDK
Downloads
385
Maintainers
Keywords
Readme
Project Overview - VNLP base sdk
Introduction
This project is a utility library designed to provide a collection of helpers for encryption, logging, API communication, and base models, aimed at improving productivity and maintainability in modern software systems. It includes commonly needed tools such as encryption helpers, logging services, and base repository models for database interactions.
Key Features
- Encryption & Security: RSA and AES encryption helpers to secure data transmission.
- Base Models & Repositories: Base classes for database models and repositories to interact with your data layer.
- Logging: Centralized logging utilities to handle logs across various platforms.
- API Middleware: Middleware for logging Axios requests and responses.
Modules
Core Modules
- Crypto: Encryption helpers for securing data.
RSAEncryption
: RSA encryption utility.AESEncryption
: AES encryption utility.
- Base: Base repository and model for interacting with databases.
BaseRepository
: Provides basic repository functionality.BaseModel
: Base class for models, designed for database interactions.
- Logger: Centralized logging system.
- Logs all interactions and events across different services.
- Axios Middleware: Middleware for Axios that logs HTTP request and response data.
TODO
Upcoming Features
- AWS S3 Helper: Integration with AWS S3 for file uploads and management.
- Additional Encryption Methods: Including more encryption algorithms such as HMAC, DES, or others.
- Extended Middleware: Additional middleware for other popular HTTP clients.
- Advanced Logging Features: Support for more log destinations (e.g., file system, cloud storage) and enhanced log formatting.
- Database Enhancements: More advanced database helpers for interacting with different databases (e.g., PostgreSQL, MySQL).
- Email Service: Integration with an email service for sending notifications and alerts.
- Caching Mechanisms: Adding cache utilities to improve performance with Redis or other caching services.
Installation
To install the package, run the following command:
npm install vnlp-sdk
VNLP logger
Overview
This package provides a centralized logging system designed for use in Node.js applications. It integrates with various logging platforms such as Telegram and supports logging HTTP request and response data. The package uses winston
for logging and provides functionality to store logs locally or send them to external channels such as Telegram.
Features
- Supports logging to the console and file system with rotation.
- Provides middleware for logging HTTP requests and responses in Express.
- Integration with Telegram for sending log messages externally.
- Support for custom error handling and masking sensitive data in logs.
- Automatically generates unique request IDs for HTTP requests.
Usage
Logger Setup To initialize the logger, use the VNLPLogger class and provide the necessary configuration:
import { VNLPLogger } from 'vnlp-sdk';
const logger = new VNLPLogger({
serviceName: 'my-service',
env: 'prod',
writeFile: true,
external: {
isDisabled: false,
provider: 'telegram',
botToken: 'your-bot-token',
channel: 'your-telegram-channel',
},
});
HTTP Logger Middleware
Use the createHttpLoggerMiddleware function to log HTTP request and response data in an Express.js application:
import { createHttpLoggerMiddleware } from 'vnlp-sdk';
import express from 'express';
const app = express();
app.use(createHttpLoggerMiddleware(logger));
app.get('/', (req, res) => {
res.send('Hello World');
});
Logging Messages
You can log messages at different levels using the VNLPLogger instance:
logger.info('Informational message', { userId: 123 });
logger.error('Error message', { errorDetails: 'Detailed error info' });
logger.warn('Warning message', { warningDetails: 'Warning details' });
External Logging (Telegram)
Send logs to an external service like Telegram:
logger.logExternal({
message: 'An external log message',
level: 'error',
channel: 'my-telegram-channel',
});
Configuration
VNLPLoggerService
The VNLPLogger requires a configuration object with the following fields:
serviceName: The name of the service to be logged (e.g., my-service). env: The environment the service is running in (e.g., prod, dev, staging). writeFile: Boolean flag to enable or disable file logging. external: Configuration for external logging providers like Telegram. isDisabled: Disable external logging if set to true. provider: The external provider, currently only supports telegram. botToken: Telegram bot token for sending messages. channel: Telegram channel where logs will be sent. envBlackList: List of environments to exclude from external logging (optional). File Logging Logs can be written to files with rotation using winston's DailyRotateFile transport. Logs are stored in the ./logs directory and will be rotated based on the configured pattern.
Masking Sensitive Data Sensitive data such as tokens, authorization headers, and cookies are automatically masked in the logs to prevent leaking sensitive information. You can customize the masking logic as needed.
Contribution
If you'd like to contribute to this project, please fork the repository, make your changes, and submit a pull request. Make sure to write tests for any new features or bug fixes.
License
This project is licensed under the MIT License.