api-response-format
v1.2.1
Published
A package libary that can help users to manage the well format of all the APIs in thier projects.
Downloads
28
Maintainers
Readme
API Response Format Package
The API Response Format Package is a Node.js package that provides a standardized format for API responses. This package can be used to simplify the process of returning consistent and structured responses from API endpoints. Features.
- Standardized response format for API endpoints
- Customizable response fields and error messages
- Easy integration with existing Node.js projects
- Well-documented API and code
Installation
To install the API Response Format Package, simply run the following command:
npm install api-response-format
Usage
Using the API Response Format Package is straightforward. Simply import the package in your Node.js project and use the response function to return a standardized response from your API endpoints.
In the example, the response.invalidCredentials function is used to return a invalid credentials response. The response.dataFound function is used to return data response with success response code.
Example:
import { Response } from "api-response-format";
import express from 'express';
const app = express();
const port = 3000;
app.listen(port, () => console.log(`App listening on port ${port}!`))
app.get('/my-route', (req, res) => {
let data = "Response Data Sample";
res.send(Response.dataFound(data));
});
The API Response Format Package also provides a different response methods for returning responses when the client sends an invalid, unauthorized request to the API endpoint.
Available Methods
| Methods | Description | Response Format | Parameters | | ------ | ------ | ------ | ------ | | badRequest | Function: Bad request | return { status: 400, success: false, obj } | obj: required, that can be anything such as message or data object | customMessage | Function: Send custom message | return { status: 409, success: false, msg } | msg: required | logoutSuccess | Function: When user logged out sucessfully. | return {status: 200,success: true, message: 'You have been logged out successfully'} | msg: required, defaultValue: 'You have been logged out successfully' | unauthrized | Function: Unauthrized Access | return { status: 401, success: false, message: 'User is not authorized for this operation' } | msg: required, defaultValue: 'User is not authorized for this operation' | loginSuccess | Function: Successful Login | return { status: 200, success: true, message: 'You have been loggedIn successfully', data } | data: required | accessTokenUpdated | Function: Successful updation of Access Token | return { status: 200, success: true, message: 'Access Token Update successfully', data } | data: required | dataEncrypted | Function: Encrypt Data Successfully | return { status: 200, success: true, message: 'User Data Encrypt successfully' } | NA | dataSync | Function: Data Sync Successfully | return { status: 200, success: true, message: 'Data Sync successfully' } | NA | dataNotFound | Function: Data Not Found | return { status: 404, success: false, message: 'No record(s) found', data } | data: optional | dataFound | Function: Sucess or Data found | return { status: 200, success: true, message: 'Success', data } | data: optional | dataFoundWithPagination | Function: Success or Data found with pagination parameters | return { status: 200, success: true, message: 'Success', data: { list: data, totalPages, limit: parseInt(limit), totalRecords, currentPage } } | data, totalPages, limit, totalRecords, currentPage | notExists | Function: Data does not exists | return { status: 409, success: false, message: '${name} doesn't exits' } | name: required | dataAlreadyExists | Function: Data already exists with provided request | return { status: 409, success: false, message: '${name} already exits with provided ${data}' } | name: required, data: requied | intervalServerError | Function: Server error | return { status, success: false, message } | msg: optional (defaultValue: 'Something went wrong, please try again'), statusCode: optional (defaultValue: 500) | documentUpdated | Function: Document updated successfully | return { status: 200, success: true, message: 'Documents has been updated successfully' } | NA | dataCreated | Function: Data Created Successfully | return { status: 201, success: true, message: '${name} has been created successfully', data } | name: required, data: required | dataNotCreated | Function: There was an error while creating data | return { status: 500, success: false, message: 'Something went wrong while creating ${name}' } | name: required | dataUpdated | Function: Data has been updated successfully | return { status: 200, success: true, message: '${name} has been updated successfully', data } | name: required, data: required | dataNotUpdated | Function: There was an error while updating data | return { status: 500, success: false, message: 'Something went wrong while updating ${name}'} | name: required | dataDeleted | Function: Data has been deleted successfully | return { status: 200, success: true, message: '${name} has been deleted successfully'} | name: required | dataNotDeleted | Function: There was an error deleting the record | return { status: 500, success: false, message: 'Something went wrong while deleting ${name}'} | name: required
License
The API Response Format Package is open-source software licensed under the MIT License.