Socket
Socket
Sign inDemoInstall

koa-formatter

Package Overview
Dependencies
0
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    koa-formatter

A response body formatter for Koa


Version published
Weekly downloads
1
decreased by-83.33%
Maintainers
1
Install size
4.32 kB
Created
Weekly downloads
 

Readme

Source

koa-formatter

A response body formatter for Koa.

This middleware can be used to format the JSON result of API calls.

Features

  • Custom format function
  • Handle aggregation of errors
  • Flowtype definition

Installation

NPM

$ npm install koa-formatter

Usage

const Koa = require('koa');
const formatter = require('koa-formatter');

const app = new Koa();

app.use(formatter());
app.use(formatter({ formatter: function(ctx, errors) {
  ctx.body = (errors.length ? 'some errors occurred' : 'ok');
}}))

Formatter

The library comes with a built-in formatter.

app.use(formatter({ formatter: formatter.defaultFormatter() }));
app.use(function(ctx) {
  ctx.throw('simple error');
});
{
  "ok": 0,
  "status": 500,
  "errors": [ "simple error" ]
}
app.use(formatter({ formatter: formatter.defaultFormatter() }));
app.use(function(ctx) {
  ctx.result = [ 'John' ];
});
{
  "ok": 1,
  "status": 200,
  "result": [
    "John"
  ]
}

Licences

MIT

Keywords

FAQs

Last updated on 29 Aug 2016

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc