next-api-flow
v1.0.2
Published
Standardized API response handling for Next.js applications
Maintainers
Readme
Next.js API Response Standardizer
A lightweight, type-safe package for standardizing API responses in Next.js applications.
Features
✅ Standardized response format for all APIs
✅ TypeScript support with full type safety
✅ Pagination support out of the box
✅ Common error response helpers
✅ Middleware for automatic error handling
✅ Works with App Router
✅ Customizable headers and status codes
Installation
npm install next-api-flow
# or
yarn add next-api-flowQuick Start
import { createApiHandler, ResponseBuilder } from 'nextjs-api-response';
// App Router
export const GET = createApiHandler(async () => {
const data = await fetchData();
return ResponseBuilder.success(data, 'Data fetched');
});
export const POST = createApiHandler(async (request: NextRequest) => {
const data = await request.json();
// Validation example
if (!data.email) {
return ResponseBuilder.validationError([
{ code: 'VALIDATION_ERROR', message: 'Email is required', field: 'email' }
]);
}
const user = await createUser(data);
return user; // automatially convert data to api response format
});Response format
Success Format
{
"success": true,
"message": "Users fetched successfully",
"data": [],
"meta": {
"timestamp": "2024-01-01T00:00:00.000Z",
"path": "/api/users",
"requestId": "",
}
}Error Response
{
"success": false,
"message": "Validation failed",
"errors": [
{
"code": "VALIDATION_ERROR",
"message": "Email is required",
"field": "email"
}
],
"meta": {
"timestamp": "2024-01-01T00:00:00.000Z",
"path": "/api/users"
}
}Contributing
License
MIT © Aditya Attrish
Links
- API REFERENCE
- GETTING STARTED
- CONTRIBUTING
- Source: repository root
