agr-shared-utils
v1.0.1
Published
Shared utilities, types, and constants for AGR Microservices
Maintainers
Readme
@agr/shared
Shared utilities, types, and constants for AGR Microservices
📦 Installation
npm install @agr/shared🚀 Usage
Import the entire package
import { ValidationUtils, ResponseUtils, HTTP_STATUS } from '@agr/shared';Import specific utilities
import { ValidationUtils } from '@agr/shared';
import { ResponseUtils } from '@agr/shared';
import { HTTP_STATUS } from '@agr/shared';🔧 Available Utilities
ValidationUtils
isValidLength(value, min, max)- Check string lengthisValidRange(value, min, max)- Check numeric rangeisValidObjectId(id)- Validate MongoDB ObjectIdisValidEmail(email)- Validate email formatisValidUrl(url)- Validate URL formatcreateValidationError(field, message, value)- Create validation error objectisInRange(value, min, max)- Check if value is in range
ResponseUtils
success(data, message)- Success responseerror(message, statusCode)- Error responsevalidationError(message, errors)- Validation error responsecreated(data, message)- Created responsepaginated(data, pagination, message)- Paginated responsenotFound(message)- Not found responseunauthorized(message)- Unauthorized responseforbidden(message)- Forbidden response
Constants
HTTP_STATUS- HTTP status codesVALIDATION_CONSTRAINTS- Validation limitsUserRole- User role constantsPostStatus- Post status constantsContentType- Content type constants
📝 Examples
Validation
import { ValidationUtils } from '@agr/shared';
const isValid = ValidationUtils.isValidLength('Hello', 1, 10); // true
const isValidId = ValidationUtils.isValidObjectId('507f1f77bcf86cd799439011'); // trueResponse
import { ResponseUtils, HTTP_STATUS } from '@agr/shared';
const successResponse = ResponseUtils.success(data, 'Operation successful');
const errorResponse = ResponseUtils.error('Something went wrong', HTTP_STATUS.INTERNAL_SERVER_ERROR);🏗️ Development
# Install dependencies
npm install
# Build
npm run build
# Watch mode
npm run build:watch
# Clean
npm run clean📄 License
ISC
🤝 Contributing
- Fork the repository
- Create your feature branch
- Commit your changes
- Push to the branch
- Open a Pull Request
