Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

express-response-formatter

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-response-formatter - npm Package Compare versions

Comparing version 1.2.7 to 2.0.0

coverage/clover.xml

14

CHANGELOG.md

@@ -8,2 +8,16 @@ # Changelog

## [2.0.0] - 2020-04-28
### Changed
- Use named export to prevent interop.
- Use 'error' key instead of 'errors' for more flexibility.
- Can pass meta as second patameter for error response.
## [1.2.7] - 2020-03-06
### Changed
- Remove ignore lib but manualy publish.
## [1.2.6] - 2020-03-06

@@ -10,0 +24,0 @@

2

lib/index.d.ts

@@ -13,2 +13,2 @@ import { Request, Response, NextFunction } from 'express';

declare const responseEnhancer: () => (req: Request<import("express-serve-static-core").ParamsDictionary>, res: Response<any>, next: NextFunction) => void;
export default responseEnhancer;
export { responseEnhancer };

@@ -11,2 +11,3 @@ "use strict";

}; };
exports.responseEnhancer = responseEnhancer;
var _generateFormatters = function (res) {

@@ -23,4 +24,4 @@ var formatter = {};

else {
formatter[method.name] = function (errors) {
responseBody = _generateErrorResponse({ errors: errors });
formatter[method.name] = function (error, meta) {
responseBody = _generateErrorResponse({ error: error, meta: meta });
res.status(parseInt(method.code)).json(responseBody);

@@ -35,12 +36,12 @@ };

return ({
data: data,
meta: meta,
data: data,
});
};
var _generateErrorResponse = function (_a) {
var errors = _a.errors;
var error = _a.error, meta = _a.meta;
return ({
errors: errors,
error: error,
meta: meta,
});
};
exports.default = responseEnhancer;
{
"name": "express-response-formatter",
"version": "1.2.7",
"version": "2.0.0",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"files": [
"lib/**/*"
],
"scripts": {
"build": "tsc",
"format": "prettier --write src/**/*.ts",
"test": "jest"
"test": "jest",
"coveralls": "jest --coverage && cat ./coverage/lcov.info | coveralls"
},

@@ -23,2 +21,3 @@ "repository": "https://github.com/aofleejay/express-response-formatter.git",

"@typescript-eslint/parser": "^2.22.0",
"coveralls": "^3.1.0",
"eslint": "^6.8.0",

@@ -25,0 +24,0 @@ "express": "^4.17.1",

# express-response-formatter
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/aofleejay/express-response-formatter/blob/master/LICENSE.md) [![npm](https://img.shields.io/npm/v/express-response-formatter.svg)](https://www.npmjs.com/package/express-response-formatter) [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](http://makeapullrequest.com) [![Build Status](https://travis-ci.org/aofleejay/express-response-formatter.svg?branch=master)](https://travis-ci.org/aofleejay/express-response-formatter)
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/aofleejay/express-response-formatter/blob/master/LICENSE.md) [![npm](https://img.shields.io/npm/v/express-response-formatter.svg)](https://www.npmjs.com/package/express-response-formatter) [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](http://makeapullrequest.com) [![Build Status](https://travis-ci.org/aofleejay/express-response-formatter.svg?branch=master)](https://travis-ci.org/aofleejay/express-response-formatter) [![Coverage Status](https://coveralls.io/repos/github/aofleejay/express-response-formatter/badge.svg?branch=master)](https://coveralls.io/github/aofleejay/express-response-formatter?branch=master)
Better way to format Express response
## How It Works
- You can use response with readable name like `res.formatter.ok` for 200 ok or `res.formatter.badRequest` for 400 bad request.
- It will format your response in two ways `success` and `error`.
- If response is `2xx, 3xx` return response under object key `data`.
- If response is `4xx, 5xx` return response under object key `error`.
- You can pass metadata as second parameter and it's will present under `meta` object key.
## Installation

@@ -15,8 +23,10 @@

Example usage
Response for 200 Ok.
```js
const app = require('express')()
const responseEnhancer = require('express-response-formatter')
import app from 'express'
import { responseEnhancer } from 'express-response-formatter'
const app = express()
// Add formatter functions to "res" object via "responseEnhancer()"

@@ -53,3 +63,3 @@ app.use(responseEnhancer())

200 OK with "meta field"
Response for 200 Ok with `meta field`

@@ -89,3 +99,3 @@ ```js

400 Bad Request with "multiple errors"
Response for 400 Bad Request with "error"

@@ -106,3 +116,3 @@ ```js

{
"errors": [
"error": [
{

@@ -120,20 +130,20 @@ "detail": "Field id is required."

| METHOD | STATUS CODE |
| ---------------------------------------- | ----------- |
| res.formatter.ok(data, meta?) | 200 |
| res.formatter.created(data, meta?) | 201 |
| res.formatter.accepted(data, meta?) | 202 |
| res.formatter.noContent(data, meta?) | 204 |
| res.formatter.badRequest(errors) | 400 |
| res.formatter.unauthorized(errors) | 401 |
| res.formatter.forbidden(errors) | 403 |
| res.formatter.notFound(errors) | 404 |
| res.formatter.methodNotAllowed(errors) | 405 |
| res.formatter.timeout(errors) | 408 |
| res.formatter.conflict(errors) | 409 |
| res.formatter.unprocess(errors) | 422 |
| res.formatter.tooManyRequests(errors) | 429 |
| res.formatter.serverError(errors) | 500 |
| res.formatter.badGateway(errors) | 502 |
| res.formatter.serviceUnavailable(errors) | 503 |
| res.formatter.gatewayTimeout(errors) | 504 |
| METHOD | STATUS CODE |
| ---------------------------------------------- | ----------- |
| res.formatter.ok(data, meta?) | 200 |
| res.formatter.created(data, meta?) | 201 |
| res.formatter.accepted(data, meta?) | 202 |
| res.formatter.noContent(data, meta?) | 204 |
| res.formatter.badRequest(errors, meta) | 400 |
| res.formatter.unauthorized(errors, meta) | 401 |
| res.formatter.forbidden(errors, meta) | 403 |
| res.formatter.notFound(errors, meta) | 404 |
| res.formatter.methodNotAllowed(errors, meta) | 405 |
| res.formatter.timeout(errors, meta) | 408 |
| res.formatter.conflict(errors, meta) | 409 |
| res.formatter.unprocess(errors, meta) | 422 |
| res.formatter.tooManyRequests(errors, meta) | 429 |
| res.formatter.serverError(errors, meta) | 500 |
| res.formatter.badGateway(errors, meta) | 502 |
| res.formatter.serviceUnavailable(errors, meta) | 503 |
| res.formatter.gatewayTimeout(errors, meta) | 504 |
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc