express-api-utils
Version:
A collection of utility functions for Express.js applications
57 lines (45 loc) • 2.05 kB
Markdown
[](https://www.npmjs.com/package/express-api-utils)
[](https://www.npmjs.com/package/express-api-utils)
[](https://bundlephobia.com/package/express-api-utils)
[](https://github.com/your-username/express-api-utils/LICENSE)
A collection of utility functions for Express.js applications including error handling, async middleware, and standardized API responses. Now, for only modulejs.
- **🚀 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.
```bash
npm install express-api-utils
```
```js
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);
```
```json
{
"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
}
```
Contributions are welcome! Please feel free to submit a Pull Request.
MIT © Aditya Attrish