@satyajeetsahoo/responses
v2.1.3
Published
Consistent Express API success and error response helpers
Maintainers
Readme
@satyajeetsahoo/responses
A standardized API response formatter for Node.js/Express applications.
Why use this package?
APIs often suffer from fragmented response structures (e.g., one route returns { data: ... }, another returns just the array [...] ). This package guarantees that your frontend always receives a consistent, predictable JSON payload for both successful operations and paginated data.
Features
- Standardized Success Structure: Enforces
{ success: true, data: ... }. - Built-in Pagination Metadata: Easily format list endpoints with page, limit, and total counts.
Installation
npm install @satyajeetsahoo/responsesUsage Examples
1. Standard Success Response
Use ApiSuccessResponse.send to format and send the data back to the client.
import { ApiSuccessResponse } from '@satyajeetsahoo/responses';
app.get('/users/:id', async (req, res) => {
const user = await db.users.find(req.params.id);
return ApiSuccessResponse.send(res, user, 'User retrieved successfully');
});Output JSON:
{
"success": true,
"message": "User retrieved successfully",
"data": { "id": 1, "name": "John Doe" }
}2. Paginated List Response
Format arrays of data with pagination metadata seamlessly.
import { ApiSuccessResponse } from '@satyajeetsahoo/responses';
app.get('/users', async (req, res) => {
const { rows, count } = await db.users.findAndCountAll({ limit: 10, offset: 0 });
return ApiSuccessResponse.sendPaginated(res, rows, {
page: 1,
limit: 10,
total: count
}, 'Users retrieved');
});Output JSON:
{
"success": true,
"message": "Users retrieved",
"data": [
{ "id": 1, "name": "John" },
{ "id": 2, "name": "Jane" }
],
"meta": {
"page": 1,
"limit": 10,
"total": 25,
"totalPages": 3
}
}