@noviantodev/academiq
v1.1.3
Published
Detailed documentation for each package exported from the project.
Readme
Project Packages Documentation
Detailed documentation for each package exported from the project.
Table of Contents
- Error Handling
- Validation Middleware
- Queue Manager
- Async Wrapper
- Auth Middleware
- HTTP Handlers
- Service Health
- Logger
- Error Middleware
Error Handling
import { ErrorHandler, AppError } from "@noviantodev/academiq";
// Create custom error
throw new AppError("Resource not found", 404);
// Use error handler middleware
app.use(ErrorHandler);The error handling package provides:
- Standardized error responses
- Custom error classes for different scenarios
- Global error handling middleware
- Detailed error logging
Validation Middleware
import { validateRequest } from "@noviantodev/academiq";
import { z } from "zod";
// Define validation schema
const userSchema = {
body: z.object({
name: z.string().min(2),
email: z.string().email(),
age: z.number().int().positive().optional(),
}),
query: z.object({
includeDetails: z.boolean().optional(),
}),
};
// Use in route
router.post("/users", validateRequest(userSchema), handleCreateUser);Features:
- Request body validation using Zod
- Query parameters validation
- Request headers validation
- Custom validation rules support
- Automatic type conversion for query parameters
Async Wrapper
import { asyncWrapper } from "@noviantodev/academiq";
router.get(
"/users",
asyncWrapper(async (req, res) => {
const users = await getUsersFromDB();
res.json(users);
})
);Features:
- Eliminates try-catch boilerplate
- Automatic error propagation
- Express middleware compatibility
- TypeScript support
Auth Middleware
import { AuthMiddleware } from "@noviantodev/academiq";
const authMiddleware = new AuthMiddleware({
jwtSecret: process.env.JWT_SECRET,
redis: {
host: process.env.REDIS_HOST,
port: parseInt(process.env.REDIS_PORT),
},
});
// Protect routes
router.get("/profile", authMiddleware.authenticateToken, (req, res) => {
res.json(req.user);
});
// Role-based authorization
router.delete(
"/users/:id",
authMiddleware.authenticateToken,
authMiddleware.authorizeRoles("admin"),
deleteUser
);Provides:
- JWT authentication
- Role-based authorization
- Token verification
HTTP Handlers
import { ServiceResponse, handleServiceResponse } from "@noviantodev/academiq";
// Use in route
router.get(
"/users/:id",
asyncWrapper(async (req, res) => {
const user = await getUserFromDB(req.params.id);
const serviceResponse = ServiceResponse.success("User found", user);
handleServiceResponse(serviceResponse, res);
})
);Common HTTP handlers for:
- CRUD operations
- File uploads
- Response formatting
- Request parsing
Service Health
import { healthCheck, ServiceHealth } from "@noviantodev/academiq";
// Basic health check
app.get("/health", healthCheck);
// Detailed service health monitoring
const serviceHealth = new ServiceHealth();
serviceHealth.addCheck("database", async () => {
return db.isConnected();
});
app.get("/health/detailed", serviceHealth.check);Features:
- Service availability checking
- Dependency health monitoring
- Performance metrics
- System status reporting
Logger
import { AppLogger } from "@noviantodev/academiq";
const logger = new AppLogger({
level: "info",
format: "json",
filename: "app.log",
});Features:
- Flexible logging configuration
- Different log levels
- File logging
- Console logging
Error Middleware
import { errorMiddleware } from "@noviantodev/academiq";
const errorMiddleware = errorMiddleware(logger);Features:
- Error logging
- Error handling
- Error response formatting
import express from "express";
import {
ErrorHandler,
validateRequest,
asyncWrapper,
AuthMiddleware,
healthCheck,
} from "@noviantodev/academiq";
const app = express();
const authMiddleware = new AuthMiddleware({
jwtSecret: process.env.JWT_SECRET,
redis: {
host: process.env.REDIS_HOST,
port: parseInt(process.env.REDIS_PORT),
},
});
// Health check
app.get("/health", healthCheck);
// Protected route with validation
app.post(
"/users",
authMiddleware.authenticateToken,
validateRequest(userSchema),
asyncWrapper(async (req, res) => {
// Handle request
res.status(201).json({ success: true });
})
);
// Global error handler
app.use(ErrorHandler);Installation
npm install @noviantodev/academiqContributing
Please read our Contributing Guide for details on our code of conduct and the process for submitting pull requests.
License
This project is licensed under the MIT License - see the LICENSE file for details.
