This repository contains shared utility functions used across multiple microservices and microfrontends.
To install this package, run:
npm i @balajidev/shared-utils
These utilities help you manage JSON Web Tokens (JWT) for authentication and authorization.
const { signToken, verifyToken } = require('utils/auth/jwt');
const token = signToken({ userId: 123 });
const payload = verifyToken(token);
console.log(payload); // { userId: 123, iat: ..., exp: ... }
These utilities provide functions to hash and compare passwords securely.
const { hashPassword, comparePassword } = require('utils/auth/password');
const hashedPassword = hashPassword('yourPassword');
console.log(comparePassword('yourPassword', hashedPassword)); // true
This utility helps validate email addresses.
const { validateEmail } = require('utils/validation/emailValidator');
console.log(validateEmail('test@example.com')); // true
These utilities provide centralized logging functions, including audit logging for security-sensitive operations.
const logger = require('utils/logging/logger');
const { auditLog } = require('utils/logging/audit');
logger.info('This is an info message');
logger.error('This is an error message');
auditLog('User login attempt', { userId: 123 });
Custom error classes for consistent error management across your application.
const { NotFoundError } = require('utils/errors');
throw new NotFoundError('Resource not found');
Utilities for date formatting and time zone conversion.
const { formatDate } = require('utils/date/format');
const { convertToTimeZone } = require('utils/date/timeZone');
const formattedDate = formatDate(new Date(), 'yyyy-MM-dd');
console.log(formattedDate); // e.g., 2024-08-16
const utcDate = convertToTimeZone(new Date(), 'UTC');
console.log(utcDate); // UTC date
Utility functions for deep cloning objects.
const { deepClone } = require('utils/object/deepClone');
const original = { a: 1, b: { c: 2 } };
const clone = deepClone(original);
console.log(clone); // { a: 1, b: { c: 2 } }
Utility for generating universally unique identifiers (UUIDs).
const { generateUUID } = require('utils/id/uuid');
const id = generateUUID();
console.log(id); // e.g., '6f1d23e4-99e2-4c87-8f14-b61e123e4567'
Send emails and push notifications using shared utilities.
const { sendEmail } = require('utils/notification/email');
const { sendPushNotification } = require('utils/notification/push');
sendEmail('user@example.com', 'Welcome', 'Thank you for signing up!');
sendPushNotification('deviceToken123', 'You have a new message!');
A pre-configured HTTP client for making API requests.
const httpClient = require('utils/api/httpClient');
httpClient.get('/api/users/123')
.then(response => console.log(response.data))
.catch(error => console.error(error));
This utility provides rate limiting middleware for Express.js applications.
const { createRateLimiter } = require('utils/rateLimiter');
const rateLimiter = createRateLimiter({ windowMs: 60000, max: 10 });
app.use('/api', rateLimiter);
These utilities help with caching data in Redis.
const { cacheResponse, getCachedResponse } = require('utils/cache');
await cacheResponse('myKey', { data: 'value' });
const data = await getCachedResponse('myKey');
console.log(data); // { data: 'value' }
This utility loads and validates configuration from environment variables.
const { config } = require('utils/config');
console.log(config.PORT); // Outputs the port number from environment or default
This utility supports translations and localization.
const { setLocale, translate } = require('utils/i18n');
setLocale('fr');
console.log(translate('greeting')); // Outputs 'Bonjour'
Input sanitization and encryption/decryption utilities.
const { sanitizeInput } = require('utils/security/sanitize');
const { encrypt, decrypt } = require('utils/security/encryption');
const safeInput = sanitizeInput('<script>alert("XSS")</script>');
console.log(safeInput); // Cleaned input
const encryptedData = encrypt('sensitive data');
const decryptedData = decrypt(encryptedData);
console.log(decryptedData); // 'sensitive data'