Giter VIP home page Giter VIP logo

express-response-formatter's Introduction

express-response-formatter

GitHub license npm code style: prettier PRs Welcome Build Status Coverage Status

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.

Usage

Installation

npm install express-response-formatter --save

Quick Start

Response for 200 Ok.

import app from 'express'
import { responseEnhancer } from 'express-response-formatter'

const app = express()

// Add formatter functions to "res" object via "responseEnhancer()"
app.use(responseEnhancer())

app.get('/success', (req, res) => {
  const users = [{ name: 'Dana Kennedy' }, { name: 'Warren Young' }]

  // It's enhance "res" with "formatter" which contain formatter functions
  res.formatter.ok(users)
})

app.listen(3000, () => console.log('Start at http://localhost:3000'))

Result

HTTP/1.1 200 Ok
{
  "data": [
    {
      "name": "Dana Kennedy"
    },
    {
      "name": "Warren Young"
    }
  ]
}

More usages

Response for 200 Ok with meta field

app.get('/success-with-meta', (req, res) => {
  const users = [{ name: 'Dana Kennedy' }, { name: 'Warren Young' }]

  const meta = {
    total: 2,
    limit: 10,
    offset: 0,
  }

  res.formatter.ok(users, meta)
})
HTTP/1.1 200 Ok
{
  "meta": {
    "total": 2,
    "limit": 10,
    "offset": 0,
  },
  "data": [
    {
      "name": "Dana Kennedy"
    },
    {
      "name": "Warren Young"
    }
  ]
}

Response for 400 Bad Request with "error"

app.get('/bad-request', (req, res) => {
  const errors = [
    { detail: 'Field id is required.' },
    { detail: 'Field foo is required.' },
  ]

  res.formatter.badRequest(errors)
})
HTTP/1.1 400 Bad Request
{
  "error": [
    {
      "detail": "Field id is required."
    },
    {
      "detail": "Field foo is required."
    }
  ]
}

APIs

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

express-response-formatter's People

Contributors

aofleejay avatar dependabot[bot] avatar thinknet-bkk avatar vijaykerure avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

express-response-formatter's Issues

Implementing other status code

Hi,

Is adding new codes considered ?

In my use case I would like to use 207 status but we should be able to also use a wide variety of http standard code.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.