express-api-utils
v1.0.2
Published
A collection of utility functions for Express.js applications
Downloads
10
Maintainers
Readme
Express API Utils 🚀
A collection of utility functions for Express.js applications including error handling, async middleware, and standardized API responses. Now, for only modulejs.
Features
- 🚀 asyncHandler - Clean async/await error handling
- 🛡️ APIError - Standardized error classes
- 📦 APIResponse - Consistent API responses
- 🔧 errorHandler - Global error handling middleware that handle Mongoose validation error, Mongoose duplicate key error, jwt token validation error, jwt token expire error, internet error.
🚀 Quick Start
Installation
npm install express-api-utilsExample
import { asyncHandler, APIError, APIResponse, errorHandler } from 'express-api-utils';
import express from 'express';
const app = express();
app.get('/users/:id', asyncHandler(async (req, res) => {
const user = await getUserById(req.params.id);
if (!user) throw APIError.notFound('User not found');
return new APIResponse(user).send(res);
}));
app.use(errorHandler);{
"statusCode": 200, // default 200 status code
"success": true,
"data": { // default null
"id":1
},
"message": "Success", // default message
"metadata": {}, // default empty object
"timestamp": "timestamp" // current date
}Getting started
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT © Aditya Attrish
