0.7.1 • Published 3 years ago
response-transformer v0.7.1
Response Transformer
Response Transformer helps you standardize your REST API response on NodeJS. It also comes with tons of response methods and functionalities with full Typescript support.
Getting started
Installation using npm
npm i response-transformer --save
Installation using yarn
yarn add response-transformer
Usage
ES5
const { okResponse, badRequestResponse } = require('response-transformer')
ES6
import { okResponse, badRequestResponse } from 'response-transformer'
Using with express
import express from 'express'
import { okResponse, badRequestResponse, validationError, sendResponse, statusCodes } from 'response-transformer'
const app = express()
app.get('/health-check', (request, response) => {
return okResponse(response, 'Everything looks good', {
name: 'Benjamin Iduwe',
role: 'Software engineer',
})
})
app.get('check', (request, response) => {
return sendResponse(response, statusCodes.ACCEPTED, 'Received for processing')
})
app.get('check', (request, response) => {
return validationError(response, 'Validation error', 'Name is required')
})
Returns
200: /health-check
{
"status": true,
"message": "Everything looks good",
"data": {
"name": "Benjamin Iduwe",
"role": "Software engineer"
}
}
202: /check
{
"status": true,
"message": "Received for processing",
}
422: /create
{
"status": false,
"message": "Validation error",
"error": "Name is required",
}
Using HttpException to throw Http errors
import { HttpException, statusCodes } from 'response-transformer';
const getName = (name) => {
if(! name){
throw new HttpException('Name is invalid', statusCodes.NOT_FOUND);
}
}
HttpException properties
- Status code: The status code for the exception (error.statusCode).
- shouldReport: Report only exception for server errors >= 500 (error.shouldReport)
Supported status codes
- OK = 200.
- CREATED = 201.
- ACCEPTED = 202.
- NO_CONTENT = 204.
- BAD_REQUEST = 400.
- PAYMENT_REQUIRED = 402.
- UNAUTHORIZED = 401.
- FORBIDDEN = 403,
- NOT_FOUND = 404.
- METHOD_NOT_ALLOWED = 405.
- CONFLICT = 409.
- UNPROCESSABLE_ENTITY = 422.
- SERVER_ERROR = 500.
- BAD_GATEWAY = 502.
- SERVICE_UNAVAILABLE = 503.
- GATEWAY_TIMEOUT = 504.
Supported frameworks
- Express
- NestJS
- AdonisJS
- SailJS
Available methods
okResponse(response, message, data);
createdResponse(response, message, data);
badRequestResponse(response, message, data);
unauthorizedResponse(response, message, data);
paymentRequiredResponse(response, message, data);
forbiddenResponse(response, message, data);
notFoundResponse(response, message, data);
methodNotAllowedResponse(response, message, data);
serverErrorResponse(response, message, data);
badGatewayResponse(response, message, data);
serviceUnavailableResponse(response, message, data);
sendResponse(response, statusCode, message, data);
Test
npm run test