Gathering detailed insights and metrics for rest-api-response-npm
Gathering detailed insights and metrics for rest-api-response-npm
Gathering detailed insights and metrics for rest-api-response-npm
Gathering detailed insights and metrics for rest-api-response-npm
rest-api-response-npm-jkg
The package is a collection of response handler functions that can be used in a Node.js web application. These functions provide a consistent way to format HTTP responses and simplify the process of handling common response scenarios. The successResponse
easy-fetch-api
Make REST requests using the Fetch API as easy as typing npm install and avoid having to manually handle headers and response parsing.
protractor-api-resource
This library extends request npm module. It helps write easy protractor tests when test data are objtained from API response.
ant-openunion-blockchain
NPM module for acquiring response from the Ant Open Union Blockchain Rest API without form authentication hassle.
npm install rest-api-response-npm
Typescript
Module System
Node Version
NPM Version
47.7
Supply Chain
87.3
Quality
75.6
Maintenance
100
Vulnerability
99.6
License
Total Downloads
3,172
Last Day
2
Last Week
16
Last Month
64
Last Year
1,637
Minified
Minified + Gzipped
Latest Version
0.1.4
Package Id
rest-api-response-npm@0.1.4
Unpacked Size
11.52 kB
Size
3.37 kB
File Count
3
NPM Version
9.6.7
Node Version
18.15.0
Publised On
04 Jul 2023
Cumulative downloads
Total Downloads
Last day
100%
2
Compared to previous day
Last week
-38.5%
16
Compared to previous week
Last month
-60.5%
64
Compared to previous month
Last year
6.6%
1,637
Compared to previous year
1
npm install rest-api-response-npm
The successResponse
, errorResponse
, customResponse
, checkValidation
, validationResponse
, catchError
functions are JavaScript functions that are used to send HTTP responses from a server to a client. They take in the following parameters:
res
- Parameter that represents the HTTP response object that will be sent back to the client.
err
- Parameter that represents the error object that will be sent back to the client.
data
- Parameter that represents the data that will be sent back to the client. This can be any JSON object or value.
message
- Parameter that represents the message that will be sent back to the client along with the data.
statusCode
- Parameter that represents the HTTP status code that will be sent back to the client.
successResponse
: This function takes in four parameters, res
(required), data
(optional), message
(optional), and statusCode
(optional).
errorResponse
: This function takes in four parameters, res
(required), data
(optional), message
(optional), and statusCode
(optional).
customResponse
: This function takes in four parameters, res
(required), data
(optional), message
(optional), and statusCode
(optional).
validationResponse
: This function takes in four parameters, res
(required), data
(required), message
(optional), and statusCode
(optional).
checkValidation
: This function takes in four parameters, res
(required) and req
(required).
catchError
: This function takes in four parameters, res
(required) and err
(required).
To use these functions in a Node.js application, simply import the module and call the desired function with the appropriate parameters. For example.
app.js
const express = require('express');
// const { ... } = require('rest-api-response-npm');
const { body } = require("express-validator");
const app = express();
// Use functions as per requiremet
// ...
app.listen(3000, () => {
console.log('Server started on port 3000');
});
const { successResponse } = require('rest-api-response-npm');
app.get('/', (req, res) => {
const data = {
id: 12,
user_name: 'Jaykumar Gohil'
};
successResponse(res, data, 'Success message', 200);
});
const { errorResponse } = require('rest-api-response-npm');
app.get('/error', (req, res) => {
let checkUser; // object
let message; // object
if(checkUser) {
message = "User not exist!"
}
errorResponse(res, checkUser, message, 400);
// or
errorResponse(res, null, message, 400);
// or
errorResponse(res, null, message);
// or
errorResponse(res, null, 'Error message');
});
const { customResponse } = require('rest-api-response-npm');
app.get('/custom', (req, res) => {
// Example 1
const data = [
{
access_token: "asdasds.asdsadsasq2we23easa.asdasdasdas",
name: 'namessss'
}
];
customResponse(res, data, 'Login Successfully', 200);
// or
customResponse(res, data);
// Example 2
customResponse(res, null, 'Token Expiration', 401);
});
const { checkValidation } = require('rest-api-response-npm');
const { body } = require("express-validator");
const validationRequest = [
// validation for
// ...
body("password")
.notEmpty()
.withMessage("Password is requierd")
.isLength({ min: 6, max: 250 })
.withMessage("Minimum 6 character password require")
.trim(),
// ...
]
app.post('/api/user', validationRequest, (req, res) => {
// Validate the request parameters
checkValidation(res, req);
// If there are no validation errors, proceed with the request handling
// ...
});
const { validationResponse } = require('rest-api-response-npm');
app.post('/api/login', (req, res) => {
// ...
// Create validation formated data for invalid password API response
data = {
password: [
"Invalid password",
]
}
validationResponse(res, data);
// or
validationResponse(res, data, "Validation failed!");
// or
validationResponse(res, data, "Validation failed!", 422);
});
app.js
const { catchError } = require('rest-api-response-npm');
// ...
// default error messgae
app.use((error, req, res, next) => {
// All types of errors in a project will be handled by this tool, and an error response will be provided in the proper format
catchError(res, error);
});
// Use functions as per requiremet
// ...
app.listen(3000, () => {
console.log('Server started on port 3000');
});
demo:
app.get('/api/user', (req, res) => {
try {
// Handle the request and throw an error if needed
// ...
} catch (err) {
// Use the next method to handle the error, and the catchError function will handle the response.
next(err);
}
});
REST APIs have become an integral part of modern web development, and this code offers a standardized and reliable approach to generate HTTP responses in a Node.js application. These functions, successResponse
, errorResponse
, and customResponse
, checkValidation
, validationResponse
, catchError
can be used as helpers to handle different types of responses in a consistent format.
By using these functions, you can prevent API format issues and save valuable development time. For frontend or mobile developers, these functions can be especially helpful since they provide a clear and predictable response structure that can be easily integrated into their application.
In addition to their practical benefits, using these functions is super easy to implement and use in your Node.js project. With just a few lines of code, you can experience the benefits of standardized and reliable HTTP responses.
By implementing these functions in your Node.js project, you can improve the overall user experience by providing clear and concise error messages and feedback. Give them a try and experience the benefits of standardized and reliable HTTP responses in your Node.js REST API development!
Above functions used for handling responses and errors in a server. successResponse
is used to return a response with data, message, and status code, while errorResponse
and customResponse
handle errors and return responses accordingly. validationResponse
reformats request parameters and returns an appropriate response, checkValidation
validates request parameters and returns an appropriate response, and catchError
is used to handle errors that occur during request processing and return a response with an error message.
No vulnerabilities found.
No security vulnerabilities found.