Skip to content

kodarsiv/krh-node

Repository files navigation

πŸš€ krh-node

A powerful response management package for Node.js, featuring multi-language support, dynamic response codes, and high-performance storage with Redis & SQLite.


πŸ“Œ Features

πŸ‘‰ Automatic Response Formatting – Simplifies handling success and error responses.
πŸ‘‰ Multi-Language Support – Fetches localized messages from Redis & SQLite.
πŸ‘‰ Dynamic Storage – Uses Redis for caching, with SQLite as a fallback.
πŸ‘‰ Optimized for Scalability – Works in both single-server and distributed environments.
πŸ‘‰ Fully Configurable – Manage response codes, messages, and settings via config.yml.


πŸ“š Installation

1️⃣ Clone the Repository

git clone https://github.com/YOUR_GITHUB_USERNAME/krh-node.git
cd krh-node

This automatically creates the project structure, installs dependencies, and initializes databases.

2️⃣ Start the Server

npm run dev

or for production:

npm run build && npm start

βš™οΈ Configuration

All settings are stored in config/config.yml:

supportedLanguages: ['en', 'fr']
codeCategories:
  auth: 200100
  payment: 200200
  validation: 400100
storage:
  sqlite:
    path: './storage-data/messages.db'
  redis:
    host: 'localhost'
    port: 6379

Environment Variables

Modify .env for custom Redis & SQLite settings:

REDIS_HOST=localhost
REDIS_PORT=6379
SQLITE_DB_PATH=./storage-data/messages.db

πŸ›  Usage

1️⃣ Middleware Usage (Express)

Add the middleware to your Express app:

import express from 'express';
import { responseManager } from './src/middleware/responseHandler';

const app = express();
app.use(responseManager());

app.get('/user', (req, res) => {
  res.success(200101, { user: 'Alice' });
});

app.get('/error', (req, res) => {
  res.error(400101, "Invalid request");
});

app.listen(3000, () => console.log('Server running on port 3000'));

2️⃣ Fetching Messages from Storage

import { messageStorage } from './src/storage';

async function testStorage() {
  await messageStorage.setMessage(200101, 'en', 'User retrieved successfully');
  const msg = await messageStorage.getMessage(200101, 'en');
  console.log(msg); // Output: User retrieved successfully
}

testStorage();

πŸ“š API Reference

res.success(code, data, customMessage?)

πŸ‘‰ Automatically sets HTTP status (based on code).
πŸ‘‰ Fetches localized message if no customMessage is provided.
πŸ‘‰ Wraps response in a standard format.

Example

res.success(200101, { user: 'Alice' });
// Response:
{
  "status": "success",
  "code": 200101,
  "message": "User retrieved successfully",
  "data": { "user": "Alice" }
}

res.error(code, errorDetails, customMessage?)

🚨 Standardized error handling.
🚨 Automatically maps HTTP status from code.
🚨 Includes error details for debugging.

Example

res.error(400101, "Invalid input");
// Response:
{
  "status": "error",
  "code": 400101,
  "message": "Validation failed. Please check your input.",
  "error": "Invalid input"
}

🏒 Project Structure

krh-node/
β”œβ”€β”€ config/
β”‚   └── config.yml              # Package-wide configuration
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ bootstrap/              # Handles DB & Redis connections
β”‚   β”œβ”€β”€ config/                 # Loads configuration settings
β”‚   β”œβ”€β”€ core/                   # Core logic (Code Registry, Localization, Errors)
β”‚   β”œβ”€β”€ middleware/             # Express middleware
β”‚   β”œβ”€β”€ services/               # Business logic for response handling
β”‚   β”œβ”€β”€ storage/                # SQLite & Redis storage providers
β”‚   β”œβ”€β”€ types/                  # TypeScript interfaces
β”‚   β”œβ”€β”€ utils/                  # Helpers (logging, validation, etc.)
β”‚   β”œβ”€β”€ index.ts                # Main entry file
β”œβ”€β”€ storage-data/                # SQLite database & migrations
β”œβ”€β”€ tests/                      # Unit & Integration tests

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published