1.2.1 • Published 2 years ago

api-response-format v1.2.1

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

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
MethodsDescriptionResponse FormatParameters
badRequestFunction: Bad requestreturn { status: 400, success: false, obj }obj: required, that can be anything such as message or data object
customMessageFunction: Send custom messagereturn { status: 409, success: false, msg }msg: required
logoutSuccessFunction: 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'
unauthrizedFunction: Unauthrized Accessreturn { status: 401, success: false, message: 'User is not authorized for this operation' }msg: required, defaultValue: 'User is not authorized for this operation'
loginSuccessFunction: Successful Loginreturn { status: 200, success: true, message: 'You have been loggedIn successfully', data }data: required
accessTokenUpdatedFunction: Successful updation of Access Tokenreturn { status: 200, success: true, message: 'Access Token Update successfully', data }data: required
dataEncryptedFunction: Encrypt Data Successfullyreturn { status: 200, success: true, message: 'User Data Encrypt successfully' }NA
dataSyncFunction: Data Sync Successfullyreturn { status: 200, success: true, message: 'Data Sync successfully' }NA
dataNotFoundFunction: Data Not Foundreturn { status: 404, success: false, message: 'No record(s) found', data }data: optional
dataFoundFunction: Sucess or Data foundreturn { status: 200, success: true, message: 'Success', data }data: optional
dataFoundWithPaginationFunction: Success or Data found with pagination parametersreturn { status: 200, success: true, message: 'Success', data: { list: data, totalPages, limit: parseInt(limit), totalRecords, currentPage } }data, totalPages, limit, totalRecords, currentPage
notExistsFunction: Data does not existsreturn { status: 409, success: false, message: '${name} doesn't exits' }name: required
dataAlreadyExistsFunction: Data already exists with provided requestreturn { status: 409, success: false, message: '${name} already exits with provided ${data}' }name: required, data: requied
intervalServerErrorFunction: Server errorreturn { status, success: false, message }msg: optional (defaultValue: 'Something went wrong, please try again'), statusCode: optional (defaultValue: 500)
documentUpdatedFunction: Document updated successfullyreturn { status: 200, success: true, message: 'Documents has been updated successfully' }NA
dataCreatedFunction: Data Created Successfullyreturn { status: 201, success: true, message: '${name} has been created successfully', data }name: required, data: required
dataNotCreatedFunction: There was an error while creating datareturn { status: 500, success: false, message: 'Something went wrong while creating ${name}' }name: required
dataUpdatedFunction: Data has been updated successfullyreturn { status: 200, success: true, message: '${name} has been updated successfully', data }name: required, data: required
dataNotUpdatedFunction: There was an error while updating datareturn { status: 500, success: false, message: 'Something went wrong while updating ${name}'}name: required
dataDeletedFunction: Data has been deleted successfullyreturn { status: 200, success: true, message: '${name} has been deleted successfully'}name: required
dataNotDeletedFunction: There was an error deleting the recordreturn { 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.

1.2.1

2 years ago

1.2.0

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago