@my-oneapp/common
v1.0.14
Published
This package contains shared code and utilities used across the MyMTN-Backend microservices architecture.
Readme
Common Library
This package contains shared code and utilities used across the MyMTN-Backend microservices architecture.
Installation
npm install @my-oneapp/commonFeatures
Error Handling
Located in /src/errors:
BadRequestError- For invalid request handlingCustomError- Base error class for consistent error formattingDatabaseConnectionError- Database connectivity issuesNotAuthorizedError- Authentication/Authorization failuresNotFoundError- Resource not found errorsRequestValidationError- Request validation failures
Constants
Located in /src/constants:
config.ts- Shared configuration constantsroles.ts- User role definitionsstatuses.ts- Common status codes and messages
Middleware
Located in /src/middlewares:
current-user.ts- Extracts and validates current user from JWTerror-handler.ts- Global error handling middlewarerequire-auth.ts- Authentication middlewarevalidate-request.ts- Request validation middleware
Events
Located in /src/events:
subjects.ts- Event subjects for message broker communication
Utils
Located in /src/utils:
jwt.ts- JWT token management (signing, verification)logger.ts- Centralized logging utilityresponse.ts- Standardized API response formatting
Usage Examples
Error Handling
import { NotFoundError } from "@my-oneapp/common";
if (!user) {
throw new NotFoundError("User not found");
}Authentication
import { requireAuth } from "@my-oneapp/common";
router.post("/api/resource", requireAuth, (req, res) => {
// Only authenticated users can access this route
});JWT Operations
import { JWT } from "@my-oneapp/common";
// Sign a new token
const token = JWT.sign({
id: user.id,
email: user.email,
role: user.role,
});
// Verify a token
const payload = JWT.verify(token);Response Formatting
import { Response } from "@my-oneapp/common";
res.json(Response.success("Operation successful", data));
res.json(Response.error("Operation failed", error));Environment Variables
The library requires the following environment variables:
JWT_SECRET- Secret key for JWT operationsNODE_ENV- Environment name ('development', 'production', 'test')
Contributing
- Create a feature branch from
main - Make your changes
- Update tests if necessary
- Submit a pull request
Testing
npm run testBuilding
npm run buildThe build output will be in the /build directory.
License
Internal use only - MyMTN Backend Services
