UNPKG

syntaxility-api-response-manager

Version:

A minimalist and standardized API Response & Pagination Manager by SyntaxilitY for Node.js applications.

98 lines (89 loc) 2.24 kB
## SyntaxilitY API Response Manager ### Usage Example (in your Express API) ``` import express from "express"; import { SyntaxilitYAPIResponseManager, PaginationHelper } from "syntaxility-api-response-manager"; import prisma from "./prismaClient.js"; const router = express.Router(); router.get("/users", async (req, res) => { try { const { page, limit, skip } = PaginationHelper.getPaginationParams(req); const [users, total] = await Promise.all([ prisma.user.findMany({ skip, take: limit, include: { role: true } }), prisma.user.count(), ]); users.sort((a, b) => b.id - a.id); const paginatedResponse = PaginationHelper.buildPaginationResponse(users, total, page, limit); SyntaxilitYAPIResponseManager.HTTP_200_OK(res, paginatedResponse, "Users fetched successfully"); } catch (err) { SyntaxilitYAPIResponseManager.HTTP_500_INTERNAL_SERVER_ERROR(res, err.message); } }); export default router; ``` ### Response with metadata: ``` { "status": 200, "code": "HTTP_200_OK", "message": "Users fetched successfully", "data": { "metadata": [ { "id": 5, "name": "Ali Raza", "email": "ali.raza@example.com", "role": { "id": 2, "name": "Admin" } }, { "id": 4, "name": "Ayesha Khan", "email": "ayesha.khan@example.com", "role": { "id": 3, "name": "Manager" } }, { "id": 3, "name": "Hassan Ahmed", "email": "hassan.ahmed@example.com", "role": { "id": 1, "name": "User" } } ], "pagination": { "total": 42, "page": 1, "limit": 3, "totalPages": 14, "hasNextPage": true, "hasPrevPage": false } } } ``` ### Response without metadata: ``` { "status": 200, "code": "HTTP_200_OK", "message": "Users fetched successfully", "data": { "metadata": [], "pagination": { "total": 0, "page": 1, "limit": 10, "totalPages": 0, "hasNextPage": false, "hasPrevPage": false } } } ```