api-common-response
v1.0.1
Published
api-common-response is a powerful and easy-to-use package for handling common API responses and managing asynchronous operations in your Node.js applications. This package provides predefined response handlers to streamline error handling and success resp
Downloads
2
Readme
api-common-response is a powerful and easy-to-use package for handling common API responses and managing asynchronous operations in your Node.js applications. This package provides predefined response handlers to streamline error handling and success responses, as well as a utility for managing promises and asynchronous functions.
Features Predefined API Responses: Easily manage standard success and error responses. Asynchronous Handling: Simplify your asynchronous code with asyncHandler. Installation To install the package, use npm:
bash Copy code npm install api-common-response
Usage
- ApiResponse Use ApiResponse to send predefined success responses.
Example
const { ApiResponse } = require('api-common-response');
// Usage in an Express route handler app.get('/api/data', (req, res) => { const data = { key: 'value' }; ApiResponse(data); }); Response Format:
- ApiError Use ApiError to handle and send predefined error responses.
const { ApiError } = require('api-common-response');
// Usage in an Express route handler app.get('/api/error', (req, res) => { try { // some operation } catch (error) { ApiError('Something went wrong'); } });
- asyncHandler Use asyncHandler to simplify your asynchronous functions and handle errors gracefully.
Example javascript Copy code const { asyncHandler } = require('api-common-response');
// Usage in an Express route handler app.get('/api/async-data', asyncHandler(async (req, res) => { const data = await fetchData(); // Your async function ApiResponse(data); }));
asyncHandler(fn): Wraps your async function to handle errors and pass them to the next middleware.
License This project is licensed under the MIT License.
Support If you have any questions or need support, feel free to rech me out at [email protected].